Skip to content

Commit

Permalink
feat(checks): add new predicates, refactor existing
Browse files Browse the repository at this point in the history
- add: isBlob, isEven, isFile, isInRange, isOdd, isRegExp
- add: hasMinLength, hasMaxLength
- update: existsAndNotNull, isArray, isArrayLike
  • Loading branch information
postspectacular committed Feb 8, 2018
1 parent 080c2ee commit 68f8fc2
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/checks/src/exists-not-null.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export function existsAndNotNull(x: any) {
return !(x === undefined || x === null);
return x != null;
}
3 changes: 3 additions & 0 deletions packages/checks/src/has-max-length.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function hasMaxLength(len: number, x: ArrayLike<any>) {
return x != null && x.length <= len;
}
3 changes: 3 additions & 0 deletions packages/checks/src/has-min-length.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function hasMinLength(len: number, x: ArrayLike<any>) {
return x != null && x.length >= len;
}
8 changes: 8 additions & 0 deletions packages/checks/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
export * from "./exists-not-null";
export * from "./exists";
export * from "./has-crypto";
export * from "./has-max-length";
export * from "./has-min-length";
export * from "./has-wasm";
export * from "./has-webgl";
export * from "./has-websocket";
export * from "./implements-function";
export * from "./is-array";
export * from "./is-arraylike";
export * from "./is-blob";
export * from "./is-boolean";
export * from "./is-chrome";
export * from "./is-even";
export * from "./is-false";
export * from "./is-file";
export * from "./is-firefox";
export * from "./is-function";
export * from "./is-ie";
export * from "./is-in-range";
export * from "./is-int32";
export * from "./is-iterable";
export * from "./is-mobile";
Expand All @@ -21,8 +27,10 @@ export * from "./is-node";
export * from "./is-null";
export * from "./is-number";
export * from "./is-object";
export * from "./is-odd";
export * from "./is-plain-object";
export * from "./is-positive";
export * from "./is-regexp";
export * from "./is-safari";
export * from "./is-string";
export * from "./is-symbol";
Expand Down
4 changes: 1 addition & 3 deletions packages/checks/src/is-array.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export function isArray(x: any): x is Array<any> {
return x != null && x.constructor === Array;
}
export const isArray = Array.isArray;
2 changes: 1 addition & 1 deletion packages/checks/src/is-arraylike.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export function isArrayLike(x: any): x is ArrayLike<any> {
return x != null && (x.constructor === Array || x.length !== undefined);
return Array.isArray(x) || (x != null && x.length !== undefined);
}
3 changes: 3 additions & 0 deletions packages/checks/src/is-blob.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isBlob(x: any): x is Blob {
return x instanceof Blob;
}
3 changes: 3 additions & 0 deletions packages/checks/src/is-even.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isEven(x: number) {
return (x % 2) === 0;
}
3 changes: 3 additions & 0 deletions packages/checks/src/is-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isFile(x: any): x is File {
return x instanceof File;
}
3 changes: 3 additions & 0 deletions packages/checks/src/is-in-range.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isInRange(min: number, max: number, x: number) {
return x >= min && x <= max;
}
3 changes: 3 additions & 0 deletions packages/checks/src/is-odd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isOdd(x: number) {
return (x % 2) === 1;
}
3 changes: 3 additions & 0 deletions packages/checks/src/is-regexp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isRegExp(x: any): x is RegExp {
return x instanceof RegExp;
}

0 comments on commit 68f8fc2

Please sign in to comment.