Comparing version 0.2.0-beta.9 to 0.2.0-beta.10
@@ -26,2 +26,3 @@ import { RefObject } from 'react'; | ||
name: string; | ||
displayName?: string; | ||
isAsync?: boolean; | ||
@@ -28,0 +29,0 @@ } |
@@ -68,3 +68,4 @@ "use strict"; | ||
} | ||
this.errors[validator.name] = error; | ||
var name = validator.displayName || validator.name; | ||
this.errors[name] = error; | ||
this._destinationNext(); | ||
@@ -71,0 +72,0 @@ }; |
@@ -1,4 +0,4 @@ | ||
import { IValidator } from './validate'; | ||
export declare function min(value: number, message?: string): IValidator<string>; | ||
export declare function max(value: number, message?: string): IValidator<string>; | ||
import { IValidator, IValidateResult } from './validate'; | ||
export declare function min(limit: number | string, message?: string): (value: string | number) => IValidateResult<string | number> | null; | ||
export declare function max(limit: number, message?: string): (value: string | number) => IValidateResult<string | number> | null; | ||
export declare function required(message?: string): IValidator<any>; | ||
@@ -5,0 +5,0 @@ export declare function requiredTrue(message?: string): IValidator<boolean>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var big_js_1 = require("big.js"); | ||
var EMAIL_REGEXP = /^(?=.{1,254}$)(?=.{1,64}@)[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+(\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+)*@[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?(\.[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?)*$/; | ||
function makeValidator(name, impl, message) { | ||
var validator = function (value) { | ||
function isEmptyInputValue(value) { | ||
// we don't check for string here so it also works with arrays | ||
return value == null || value.length === 0; | ||
} | ||
function min(limit, message) { | ||
return function min(value) { | ||
if (isEmptyInputValue(value)) { | ||
return null; | ||
} | ||
return impl(value) | ||
? { | ||
try { | ||
var decimal = new big_js_1.default(value); | ||
if (decimal.lt(limit)) { | ||
return { | ||
actual: value, | ||
limit: limit, | ||
message: message, | ||
}; | ||
} | ||
} | ||
catch (error) { | ||
return { | ||
actual: value, | ||
limit: limit, | ||
message: message, | ||
actual: value, | ||
} | ||
: null; | ||
}; | ||
} | ||
return null; | ||
}; | ||
validator.name = name; | ||
return validator; | ||
} | ||
function isEmptyInputValue(value) { | ||
// we don't check for string here so it also works with arrays | ||
return value == null || value.length === 0; | ||
} | ||
function min(value, message) { | ||
return makeValidator('min', function (input) { | ||
var parsed = parseFloat(input); | ||
return !isNaN(parsed) && parsed < value; | ||
}, message); | ||
} | ||
exports.min = min; | ||
function max(value, message) { | ||
return makeValidator('max', function (input) { | ||
var parsed = parseFloat(input); | ||
return !isNaN(parsed) && parsed > value; | ||
}, message); | ||
function max(limit, message) { | ||
return function max(value) { | ||
if (isEmptyInputValue(value)) { | ||
return null; | ||
} | ||
try { | ||
var decimal = new big_js_1.default(value); | ||
if (decimal.gt(limit)) { | ||
return { | ||
actual: value, | ||
limit: limit, | ||
message: message, | ||
}; | ||
} | ||
} | ||
catch (error) { | ||
return { | ||
actual: value, | ||
limit: limit, | ||
message: message, | ||
}; | ||
} | ||
return null; | ||
}; | ||
} | ||
@@ -106,8 +130,8 @@ exports.max = max; | ||
actual: input, | ||
pattern: regexp, | ||
} | ||
: null; | ||
} | ||
pattern.name = "pattern(" + regexp + ")"; | ||
return pattern; | ||
} | ||
exports.pattern = pattern; |
@@ -26,2 +26,3 @@ import { RefObject } from 'react'; | ||
name: string; | ||
displayName?: string; | ||
isAsync?: boolean; | ||
@@ -28,0 +29,0 @@ } |
@@ -66,3 +66,4 @@ import * as tslib_1 from "tslib"; | ||
} | ||
this.errors[validator.name] = error; | ||
var name = validator.displayName || validator.name; | ||
this.errors[name] = error; | ||
this._destinationNext(); | ||
@@ -69,0 +70,0 @@ }; |
@@ -1,4 +0,4 @@ | ||
import { IValidator } from './validate'; | ||
export declare function min(value: number, message?: string): IValidator<string>; | ||
export declare function max(value: number, message?: string): IValidator<string>; | ||
import { IValidator, IValidateResult } from './validate'; | ||
export declare function min(limit: number | string, message?: string): (value: string | number) => IValidateResult<string | number> | null; | ||
export declare function max(limit: number, message?: string): (value: string | number) => IValidateResult<string | number> | null; | ||
export declare function required(message?: string): IValidator<any>; | ||
@@ -5,0 +5,0 @@ export declare function requiredTrue(message?: string): IValidator<boolean>; |
@@ -0,33 +1,57 @@ | ||
import Decimal from 'big.js'; | ||
var EMAIL_REGEXP = /^(?=.{1,254}$)(?=.{1,64}@)[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+(\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+)*@[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?(\.[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?)*$/; | ||
function makeValidator(name, impl, message) { | ||
var validator = function (value) { | ||
function isEmptyInputValue(value) { | ||
// we don't check for string here so it also works with arrays | ||
return value == null || value.length === 0; | ||
} | ||
export function min(limit, message) { | ||
return function min(value) { | ||
if (isEmptyInputValue(value)) { | ||
return null; | ||
} | ||
return impl(value) | ||
? { | ||
try { | ||
var decimal = new Decimal(value); | ||
if (decimal.lt(limit)) { | ||
return { | ||
actual: value, | ||
limit: limit, | ||
message: message, | ||
}; | ||
} | ||
} | ||
catch (error) { | ||
return { | ||
actual: value, | ||
limit: limit, | ||
message: message, | ||
}; | ||
} | ||
return null; | ||
}; | ||
} | ||
export function max(limit, message) { | ||
return function max(value) { | ||
if (isEmptyInputValue(value)) { | ||
return null; | ||
} | ||
try { | ||
var decimal = new Decimal(value); | ||
if (decimal.gt(limit)) { | ||
return { | ||
actual: value, | ||
limit: limit, | ||
message: message, | ||
}; | ||
} | ||
} | ||
catch (error) { | ||
return { | ||
actual: value, | ||
} | ||
: null; | ||
limit: limit, | ||
message: message, | ||
}; | ||
} | ||
return null; | ||
}; | ||
validator.name = name; | ||
return validator; | ||
} | ||
function isEmptyInputValue(value) { | ||
// we don't check for string here so it also works with arrays | ||
return value == null || value.length === 0; | ||
} | ||
export function min(value, message) { | ||
return makeValidator('min', function (input) { | ||
var parsed = parseFloat(input); | ||
return !isNaN(parsed) && parsed < value; | ||
}, message); | ||
} | ||
export function max(value, message) { | ||
return makeValidator('max', function (input) { | ||
var parsed = parseFloat(input); | ||
return !isNaN(parsed) && parsed > value; | ||
}, message); | ||
} | ||
export function required(message) { | ||
@@ -97,7 +121,7 @@ function required(input) { | ||
actual: input, | ||
pattern: regexp, | ||
} | ||
: null; | ||
} | ||
pattern.name = "pattern(" + regexp + ")"; | ||
return pattern; | ||
} |
{ | ||
"name": "formulr", | ||
"version": "0.2.0-beta.9", | ||
"version": "0.2.0-beta.10", | ||
"description": "Form toolkit for React", | ||
@@ -36,4 +36,6 @@ "main": "./lib/index.js", | ||
"dependencies": { | ||
"@types/big.js": "^4.0.5", | ||
"@types/react": "^16.8.0", | ||
"@types/react-dom": "^16.8.0", | ||
"big.js": "^5.2.2", | ||
"react": "^16.8.0", | ||
@@ -40,0 +42,0 @@ "react-dom": "^16.8.0", |
@@ -43,2 +43,3 @@ import { RefObject } from 'react'; | ||
name: string; | ||
displayName?: string; | ||
isAsync?: boolean; | ||
@@ -114,3 +115,4 @@ } | ||
} | ||
this.errors[validator.name] = error; | ||
const name = validator.displayName || validator.name; | ||
this.errors[name] = error; | ||
this._destinationNext(); | ||
@@ -117,0 +119,0 @@ } |
@@ -1,50 +0,61 @@ | ||
import { ValidatorResult, IValidator } from './validate'; | ||
import Decimal from 'big.js'; | ||
import { IValidator, IValidateResult } from './validate'; | ||
const EMAIL_REGEXP = /^(?=.{1,254}$)(?=.{1,64}@)[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+(\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+)*@[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?(\.[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?)*$/; | ||
type ValidatorImpl<Value> = (input: Value) => boolean; | ||
function isEmptyInputValue(value: any) { | ||
// we don't check for string here so it also works with arrays | ||
return value == null || value.length === 0; | ||
} | ||
function makeValidator<Value>(name: string, impl: ValidatorImpl<Value>, message?: string): IValidator<Value> { | ||
const validator: IValidator<Value> = (value: Value): ValidatorResult<Value> => { | ||
export function min(limit: number | string, message?: string) { | ||
return function min(value: number | string): IValidateResult<number | string> | null { | ||
if (isEmptyInputValue(value)) { | ||
return null; | ||
} | ||
return impl(value) | ||
? { | ||
try { | ||
const decimal = new Decimal(value); | ||
if (decimal.lt(limit)) { | ||
return { | ||
actual: value, | ||
limit, | ||
message, | ||
actual: value, | ||
} | ||
: null; | ||
}; | ||
} | ||
} catch (error) { | ||
return { | ||
actual: value, | ||
limit, | ||
message, | ||
}; | ||
} | ||
return null; | ||
}; | ||
validator.name = name; | ||
return validator; | ||
} | ||
function isEmptyInputValue(value: any) { | ||
// we don't check for string here so it also works with arrays | ||
return value == null || value.length === 0; | ||
export function max(limit: number, message?: string) { | ||
return function max(value: number | string): IValidateResult<number | string> | null { | ||
if (isEmptyInputValue(value)) { | ||
return null; | ||
} | ||
try { | ||
const decimal = new Decimal(value); | ||
if (decimal.gt(limit)) { | ||
return { | ||
actual: value, | ||
limit, | ||
message, | ||
}; | ||
} | ||
} catch (error) { | ||
return { | ||
actual: value, | ||
limit, | ||
message, | ||
}; | ||
} | ||
return null; | ||
}; | ||
} | ||
export function min(value: number, message?: string) { | ||
return makeValidator( | ||
'min', | ||
(input: string) => { | ||
const parsed = parseFloat(input); | ||
return !isNaN(parsed) && parsed < value; | ||
}, | ||
message, | ||
); | ||
} | ||
export function max(value: number, message?: string) { | ||
return makeValidator( | ||
'max', | ||
(input: string) => { | ||
const parsed = parseFloat(input); | ||
return !isNaN(parsed) && parsed > value; | ||
}, | ||
message, | ||
); | ||
} | ||
export function required(message?: string): IValidator<any> { | ||
@@ -123,7 +134,7 @@ function required(input: any) { | ||
actual: input, | ||
pattern: regexp, | ||
} | ||
: null; | ||
} | ||
pattern.name = `pattern(${regexp})`; | ||
return pattern; | ||
} |
102968
2962
9
+ Added@types/big.js@^4.0.5
+ Addedbig.js@^5.2.2
+ Added@types/big.js@4.0.5(transitive)
+ Addedbig.js@5.2.2(transitive)