From 68f8fc23161e7bbcc8f848b24eeedaf0dd61944d Mon Sep 17 00:00:00 2001 From: Karsten Schmidt Date: Thu, 8 Feb 2018 03:19:15 +0000 Subject: [PATCH] feat(checks): add new predicates, refactor existing - add: isBlob, isEven, isFile, isInRange, isOdd, isRegExp - add: hasMinLength, hasMaxLength - update: existsAndNotNull, isArray, isArrayLike --- packages/checks/src/exists-not-null.ts | 2 +- packages/checks/src/has-max-length.ts | 3 +++ packages/checks/src/has-min-length.ts | 3 +++ packages/checks/src/index.ts | 8 ++++++++ packages/checks/src/is-array.ts | 4 +--- packages/checks/src/is-arraylike.ts | 2 +- packages/checks/src/is-blob.ts | 3 +++ packages/checks/src/is-even.ts | 3 +++ packages/checks/src/is-file.ts | 3 +++ packages/checks/src/is-in-range.ts | 3 +++ packages/checks/src/is-odd.ts | 3 +++ packages/checks/src/is-regexp.ts | 3 +++ 12 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 packages/checks/src/has-max-length.ts create mode 100644 packages/checks/src/has-min-length.ts create mode 100644 packages/checks/src/is-blob.ts create mode 100644 packages/checks/src/is-even.ts create mode 100644 packages/checks/src/is-file.ts create mode 100644 packages/checks/src/is-in-range.ts create mode 100644 packages/checks/src/is-odd.ts create mode 100644 packages/checks/src/is-regexp.ts diff --git a/packages/checks/src/exists-not-null.ts b/packages/checks/src/exists-not-null.ts index 2e41f593a7..dfc00f83f9 100644 --- a/packages/checks/src/exists-not-null.ts +++ b/packages/checks/src/exists-not-null.ts @@ -1,3 +1,3 @@ export function existsAndNotNull(x: any) { - return !(x === undefined || x === null); + return x != null; } diff --git a/packages/checks/src/has-max-length.ts b/packages/checks/src/has-max-length.ts new file mode 100644 index 0000000000..5cd5e31072 --- /dev/null +++ b/packages/checks/src/has-max-length.ts @@ -0,0 +1,3 @@ +export function hasMaxLength(len: number, x: ArrayLike) { + return x != null && x.length <= len; +} diff --git a/packages/checks/src/has-min-length.ts b/packages/checks/src/has-min-length.ts new file mode 100644 index 0000000000..905a553295 --- /dev/null +++ b/packages/checks/src/has-min-length.ts @@ -0,0 +1,3 @@ +export function hasMinLength(len: number, x: ArrayLike) { + return x != null && x.length >= len; +} diff --git a/packages/checks/src/index.ts b/packages/checks/src/index.ts index 0b3d55c9e5..d873747bf8 100644 --- a/packages/checks/src/index.ts +++ b/packages/checks/src/index.ts @@ -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"; @@ -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"; diff --git a/packages/checks/src/is-array.ts b/packages/checks/src/is-array.ts index e3240b764f..3d67de5c37 100644 --- a/packages/checks/src/is-array.ts +++ b/packages/checks/src/is-array.ts @@ -1,3 +1 @@ -export function isArray(x: any): x is Array { - return x != null && x.constructor === Array; -} +export const isArray = Array.isArray; diff --git a/packages/checks/src/is-arraylike.ts b/packages/checks/src/is-arraylike.ts index 387e203867..a770a5b345 100644 --- a/packages/checks/src/is-arraylike.ts +++ b/packages/checks/src/is-arraylike.ts @@ -1,3 +1,3 @@ export function isArrayLike(x: any): x is ArrayLike { - return x != null && (x.constructor === Array || x.length !== undefined); + return Array.isArray(x) || (x != null && x.length !== undefined); } diff --git a/packages/checks/src/is-blob.ts b/packages/checks/src/is-blob.ts new file mode 100644 index 0000000000..b8902c8a0a --- /dev/null +++ b/packages/checks/src/is-blob.ts @@ -0,0 +1,3 @@ +export function isBlob(x: any): x is Blob { + return x instanceof Blob; +} diff --git a/packages/checks/src/is-even.ts b/packages/checks/src/is-even.ts new file mode 100644 index 0000000000..464503c36e --- /dev/null +++ b/packages/checks/src/is-even.ts @@ -0,0 +1,3 @@ +export function isEven(x: number) { + return (x % 2) === 0; +} diff --git a/packages/checks/src/is-file.ts b/packages/checks/src/is-file.ts new file mode 100644 index 0000000000..450a1771a4 --- /dev/null +++ b/packages/checks/src/is-file.ts @@ -0,0 +1,3 @@ +export function isFile(x: any): x is File { + return x instanceof File; +} diff --git a/packages/checks/src/is-in-range.ts b/packages/checks/src/is-in-range.ts new file mode 100644 index 0000000000..80b89b5930 --- /dev/null +++ b/packages/checks/src/is-in-range.ts @@ -0,0 +1,3 @@ +export function isInRange(min: number, max: number, x: number) { + return x >= min && x <= max; +} \ No newline at end of file diff --git a/packages/checks/src/is-odd.ts b/packages/checks/src/is-odd.ts new file mode 100644 index 0000000000..a97a0e004c --- /dev/null +++ b/packages/checks/src/is-odd.ts @@ -0,0 +1,3 @@ +export function isOdd(x: number) { + return (x % 2) === 1; +} diff --git a/packages/checks/src/is-regexp.ts b/packages/checks/src/is-regexp.ts new file mode 100644 index 0000000000..9c265f7875 --- /dev/null +++ b/packages/checks/src/is-regexp.ts @@ -0,0 +1,3 @@ +export function isRegExp(x: any): x is RegExp { + return x instanceof RegExp; +}