-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Harry Grumbar
committed
Nov 23, 2017
1 parent
553e91f
commit d3ef69f
Showing
1 changed file
with
17 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,48 @@ | ||
// import isEqual = require('lodash/isEqual') | ||
|
||
export type Rule<T> = (val: any, req: T) => boolean; | ||
export type Rule<T> = (val: any, req: T) => boolean | ||
|
||
export interface DefaultRules { | ||
required: Rule<boolean>; | ||
equals: Rule<any>; | ||
min: Rule<number>; | ||
max: Rule<number>; | ||
required: Rule<boolean> | ||
equals: Rule<any> | ||
min: Rule<number> | ||
max: Rule<number> | ||
} | ||
|
||
export function required(val: any, req: boolean = true) { | ||
if (!req) { | ||
return true; | ||
return true | ||
} | ||
|
||
if (val === undefined || val === null) { | ||
return false; | ||
return false | ||
} | ||
|
||
return String(val).replace(/\s/g, "").length > 0; | ||
return String(val).replace(/\s/g, '').length > 0 | ||
} | ||
|
||
export function equals(val: any, compare: any) { | ||
return val === compare; | ||
return val === compare | ||
} | ||
|
||
export function min(val: any, req: number) { | ||
return getSize(val) >= req; | ||
return getSize(val) >= req | ||
} | ||
|
||
export function max(val: any, req: number) { | ||
return getSize(val) <= req; | ||
return getSize(val) <= req | ||
} | ||
|
||
const rules: DefaultRules = { required, equals, min, max }; | ||
const rules: DefaultRules = { required, equals, min, max } | ||
|
||
export default rules; | ||
export default rules | ||
|
||
function getSize(val: any) { | ||
if (val instanceof Array) { | ||
return val.length; | ||
return val.length | ||
} | ||
|
||
if (typeof val === "number") { | ||
return val; | ||
if (typeof val === 'number') { | ||
return val | ||
} | ||
|
||
return val.length; | ||
return val.length | ||
} |