@wopjs/cast
Advanced tools
+30
-33
| type _ = undefined; | ||
| /** Map `never` to a different type */ | ||
| type MapNeverTo<T, U> = [T, U][T extends any ? 0 : 1]; | ||
| /** Returns `true` if `x` is not `undefined`. */ | ||
@@ -10,8 +12,7 @@ declare const isDefined: <T>(x: T | undefined) => x is T; | ||
| type Falsy = false | null | undefined | 0 | ""; | ||
| interface IsFalsy { | ||
| (x: unknown): x is Falsy; | ||
| <T>(x: T): x is Extract<T, Falsy>; | ||
| } | ||
| type ExtractFalsy<T> = MapNeverTo<Extract<T, Falsy>, Falsy>; | ||
| /** Returns `true` if `Boolean(x)` is `false`. */ | ||
| declare const isFalsy: IsFalsy; | ||
| declare const isFalsy: <T>(x: T) => x is ExtractFalsy<T>; | ||
| /** Returns `x` if `Boolean(x)` is `false`, otherwise returns `undefined`. */ | ||
| declare const toFalsy: <T>(x: T) => ExtractFalsy<T> | _; | ||
| /** Returns `true` if `Boolean(x)` is `true`. */ | ||
@@ -40,38 +41,38 @@ declare const isTruthy: <T>(x: T) => x is Exclude<T, Falsy>; | ||
| declare const toNonEmptyString: (x: unknown) => string | _; | ||
| type NormalizeArrayType<T> = T extends readonly unknown[] ? T : unknown[]; | ||
| type ExtractArray<T> = NormalizeArrayType<Extract<T, readonly unknown[]>>; | ||
| interface IsArray { | ||
| (x: unknown): x is unknown[]; | ||
| <T>(x: T): x is T extends readonly unknown[] ? T : never; | ||
| } | ||
| declare const isArray: IsArray; | ||
| type ExtractArray<T> = MapNeverTo<Extract<T, readonly unknown[]>, unknown[]>; | ||
| declare const isArray: <T>(x: T) => x is ExtractArray<T>; | ||
| /** Returns `x` if `x` is an array. */ | ||
| declare const toArray: <T>(x: T) => ExtractArray<T> | _; | ||
| /** Returns `x` if `x` is an array, otherwise returns `[]` (empty array). */ | ||
| declare const asArray: <T>(x: T) => ExtractArray<T>; | ||
| /** Returns `true` if `x` is an array and has at least one element. */ | ||
| declare const isNonEmptyArray: IsArray; | ||
| declare const isNonEmptyArray: <T>(x: T) => x is ExtractArray<T>; | ||
| /** Returns `x` if `x` is an array and has at least one element, otherwise returns `undefined`. */ | ||
| declare const toNonEmptyArray: <T>(x: T) => ExtractArray<T> | _; | ||
| /** Returns `x` if `x` is an array, otherwise returns `[]` (empty array). */ | ||
| declare const asArray: <T>(x: T) => ExtractArray<T>; | ||
| type ExtractObject<T> = MapNeverTo<Extract<T, object>, object>; | ||
| /** Returns `true` if `x` is an object (including array) and not null. */ | ||
| declare const isObject: <T>(x: T) => x is ExtractObject<T>; | ||
| /** Returns `x` if `x` is an object (including array). */ | ||
| declare const toObject: <T>(x: T) => ExtractObject<T> | _; | ||
| /** Returns `x` if `x` is an object (including array), otherwise returns `{}` (empty object). */ | ||
| declare const asObject: <T>(x: T) => ExtractObject<T>; | ||
| interface PlainObject { | ||
| [key: PropertyKey]: unknown; | ||
| } | ||
| /** Returns `true` if `x` is an object (including array) and not null. */ | ||
| declare const isObject: (x: unknown) => x is object; | ||
| /** Returns `x` if `x` is an object (including array), otherwise returns `{}` (empty object). */ | ||
| declare const asObject: (x: unknown) => object; | ||
| type ExtractPlainObject<T> = MapNeverTo<Exclude<Extract<T, object>, readonly unknown[]>, PlainObject>; | ||
| /** Returns `true` if `x` is a plain object (shallow test), not `null` or array. */ | ||
| declare const isPlainObject: (x: unknown) => x is PlainObject; | ||
| declare const isPlainObject: <T>(x: T) => x is ExtractPlainObject<T>; | ||
| /** Returns `x` if `x` is a plain object. */ | ||
| declare const toPlainObject: (x: unknown) => PlainObject | _; | ||
| declare const toPlainObject: <T>(x: T) => ExtractPlainObject<T> | _; | ||
| /** Returns `x` if `x` is a plain object, otherwise returns `{}` (empty object). */ | ||
| declare const asPlainObject: (x: unknown) => PlainObject; | ||
| declare const asPlainObject: <T>(x: T) => ExtractPlainObject<T>; | ||
| /** Returns `true` if `x` is a plain object and has at least one key. */ | ||
| declare const isNonEmptyPlainObject: (x: unknown) => x is PlainObject; | ||
| declare const isNonEmptyPlainObject: <T>(x: T) => x is ExtractPlainObject<T>; | ||
| /** Returns `x` if `x` is a plain object and has at least one key. */ | ||
| declare const toNonEmptyPlainObject: <T extends PlainObject>(x: T) => T | _; | ||
| declare const toNonEmptyPlainObject: <T>(x: T) => ExtractPlainObject<T> | _; | ||
| /** Returns `true` if `x` is a plain object and has at least one key with non-undefined value. */ | ||
| declare const isNonEmptyJSONObject: (x: unknown) => x is PlainObject; | ||
| declare const isNonEmptyJSONObject: <T>(x: T) => x is ExtractPlainObject<T>; | ||
| /** Returns `x` if `x` is a plain object and has at least one key with non-undefined value, otherwise returns `undefined`. */ | ||
| declare const toNonEmptyJSONObject: <T extends PlainObject>(x: T) => T | _; | ||
| declare const toNonEmptyJSONObject: <T>(x: T) => ExtractPlainObject<T> | _; | ||
| type ExtractPlainObjectValue<T> = ExtractPlainObject<T>[keyof ExtractPlainObject<T>]; | ||
| /** | ||
@@ -81,9 +82,5 @@ * Creates an object from `x` with keys `k` if `f(x[k])` returns `true`. | ||
| */ | ||
| declare const toPlainObjectOf: <T>(x: unknown, f: (v: unknown) => v is T) => { | ||
| [key: PropertyKey]: T; | ||
| } | _; | ||
| declare const toPlainObjectOf: <T, U extends ExtractPlainObjectValue<T>>(x: T, f: (v: ExtractPlainObjectValue<T>) => v is U) => Record<PropertyKey, U> | _; | ||
| /** Filter props from object `x` whose values are `true`. */ | ||
| declare const toPlainObjectOfTrue: (x: unknown) => { | ||
| [key: PropertyKey]: true; | ||
| } | _; | ||
| declare const toPlainObjectOfTrue: (x: unknown) => Record<PropertyKey, true> | _; | ||
| /** | ||
@@ -103,2 +100,2 @@ * Returns `x` if `x` is string, otherwise returns `''` (empty string) if | ||
| export { type ExtractArray, type Falsy, type IsArray, type IsFalsy, type NormalizeArrayType, type PlainObject, type _, asArray, asNumber, asObject, asPlainObject, asString, asTrue, isArray, isBoolean, isDefined, isFalsy, isNonEmptyArray, isNonEmptyJSONObject, isNonEmptyPlainObject, isNonEmptyString, isNumber, isObject, isPlainObject, isString, isTrue, isTruthy, noop, print, returnsEmptyString, returnsFalse, returnsNull, returnsTrue, returnsUndefined, toArray, toBoolean, toNonEmptyArray, toNonEmptyJSONObject, toNonEmptyPlainObject, toNonEmptyString, toNumber, toPlainObject, toPlainObjectOf, toPlainObjectOfTrue, toString, toTrue, toTruthy }; | ||
| export { type ExtractArray, type ExtractFalsy, type ExtractObject, type ExtractPlainObject, type Falsy, type MapNeverTo, type PlainObject, type _, asArray, asNumber, asObject, asPlainObject, asString, asTrue, isArray, isBoolean, isDefined, isFalsy, isNonEmptyArray, isNonEmptyJSONObject, isNonEmptyPlainObject, isNonEmptyString, isNumber, isObject, isPlainObject, isString, isTrue, isTruthy, noop, print, returnsEmptyString, returnsFalse, returnsNull, returnsTrue, returnsUndefined, toArray, toBoolean, toFalsy, toNonEmptyArray, toNonEmptyJSONObject, toNonEmptyPlainObject, toNonEmptyString, toNumber, toObject, toPlainObject, toPlainObjectOf, toPlainObjectOfTrue, toString, toTrue, toTruthy }; |
+30
-33
| type _ = undefined; | ||
| /** Map `never` to a different type */ | ||
| type MapNeverTo<T, U> = [T, U][T extends any ? 0 : 1]; | ||
| /** Returns `true` if `x` is not `undefined`. */ | ||
@@ -10,8 +12,7 @@ declare const isDefined: <T>(x: T | undefined) => x is T; | ||
| type Falsy = false | null | undefined | 0 | ""; | ||
| interface IsFalsy { | ||
| (x: unknown): x is Falsy; | ||
| <T>(x: T): x is Extract<T, Falsy>; | ||
| } | ||
| type ExtractFalsy<T> = MapNeverTo<Extract<T, Falsy>, Falsy>; | ||
| /** Returns `true` if `Boolean(x)` is `false`. */ | ||
| declare const isFalsy: IsFalsy; | ||
| declare const isFalsy: <T>(x: T) => x is ExtractFalsy<T>; | ||
| /** Returns `x` if `Boolean(x)` is `false`, otherwise returns `undefined`. */ | ||
| declare const toFalsy: <T>(x: T) => ExtractFalsy<T> | _; | ||
| /** Returns `true` if `Boolean(x)` is `true`. */ | ||
@@ -40,38 +41,38 @@ declare const isTruthy: <T>(x: T) => x is Exclude<T, Falsy>; | ||
| declare const toNonEmptyString: (x: unknown) => string | _; | ||
| type NormalizeArrayType<T> = T extends readonly unknown[] ? T : unknown[]; | ||
| type ExtractArray<T> = NormalizeArrayType<Extract<T, readonly unknown[]>>; | ||
| interface IsArray { | ||
| (x: unknown): x is unknown[]; | ||
| <T>(x: T): x is T extends readonly unknown[] ? T : never; | ||
| } | ||
| declare const isArray: IsArray; | ||
| type ExtractArray<T> = MapNeverTo<Extract<T, readonly unknown[]>, unknown[]>; | ||
| declare const isArray: <T>(x: T) => x is ExtractArray<T>; | ||
| /** Returns `x` if `x` is an array. */ | ||
| declare const toArray: <T>(x: T) => ExtractArray<T> | _; | ||
| /** Returns `x` if `x` is an array, otherwise returns `[]` (empty array). */ | ||
| declare const asArray: <T>(x: T) => ExtractArray<T>; | ||
| /** Returns `true` if `x` is an array and has at least one element. */ | ||
| declare const isNonEmptyArray: IsArray; | ||
| declare const isNonEmptyArray: <T>(x: T) => x is ExtractArray<T>; | ||
| /** Returns `x` if `x` is an array and has at least one element, otherwise returns `undefined`. */ | ||
| declare const toNonEmptyArray: <T>(x: T) => ExtractArray<T> | _; | ||
| /** Returns `x` if `x` is an array, otherwise returns `[]` (empty array). */ | ||
| declare const asArray: <T>(x: T) => ExtractArray<T>; | ||
| type ExtractObject<T> = MapNeverTo<Extract<T, object>, object>; | ||
| /** Returns `true` if `x` is an object (including array) and not null. */ | ||
| declare const isObject: <T>(x: T) => x is ExtractObject<T>; | ||
| /** Returns `x` if `x` is an object (including array). */ | ||
| declare const toObject: <T>(x: T) => ExtractObject<T> | _; | ||
| /** Returns `x` if `x` is an object (including array), otherwise returns `{}` (empty object). */ | ||
| declare const asObject: <T>(x: T) => ExtractObject<T>; | ||
| interface PlainObject { | ||
| [key: PropertyKey]: unknown; | ||
| } | ||
| /** Returns `true` if `x` is an object (including array) and not null. */ | ||
| declare const isObject: (x: unknown) => x is object; | ||
| /** Returns `x` if `x` is an object (including array), otherwise returns `{}` (empty object). */ | ||
| declare const asObject: (x: unknown) => object; | ||
| type ExtractPlainObject<T> = MapNeverTo<Exclude<Extract<T, object>, readonly unknown[]>, PlainObject>; | ||
| /** Returns `true` if `x` is a plain object (shallow test), not `null` or array. */ | ||
| declare const isPlainObject: (x: unknown) => x is PlainObject; | ||
| declare const isPlainObject: <T>(x: T) => x is ExtractPlainObject<T>; | ||
| /** Returns `x` if `x` is a plain object. */ | ||
| declare const toPlainObject: (x: unknown) => PlainObject | _; | ||
| declare const toPlainObject: <T>(x: T) => ExtractPlainObject<T> | _; | ||
| /** Returns `x` if `x` is a plain object, otherwise returns `{}` (empty object). */ | ||
| declare const asPlainObject: (x: unknown) => PlainObject; | ||
| declare const asPlainObject: <T>(x: T) => ExtractPlainObject<T>; | ||
| /** Returns `true` if `x` is a plain object and has at least one key. */ | ||
| declare const isNonEmptyPlainObject: (x: unknown) => x is PlainObject; | ||
| declare const isNonEmptyPlainObject: <T>(x: T) => x is ExtractPlainObject<T>; | ||
| /** Returns `x` if `x` is a plain object and has at least one key. */ | ||
| declare const toNonEmptyPlainObject: <T extends PlainObject>(x: T) => T | _; | ||
| declare const toNonEmptyPlainObject: <T>(x: T) => ExtractPlainObject<T> | _; | ||
| /** Returns `true` if `x` is a plain object and has at least one key with non-undefined value. */ | ||
| declare const isNonEmptyJSONObject: (x: unknown) => x is PlainObject; | ||
| declare const isNonEmptyJSONObject: <T>(x: T) => x is ExtractPlainObject<T>; | ||
| /** Returns `x` if `x` is a plain object and has at least one key with non-undefined value, otherwise returns `undefined`. */ | ||
| declare const toNonEmptyJSONObject: <T extends PlainObject>(x: T) => T | _; | ||
| declare const toNonEmptyJSONObject: <T>(x: T) => ExtractPlainObject<T> | _; | ||
| type ExtractPlainObjectValue<T> = ExtractPlainObject<T>[keyof ExtractPlainObject<T>]; | ||
| /** | ||
@@ -81,9 +82,5 @@ * Creates an object from `x` with keys `k` if `f(x[k])` returns `true`. | ||
| */ | ||
| declare const toPlainObjectOf: <T>(x: unknown, f: (v: unknown) => v is T) => { | ||
| [key: PropertyKey]: T; | ||
| } | _; | ||
| declare const toPlainObjectOf: <T, U extends ExtractPlainObjectValue<T>>(x: T, f: (v: ExtractPlainObjectValue<T>) => v is U) => Record<PropertyKey, U> | _; | ||
| /** Filter props from object `x` whose values are `true`. */ | ||
| declare const toPlainObjectOfTrue: (x: unknown) => { | ||
| [key: PropertyKey]: true; | ||
| } | _; | ||
| declare const toPlainObjectOfTrue: (x: unknown) => Record<PropertyKey, true> | _; | ||
| /** | ||
@@ -103,2 +100,2 @@ * Returns `x` if `x` is string, otherwise returns `''` (empty string) if | ||
| export { type ExtractArray, type Falsy, type IsArray, type IsFalsy, type NormalizeArrayType, type PlainObject, type _, asArray, asNumber, asObject, asPlainObject, asString, asTrue, isArray, isBoolean, isDefined, isFalsy, isNonEmptyArray, isNonEmptyJSONObject, isNonEmptyPlainObject, isNonEmptyString, isNumber, isObject, isPlainObject, isString, isTrue, isTruthy, noop, print, returnsEmptyString, returnsFalse, returnsNull, returnsTrue, returnsUndefined, toArray, toBoolean, toNonEmptyArray, toNonEmptyJSONObject, toNonEmptyPlainObject, toNonEmptyString, toNumber, toPlainObject, toPlainObjectOf, toPlainObjectOfTrue, toString, toTrue, toTruthy }; | ||
| export { type ExtractArray, type ExtractFalsy, type ExtractObject, type ExtractPlainObject, type Falsy, type MapNeverTo, type PlainObject, type _, asArray, asNumber, asObject, asPlainObject, asString, asTrue, isArray, isBoolean, isDefined, isFalsy, isNonEmptyArray, isNonEmptyJSONObject, isNonEmptyPlainObject, isNonEmptyString, isNumber, isObject, isPlainObject, isString, isTrue, isTruthy, noop, print, returnsEmptyString, returnsFalse, returnsNull, returnsTrue, returnsUndefined, toArray, toBoolean, toFalsy, toNonEmptyArray, toNonEmptyJSONObject, toNonEmptyPlainObject, toNonEmptyString, toNumber, toObject, toPlainObject, toPlainObjectOf, toPlainObjectOfTrue, toString, toTrue, toTruthy }; |
+5
-1
@@ -10,2 +10,3 @@ 'use strict'; | ||
| var isFalsy = (x) => !x; | ||
| var toFalsy = (x) => isFalsy(x) ? x : _; | ||
| var isTruthy = (x) => !!x; | ||
@@ -25,6 +26,7 @@ var toTruthy = (x) => isTruthy(x) ? x : _; | ||
| var toArray = (x) => isArray(x) ? x : _; | ||
| var asArray = (x) => isArray(x) ? x : []; | ||
| var isNonEmptyArray = (x) => isArray(x) && x.length > 0; | ||
| var toNonEmptyArray = (x) => isNonEmptyArray(x) ? x : _; | ||
| var asArray = (x) => isArray(x) ? x : []; | ||
| var isObject = (x) => x !== null && typeof x === "object"; | ||
| var toObject = (x) => isObject(x) ? x : _; | ||
| var asObject = (x) => isObject(x) ? x : {}; | ||
@@ -103,2 +105,3 @@ var isPlainObject = (x) => isObject(x) && !isArray(x); | ||
| exports.toBoolean = toBoolean; | ||
| exports.toFalsy = toFalsy; | ||
| exports.toNonEmptyArray = toNonEmptyArray; | ||
@@ -109,2 +112,3 @@ exports.toNonEmptyJSONObject = toNonEmptyJSONObject; | ||
| exports.toNumber = toNumber; | ||
| exports.toObject = toObject; | ||
| exports.toPlainObject = toPlainObject; | ||
@@ -111,0 +115,0 @@ exports.toPlainObjectOf = toPlainObjectOf; |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"sources":["../src/is-to-as.ts","../src/returns.ts"],"names":[],"mappings":";;;AAAA,IAAM,CAAA,GAAI,MAAA;AAIH,IAAM,SAAA,GAAY,CAAI,CAAA,KAA6B,CAAA,KAAM;AAEzD,IAAM,MAAA,GAAS,CAAC,CAAA,KAA0B,CAAA,KAAM;AAGhD,IAAM,MAAA,GAAS,CAAC,CAAA,KAA0B,CAAA,KAAM,OAAO,IAAA,GAAO;AAG9D,IAAM,MAAA,GAAS,CAAC,CAAA,KAAyB,CAAA,KAAM,OAAO,CAAA,GAAI;AAU1D,IAAM,OAAA,GAAmB,CAAC,CAAA,KAA2B,CAAC;AAGtD,IAAM,QAAA,GAAW,CAAI,CAAA,KAAiC,CAAC,CAAC;AAGxD,IAAM,WAAW,CAAI,CAAA,KAAiC,QAAA,CAAS,C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C,MAAA,KAAW,EAAC,EAAG,GAAG,CAAA,GAAI,KAAA;AAAA,MACzB;AAAA,IACF;AAEA,IAAA,OAAO,MAAA;AAAA,EACT;AACF;AAGO,IAAM,mBAAA,GAAsB,CAAC,CAAA,KAA8C,eAAA,CAAgB,GAAG,MAAM;AAOpG,IAAM,KAAA,GAAQ,CAAC,CAAA,KAAuB;AAC3C,EAAA,IAAI,QAAA,CAAS,CAAC,CAAA,EAAG,OAAO,CAAA;AACxB,EAAA,IAAI,CAAA,IAAK,MAAM,OAAO,EAAA;AACtB,EAAA,IAAI;AACF,IAAA,OAAO,IAAA,CAAK,SAAA,CAAU,CAAA,EAAG,IAAA,EAAM,CAAC,CAAA;AAAA,EAClC,CAAA,CAAA,MAAQ;AAEN,IAAA,OAAO,CAAA,GAAI,EAAA;AAAA,EACb;AACF;;;ACnKO,IAAM,OAAO,MAAY;AAAC;AAE1B,IAAM,gBAAA,GAAmB;AAEzB,IAAM,cAAc,MAAY;AAEhC,IAAM,eAAe,MAAa;AAElC,IAAM,cAAc,MAAY;AAEhC,IAAM,qBAAqB,MAAc","file":"index.js"} |
+4
-2
@@ -8,2 +8,3 @@ // src/is-to-as.ts | ||
| var isFalsy = (x) => !x; | ||
| var toFalsy = (x) => isFalsy(x) ? x : _; | ||
| var isTruthy = (x) => !!x; | ||
@@ -23,6 +24,7 @@ var toTruthy = (x) => isTruthy(x) ? x : _; | ||
| var toArray = (x) => isArray(x) ? x : _; | ||
| var asArray = (x) => isArray(x) ? x : []; | ||
| var isNonEmptyArray = (x) => isArray(x) && x.length > 0; | ||
| var toNonEmptyArray = (x) => isNonEmptyArray(x) ? x : _; | ||
| var asArray = (x) => isArray(x) ? x : []; | ||
| var isObject = (x) => x !== null && typeof x === "object"; | ||
| var toObject = (x) => isObject(x) ? x : _; | ||
| var asObject = (x) => isObject(x) ? x : {}; | ||
@@ -72,4 +74,4 @@ var isPlainObject = (x) => isObject(x) && !isArray(x); | ||
| export { asArray, asNumber, asObject, asPlainObject, asString, asTrue, isArray, isBoolean, isDefined, isFalsy, isNonEmptyArray, isNonEmptyJSONObject, isNonEmptyPlainObject, isNonEmptyString, isNumber, isObject, isPlainObject, isString, isTrue, isTruthy, noop, print, returnsEmptyString, returnsFalse, returnsNull, returnsTrue, returnsUndefined, toArray, toBoolean, toNonEmptyArray, toNonEmptyJSONObject, toNonEmptyPlainObject, toNonEmptyString, toNumber, toPlainObject, toPlainObjectOf, toPlainObjectOfTrue, toString, toTrue, toTruthy }; | ||
| export { asArray, asNumber, asObject, asPlainObject, asString, asTrue, isArray, isBoolean, isDefined, isFalsy, isNonEmptyArray, isNonEmptyJSONObject, isNonEmptyPlainObject, isNonEmptyString, isNumber, isObject, isPlainObject, isString, isTrue, isTruthy, noop, print, returnsEmptyString, returnsFalse, returnsNull, returnsTrue, returnsUndefined, toArray, toBoolean, toFalsy, toNonEmptyArray, toNonEmptyJSONObject, toNonEmptyPlainObject, toNonEmptyString, toNumber, toObject, toPlainObject, toPlainObjectOf, toPlainObjectOfTrue, toString, toTrue, toTruthy }; | ||
| //# sourceMappingURL=index.mjs.map | ||
| //# sourceMappingURL=index.mjs.map |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"sources":["../src/is-to-as.ts","../src/returns.ts"],"names":[],"mappings":";AAAA,IAAM,CAAA,GAAI,MAAA;AAIH,IAAM,SAAA,GAAY,CAAI,CAAA,KAA6B,CAAA,KAAM;AAEzD,IAAM,MAAA,GAAS,CAAC,CAAA,KAA0B,CAAA,KAAM;AAGhD,IAAM,MAAA,GAAS,CAAC,CAAA,KAA0B,CAAA,KAAM,OAAO,IAAA,GAAO;AAG9D,IAAM,MAAA,GAAS,CAAC,CAAA,KAAyB,CAAA,KAAM,OAAO,CAAA,GAAI;AAU1D,IAAM,OAAA,GAAmB,CAAC,CAAA,KAA2B,CAAC;AAGtD,IAAM,QAAA,GAAW,CAAI,CAAA,KAAiC,CAAC,CAAC;AAGxD,IAAM,WAAW,CAAI,CAAA,KAAiC,QAAA,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AA,CAAC,MAAA,KAAW,EAAC,EAAG,GAAG,CAAA,GAAI,KAAA;AAAA,MACzB;AAAA,IACF;AAEA,IAAA,OAAO,MAAA;AAAA,EACT;AACF;AAGO,IAAM,mBAAA,GAAsB,CAAC,CAAA,KAA8C,eAAA,CAAgB,GAAG,MAAM;AAOpG,IAAM,KAAA,GAAQ,CAAC,CAAA,KAAuB;AAC3C,EAAA,IAAI,QAAA,CAAS,CAAC,CAAA,EAAG,OAAO,CAAA;AACxB,EAAA,IAAI,CAAA,IAAK,MAAM,OAAO,EAAA;AACtB,EAAA,IAAI;AACF,IAAA,OAAO,IAAA,CAAK,SAAA,CAAU,CAAA,EAAG,IAAA,EAAM,CAAC,CAAA;AAAA,EAClC,CAAA,CAAA,MAAQ;AAEN,IAAA,OAAO,CAAA,GAAI,EAAA;AAAA,EACb;AACF;;;ACnKO,IAAM,OAAO,MAAY;AAAC;AAE1B,IAAM,gBAAA,GAAmB;AAEzB,IAAM,cAAc,MAAY;AAEhC,IAAM,eAAe,MAAa;AAElC,IAAM,cAAc,MAAY;AAEhC,IAAM,qBAAqB,MAAc","file":"index.mjs"} |
+10
-3
| { | ||
| "name": "@wopjs/cast", | ||
| "version": "0.1.7", | ||
| "description": "Filter types from unknown", | ||
| "version": "0.1.8", | ||
| "description": "Type-safe utilities for filtering and coercing unknown values in TypeScript.", | ||
| "repository": "wopjs/cast", | ||
@@ -28,3 +28,10 @@ "main": "./dist/index.js", | ||
| "typescript", | ||
| "coerce" | ||
| "type-guard", | ||
| "type-narrowing", | ||
| "type-safe", | ||
| "coerce", | ||
| "cast", | ||
| "unknown", | ||
| "validation", | ||
| "filter" | ||
| ], | ||
@@ -31,0 +38,0 @@ "maintainers": [ |
+151
-13
@@ -9,4 +9,11 @@ # @wopjs/cast | ||
| Filter types from unknown. | ||
| Type-safe utilities for filtering and coercing `unknown` values in TypeScript. | ||
| ## Features | ||
| - **Three-tier API pattern** (`is`/`to`/`as`) for flexible type filtering | ||
| - **Strong TypeScript type narrowing** that preserves complex types (unions, tuples, readonly arrays) | ||
| - **Zero dependencies** and tree-shakeable | ||
| - **Safe edge case handling** (NaN, null, undefined, circular references) | ||
| ## Install | ||
@@ -18,20 +25,151 @@ | ||
| ## Usage | ||
| ## API Pattern | ||
| ```js | ||
| import * as c from "@wopjs/cast"; | ||
| import { Option } from "@wopjs/tsur"; | ||
| Every type has up to three functions following a consistent naming pattern: | ||
| const dataParser = { | ||
| width: c.toNumber, | ||
| position: a => Option.from(a, a => a && c.isNumber(a.x) && c.isNumber(a.y)), | ||
| metadata: c.toNonEmptyPlainObject, | ||
| }; | ||
| | Pattern | Returns | Use Case | | ||
| | ------- | ---------------- | --------------------------------------------- | | ||
| | `is*` | `boolean` | Type guards for conditional narrowing | | ||
| | `to*` | `T \| undefined` | Optional values, composable with Option types | | ||
| | `as*` | `T` | Always returns valid value with fallback | | ||
| function parseData(data) { | ||
| const d = c.asObject(data); | ||
| return Object.fromEntries(Object.keys(dataParser).map(k => [k, Option.unwrapOr(dataParser[k](d[k]))])); | ||
| ```ts | ||
| import { isNumber, toNumber, asNumber } from "@wopjs/cast"; | ||
| // is* - Type guard for conditionals | ||
| if (isNumber(value)) { | ||
| value; // type narrowed to number | ||
| } | ||
| // to* - Returns undefined for invalid input | ||
| const num = toNumber(input); // number | undefined | ||
| // as* - Always returns a number (0 as fallback) | ||
| const safeNum = asNumber(input); // number | ||
| ``` | ||
| ## Type Narrowing | ||
| The library preserves and correctly narrows complex TypeScript types: | ||
| ```ts | ||
| import { isArray, toArray, isTruthy } from "@wopjs/cast"; | ||
| // Preserves array element types | ||
| const arr: string[] = ["a", "b"]; | ||
| if (isArray(arr)) { | ||
| arr; // still string[], not unknown[] | ||
| } | ||
| // Extracts from unions | ||
| const value: string | string[] = getData(); | ||
| if (isArray(value)) { | ||
| value; // narrowed to string[] | ||
| } | ||
| // Preserves tuples and readonly arrays | ||
| const tuple: [string, number] = ["a", 1]; | ||
| const result = toArray(tuple); // [string, number] | undefined | ||
| // Excludes falsy types | ||
| const val: string | null | undefined = "hello"; | ||
| if (isTruthy(val)) { | ||
| val; // narrowed to string | ||
| } | ||
| ``` | ||
| ## Available Functions | ||
| ### Booleans | ||
| | Function | Description | | ||
| | ----------- | -------------------------------------------- | | ||
| | `isTrue` | Returns `true` if value is exactly `true` | | ||
| | `toTrue` | Returns `true` or `undefined` | | ||
| | `asTrue` | Returns `true` or `false` | | ||
| | `isTruthy` | Returns `true` if `Boolean(x)` is `true` | | ||
| | `toTruthy` | Returns truthy value or `undefined` | | ||
| | `isFalsy` | Returns `true` if `Boolean(x)` is `false` | | ||
| | `toFalsy` | Returns falsy value or `undefined` | | ||
| | `isBoolean` | Returns `true` if value is `true` or `false` | | ||
| | `toBoolean` | Returns boolean or `undefined` | | ||
| ### Numbers | ||
| | Function | Description | | ||
| | ---------- | ----------------------------------------------------- | | ||
| | `isNumber` | Returns `true` if value is a number (excluding `NaN`) | | ||
| | `toNumber` | Returns number or `undefined` | | ||
| | `asNumber` | Returns number or `0` | | ||
| ### Strings | ||
| | Function | Description | | ||
| | ------------------ | --------------------------------------------- | | ||
| | `isString` | Returns `true` if value is a string | | ||
| | `toString` | Returns string or `undefined` | | ||
| | `asString` | Returns string or `""` | | ||
| | `isNonEmptyString` | Returns `true` if value is a non-empty string | | ||
| | `toNonEmptyString` | Returns non-empty string or `undefined` | | ||
| ### Arrays | ||
| | Function | Description | | ||
| | ----------------- | ----------------------------------------------- | | ||
| | `isArray` | Type guard for arrays (preserves element types) | | ||
| | `toArray` | Returns array or `undefined` | | ||
| | `asArray` | Returns array or `[]` | | ||
| | `isNonEmptyArray` | Type guard for non-empty arrays | | ||
| | `toNonEmptyArray` | Returns non-empty array or `undefined` | | ||
| ### Objects | ||
| | Function | Description | | ||
| | ----------------------- | --------------------------------------------------------------- | | ||
| | `isObject` | Returns `true` for objects (including arrays, excluding `null`) | | ||
| | `toObject` | Returns object or `undefined` | | ||
| | `asObject` | Returns object or `{}` | | ||
| | `isPlainObject` | Returns `true` for plain objects (excluding arrays) | | ||
| | `toPlainObject` | Returns plain object or `undefined` | | ||
| | `asPlainObject` | Returns plain object or `{}` | | ||
| | `isNonEmptyPlainObject` | Returns `true` for objects with at least one key | | ||
| | `toNonEmptyPlainObject` | Returns non-empty object or `undefined` | | ||
| | `isNonEmptyJSONObject` | Returns `true` for objects with non-undefined values | | ||
| | `toNonEmptyJSONObject` | Returns JSON object or `undefined` | | ||
| ### Utilities | ||
| | Function | Description | | ||
| | --------------------- | ------------------------------------------ | | ||
| | `isDefined` | Returns `true` if value is not `undefined` | | ||
| | `toPlainObjectOf` | Filter object values by type predicate | | ||
| | `toPlainObjectOfTrue` | Filter object to only `true` values | | ||
| | `print` | Safely stringify any value for display | | ||
| ## Usage Example | ||
| ```ts | ||
| import * as c from "@wopjs/cast"; | ||
| // Parsing unknown API response | ||
| function parseUser(data: unknown) { | ||
| const obj = c.asPlainObject(data); | ||
| return { | ||
| name: c.asString(obj.name), | ||
| age: c.toNumber(obj.age), // undefined if not a number | ||
| tags: c.asArray(obj.tags), | ||
| settings: c.toNonEmptyPlainObject(obj.settings), | ||
| }; | ||
| } | ||
| // Filtering object values | ||
| const config = { debug: true, verbose: false, enabled: true }; | ||
| c.toPlainObjectOfTrue(config); // { debug: true, enabled: true } | ||
| // Safe number handling | ||
| c.isNumber(NaN); // false (NaN is excluded) | ||
| c.asNumber(NaN); // 0 (safe fallback) | ||
| c.asNumber("42"); // 0 (not coerced, use parseInt for that) | ||
| ``` | ||
| ## Publish New Version | ||
@@ -38,0 +176,0 @@ |
+462
-4
@@ -0,1 +1,3 @@ | ||
| import type { PlainObject } from "."; | ||
| import { describe, it, expect } from "vitest"; | ||
@@ -10,2 +12,3 @@ | ||
| isFalsy, | ||
| toFalsy, | ||
| isBoolean, | ||
@@ -28,2 +31,3 @@ toBoolean, | ||
| isObject, | ||
| toObject, | ||
| asObject, | ||
@@ -72,2 +76,29 @@ isPlainObject, | ||
| expect(isTruthy(1)).toBe(true); | ||
| { | ||
| // Type narrowing - excludes falsy from union | ||
| const val = castType<string | null | undefined>("hello"); | ||
| if (isTruthy(val)) { | ||
| const check: string = val; | ||
| expect(check).toBe("hello"); | ||
| } | ||
| } | ||
| { | ||
| // Type narrowing - excludes all falsy types | ||
| const val = castType<number | false | null | undefined | "" | 0>(42); | ||
| if (isTruthy(val)) { | ||
| const check: number = val; | ||
| expect(check).toBe(42); | ||
| } | ||
| } | ||
| { | ||
| // Type narrowing - narrows boolean to true | ||
| const val = castType<boolean>(true); | ||
| if (isTruthy(val)) { | ||
| const check: true = val; | ||
| expect(check).toBe(true); | ||
| } | ||
| } | ||
| }); | ||
@@ -95,4 +126,69 @@ | ||
| expect(isFalsy(1)).toBe(false); | ||
| { | ||
| // Type narrowing - extracts falsy from union | ||
| const val = castType<string | null | undefined>(null); | ||
| if (isFalsy(val)) { | ||
| const check: null | undefined = val; | ||
| expect(check).toBe(null); | ||
| } | ||
| } | ||
| { | ||
| // Type narrowing - extracts all falsy types | ||
| const val = castType<number | false | null | undefined | "" | 0>(0); | ||
| if (isFalsy(val)) { | ||
| const check: false | null | undefined | "" | 0 = val; | ||
| expect(check).toBe(0); | ||
| } | ||
| } | ||
| { | ||
| // Type narrowing - narrows boolean to false | ||
| const val = castType<boolean>(false); | ||
| if (isFalsy(val)) { | ||
| const check: false = val; | ||
| expect(check).toBe(false); | ||
| } | ||
| } | ||
| }); | ||
| it("toFalsy", () => { | ||
| expect(toFalsy(false)).toBe(false); | ||
| expect(toFalsy(null)).toBe(null); | ||
| expect(toFalsy(undefined)).toBe(undefined); | ||
| expect(toFalsy(0)).toBe(0); | ||
| expect(toFalsy("")).toBe(""); | ||
| expect(toFalsy(true)).toBe(undefined); | ||
| expect(toFalsy(1)).toBe(undefined); | ||
| expect(toFalsy("hello")).toBe(undefined); | ||
| { | ||
| // Type narrowing - extracts falsy from union | ||
| const val = castType<string | null | undefined>(null); | ||
| const result = toFalsy(val); | ||
| if (result !== undefined) { | ||
| const check: "" | null = result; | ||
| expect(check).toBe(null); | ||
| } | ||
| } | ||
| { | ||
| // Type narrowing - returns undefined for truthy value | ||
| const val = castType<string | null>("hello"); | ||
| const result: "" | null | undefined = toFalsy(val); | ||
| expect(result).toBe(undefined); | ||
| } | ||
| { | ||
| // Type narrowing - extracts falsy from number union | ||
| const val = castType<number | false>(0); | ||
| const result = toFalsy(val); | ||
| if (result !== undefined) { | ||
| const check: 0 | false = result; | ||
| expect(check).toBe(0); | ||
| } | ||
| } | ||
| }); | ||
| it("isBoolean", () => { | ||
@@ -473,6 +569,11 @@ expect(isBoolean(true)).toBe(true); | ||
| { | ||
| // Type narrowing - unknown input returns unknown[] | undefined | ||
| // Type narrowing - unknown input returns unknown[] | ||
| const arr = castType<unknown>(["a", "b"]); | ||
| const result: unknown[] | undefined = toNonEmptyArray(arr); | ||
| expect(result).toEqual(["a", "b"]); | ||
| let result = toNonEmptyArray(arr); | ||
| if (result) { | ||
| // rule out never | ||
| let check = result; | ||
| check = castType<unknown[]>(result); | ||
| expect(check).toEqual(["a", "b"]); | ||
| } | ||
| } | ||
@@ -483,4 +584,10 @@ | ||
| const arr = castType<string>("hello"); | ||
| const result: unknown[] | undefined = toNonEmptyArray(arr); | ||
| const result = toNonEmptyArray(arr); | ||
| expect(result).toBe(undefined); | ||
| if (result) { | ||
| // rule out never | ||
| let _check = result; | ||
| _check = castType<unknown[]>(result); | ||
| throw new Error("Unreachable"); | ||
| } | ||
| } | ||
@@ -493,4 +600,109 @@ }); | ||
| expect(isObject(null)).toBe(false); | ||
| { | ||
| // Type narrowing - preserves object type | ||
| const obj = castType<{ a: string }>({ a: "hello" }); | ||
| if (isObject(obj)) { | ||
| const check: { a: string } = obj; | ||
| expect(check).toBe(obj); | ||
| } | ||
| } | ||
| { | ||
| // Type narrowing - extracts object from union | ||
| const obj = castType<string | { a: string }>({ a: "hello" }); | ||
| if (isObject(obj)) { | ||
| const check: object = obj; | ||
| expect(check).toEqual({ a: "hello" }); | ||
| } | ||
| } | ||
| { | ||
| // Type narrowing - arrays are objects | ||
| const arr = castType<string[]>(["a", "b"]); | ||
| if (isObject(arr)) { | ||
| const check: string[] = arr; | ||
| expect(check).toBe(arr); | ||
| } | ||
| } | ||
| { | ||
| // Type narrowing - unknown input narrows to object | ||
| const obj = castType<unknown>({ a: 1 }); | ||
| if (isObject(obj)) { | ||
| const check: object = obj; | ||
| expect(check).toEqual({ a: 1 }); | ||
| } | ||
| } | ||
| { | ||
| // Type narrowing - null | object union extracts object | ||
| const obj = castType<null | { x: number }>({ x: 42 }); | ||
| if (isObject(obj)) { | ||
| const check: { x: number } = obj; | ||
| expect(check).toEqual({ x: 42 }); | ||
| } | ||
| } | ||
| }); | ||
| it("toObject", () => { | ||
| expect(toObject({})).toEqual({}); | ||
| expect(toObject([])).toEqual([]); | ||
| expect(toObject(null)).toBe(undefined); | ||
| { | ||
| // Type narrowing - preserves object type | ||
| const obj = castType<{ a: string }>({ a: "hello" }); | ||
| const result = toObject(obj); | ||
| if (result) { | ||
| const check: { a: string } = result; | ||
| expect(check).toBe(obj); | ||
| } | ||
| } | ||
| { | ||
| // Type narrowing - extracts object from union | ||
| const obj = castType<string | { a: string }>({ a: "hello" }); | ||
| const result = toObject(obj); | ||
| if (result) { | ||
| const check: { a: string } = result; | ||
| expect(check).toEqual({ a: "hello" }); | ||
| } | ||
| } | ||
| { | ||
| // Type narrowing - preserves array (arrays are objects) | ||
| const arr = castType<string[]>(["a", "b"]); | ||
| const result = toObject(arr); | ||
| if (result) { | ||
| const check: string[] = result; | ||
| expect(check).toBe(arr); | ||
| } | ||
| } | ||
| { | ||
| // Type narrowing - unknown input returns object | undefined | ||
| const obj = castType<unknown>({ a: 1 }); | ||
| const result: object | undefined = toObject(obj); | ||
| expect(result).toEqual({ a: 1 }); | ||
| } | ||
| { | ||
| // Type narrowing - non-object type (string) returns undefined | ||
| const obj = castType<string>("hello"); | ||
| const result: object | undefined = toObject(obj); | ||
| expect(result).toBe(undefined); | ||
| } | ||
| { | ||
| // Type narrowing - null | object union extracts object | ||
| const obj = castType<null | { x: number }>({ x: 42 }); | ||
| const result = toObject(obj); | ||
| if (result) { | ||
| const check: { x: number } = result; | ||
| expect(check).toEqual({ x: 42 }); | ||
| } | ||
| } | ||
| }); | ||
| it("asObject", () => { | ||
@@ -500,2 +712,44 @@ expect(asObject({})).toEqual({}); | ||
| expect(asObject(null)).toEqual({}); | ||
| { | ||
| // Type narrowing - preserves object type | ||
| const obj = castType<{ a: string }>({ a: "hello" }); | ||
| const result: { a: string } = asObject(obj); | ||
| expect(result).toBe(obj); | ||
| } | ||
| { | ||
| // Type narrowing - extracts object from union | ||
| const obj = castType<string | { a: string }>({ a: "hello" }); | ||
| const result: { a: string } = asObject(obj); | ||
| expect(result).toEqual({ a: "hello" }); | ||
| } | ||
| { | ||
| // Type narrowing - preserves array (arrays are objects) | ||
| const arr = castType<string[]>(["a", "b"]); | ||
| const result: string[] = asObject(arr); | ||
| expect(result).toBe(arr); | ||
| } | ||
| { | ||
| // Type narrowing - unknown input returns object | ||
| const obj = castType<unknown>({ a: 1 }); | ||
| const result: object = asObject(obj); | ||
| expect(result).toEqual({ a: 1 }); | ||
| } | ||
| { | ||
| // Type narrowing - non-object type (string) returns object | ||
| const obj = castType<string>("hello"); | ||
| const result: object = asObject(obj); | ||
| expect(result).toEqual({}); | ||
| } | ||
| { | ||
| // Type narrowing - null | object union extracts object | ||
| const obj = castType<null | { x: number }>({ x: 42 }); | ||
| const result: { x: number } = asObject(obj); | ||
| expect(result).toEqual({ x: 42 }); | ||
| } | ||
| }); | ||
@@ -507,2 +761,29 @@ | ||
| expect(isPlainObject(null)).toBe(false); | ||
| { | ||
| // Type narrowing - unknown input narrows to PlainObject | ||
| const obj = castType<unknown>({ a: 1 }); | ||
| if (isPlainObject(obj)) { | ||
| const check: PlainObject = obj; | ||
| expect(check).toEqual({ a: 1 }); | ||
| } | ||
| } | ||
| { | ||
| // Type narrowing - excludes array from union | ||
| const obj = castType<{ a: number } | number[]>({ a: 1 }); | ||
| if (isPlainObject(obj)) { | ||
| const check: PlainObject = obj; | ||
| expect(check).toEqual({ a: 1 }); | ||
| } | ||
| } | ||
| { | ||
| // Type narrowing - null | object union extracts PlainObject | ||
| const obj = castType<null | { x: number }>({ x: 42 }); | ||
| if (isPlainObject(obj)) { | ||
| const check: { x: number } = obj; | ||
| expect(check).toEqual({ x: 42 }); | ||
| } | ||
| } | ||
| }); | ||
@@ -514,2 +795,16 @@ | ||
| expect(toPlainObject(null)).toBe(undefined); | ||
| { | ||
| // Type narrowing - unknown input returns PlainObject | undefined | ||
| const obj = castType<unknown>({ a: 1 }); | ||
| const result: PlainObject | undefined = toPlainObject(obj); | ||
| expect(result).toEqual({ a: 1 }); | ||
| } | ||
| { | ||
| // Type narrowing - array returns undefined | ||
| const arr = castType<number[]>([1, 2]); | ||
| const result: PlainObject | undefined = toPlainObject(arr); | ||
| expect(result).toBe(undefined); | ||
| } | ||
| }); | ||
@@ -521,2 +816,23 @@ | ||
| expect(asPlainObject(null)).toEqual({}); | ||
| { | ||
| // Type narrowing - unknown input returns PlainObject | ||
| const obj = castType<unknown>({ a: 1 }); | ||
| const result: PlainObject = asPlainObject(obj); | ||
| expect(result).toEqual({ a: 1 }); | ||
| } | ||
| { | ||
| // Type narrowing - array returns empty object | ||
| const arr = castType<number[]>([1, 2]); | ||
| const result: PlainObject = asPlainObject(arr); | ||
| expect(result).toEqual({}); | ||
| } | ||
| { | ||
| // Type narrowing - non-object returns empty object | ||
| const val = castType<string>("hello"); | ||
| const result: PlainObject = asPlainObject(val); | ||
| expect(result).toEqual({}); | ||
| } | ||
| }); | ||
@@ -528,2 +844,31 @@ | ||
| expect(isNonEmptyPlainObject([])).toBe(false); | ||
| { | ||
| // Type narrowing - unknown input narrows to PlainObject | ||
| const obj = castType<unknown>({ a: 1 }); | ||
| if (isNonEmptyPlainObject(obj)) { | ||
| const check: PlainObject = obj; | ||
| expect(check).toEqual({ a: 1 }); | ||
| } | ||
| } | ||
| { | ||
| // Type narrowing - empty object returns false | ||
| const obj = castType<{ a?: number }>({}); | ||
| if (isNonEmptyPlainObject(obj)) { | ||
| const check: PlainObject = obj; | ||
| expect(check).toEqual({}); | ||
| } else { | ||
| expect(obj).toEqual({}); | ||
| } | ||
| } | ||
| { | ||
| // Type narrowing - excludes array | ||
| const obj = castType<{ a: number } | number[]>({ a: 1 }); | ||
| if (isNonEmptyPlainObject(obj)) { | ||
| const check: PlainObject = obj; | ||
| expect(check).toEqual({ a: 1 }); | ||
| } | ||
| } | ||
| }); | ||
@@ -534,2 +879,33 @@ | ||
| expect(toNonEmptyPlainObject({})).toBe(undefined); | ||
| { | ||
| // Type narrowing - preserves object type | ||
| const obj = castType<{ a: number }>({ a: 1 }); | ||
| const result = toNonEmptyPlainObject(obj); | ||
| if (result) { | ||
| const check: { a: number } = result; | ||
| expect(check).toEqual({ a: 1 }); | ||
| } | ||
| } | ||
| { | ||
| // Type narrowing - undefined input returns undefined | ||
| const obj = castType<{ a: number } | undefined>(undefined); | ||
| const result: { a: number } | undefined = toNonEmptyPlainObject(obj); | ||
| expect(result).toBe(undefined); | ||
| } | ||
| { | ||
| // Type narrowing - empty object returns undefined | ||
| const obj = castType<{ a?: number }>({}); | ||
| const result = toNonEmptyPlainObject(obj); | ||
| expect(result).toBe(undefined); | ||
| } | ||
| { | ||
| // type narrowing - non-object type returns PlainObject | undefined | ||
| const obj = castType<number>(42); | ||
| const result: PlainObject | undefined = toNonEmptyPlainObject(obj); | ||
| expect(result).toBe(undefined); | ||
| } | ||
| }); | ||
@@ -541,2 +917,32 @@ | ||
| expect(isNonEmptyJSONObject([])).toBe(false); | ||
| expect(isNonEmptyJSONObject({ a: undefined })).toBe(false); | ||
| { | ||
| // Type narrowing - unknown input narrows to PlainObject | ||
| const obj = castType<unknown>({ a: 1 }); | ||
| if (isNonEmptyJSONObject(obj)) { | ||
| const check: PlainObject = obj; | ||
| expect(check).toEqual({ a: 1 }); | ||
| } | ||
| } | ||
| { | ||
| // Type narrowing - object with only undefined values returns false | ||
| const obj = castType<{ a?: number }>({ a: undefined }); | ||
| if (isNonEmptyJSONObject(obj)) { | ||
| const check: PlainObject = obj; | ||
| expect(check).toEqual({}); | ||
| } else { | ||
| expect(obj).toEqual({ a: undefined }); | ||
| } | ||
| } | ||
| { | ||
| // Type narrowing - excludes array | ||
| const obj = castType<{ a: number } | number[]>({ a: 1 }); | ||
| if (isNonEmptyJSONObject(obj)) { | ||
| const check: PlainObject = obj; | ||
| expect(check).toEqual({ a: 1 }); | ||
| } | ||
| } | ||
| }); | ||
@@ -547,2 +953,26 @@ | ||
| expect(toNonEmptyJSONObject({})).toBe(undefined); | ||
| { | ||
| // Type narrowing - preserves object type | ||
| const obj = castType<{ a: number }>({ a: 1 }); | ||
| const result = toNonEmptyJSONObject(obj); | ||
| if (result) { | ||
| const check: { a: number } = result; | ||
| expect(check).toEqual({ a: 1 }); | ||
| } | ||
| } | ||
| { | ||
| // Type narrowing - undefined input returns undefined | ||
| const obj = castType<{ a: number } | undefined>(undefined); | ||
| const result: { a: number } | undefined = toNonEmptyJSONObject(obj); | ||
| expect(result).toBe(undefined); | ||
| } | ||
| { | ||
| // Type narrowing - object with only undefined values returns undefined | ||
| const obj = castType<{ a: number; b?: string }>({ a: undefined as unknown as number }); | ||
| const result = toNonEmptyJSONObject(obj); | ||
| expect(result).toBe(undefined); | ||
| } | ||
| }); | ||
@@ -552,5 +982,33 @@ | ||
| expect(toPlainObjectOf({ a: true, b: false }, isTrue)).toEqual({ a: true }); | ||
| // @ts-expect-error type mismatched | ||
| expect(toPlainObjectOf({ a: 1, b: 2 }, isTrue)).toBe(undefined); | ||
| expect(toPlainObjectOf({ a: 1, b: 2 }, isTruthy)).toEqual({ a: 1, b: 2 }); | ||
| expect(toPlainObjectOf(undefined, isTrue)).toBe(undefined); | ||
| { | ||
| // Type narrowing - result type matches predicate | ||
| const obj = castType<{ a: unknown; b: unknown }>({ a: "hello", b: 123 }); | ||
| const result = toPlainObjectOf(obj, isString); | ||
| if (result) { | ||
| const check: { [key: PropertyKey]: string } = result; | ||
| expect(check).toEqual({ a: "hello" }); | ||
| } | ||
| } | ||
| { | ||
| // Type narrowing - filters to number type | ||
| const obj = castType<{ a: unknown; b: unknown }>({ a: 1, b: "hello" }); | ||
| const result = toPlainObjectOf(obj, isNumber); | ||
| if (result) { | ||
| const check: { [key: PropertyKey]: number } = result; | ||
| expect(check).toEqual({ a: 1 }); | ||
| } | ||
| } | ||
| { | ||
| // Type narrowing - returns undefined when no values match | ||
| const obj = castType<{ a: unknown; b: unknown }>({ a: "hello", b: "world" }); | ||
| const result = toPlainObjectOf(obj, isNumber); | ||
| expect(result).toBe(undefined); | ||
| } | ||
| }); | ||
@@ -557,0 +1015,0 @@ |
+44
-35
| const _ = undefined; | ||
| export type _ = undefined; | ||
| /** Map `never` to a different type */ | ||
| export type MapNeverTo<T, U> = [T, U][T extends any ? 0 : 1]; | ||
| /** Returns `true` if `x` is not `undefined`. */ | ||
@@ -17,10 +20,10 @@ export const isDefined = <T>(x: T | undefined): x is T => x !== _; | ||
| export interface IsFalsy { | ||
| (x: unknown): x is Falsy; | ||
| <T>(x: T): x is Extract<T, Falsy>; | ||
| } | ||
| export type ExtractFalsy<T> = MapNeverTo<Extract<T, Falsy>, Falsy>; | ||
| /** Returns `true` if `Boolean(x)` is `false`. */ | ||
| export const isFalsy: IsFalsy = (x: unknown): x is Falsy => !x; | ||
| export const isFalsy = <T>(x: T): x is ExtractFalsy<T> => !x; | ||
| /** Returns `x` if `Boolean(x)` is `false`, otherwise returns `undefined`. */ | ||
| export const toFalsy = <T>(x: T): ExtractFalsy<T> | _ => (isFalsy(x) ? x : _); | ||
| /** Returns `true` if `Boolean(x)` is `true`. */ | ||
@@ -61,25 +64,29 @@ export const isTruthy = <T>(x: T): x is Exclude<T, Falsy> => !!x; | ||
| export type NormalizeArrayType<T> = T extends readonly unknown[] ? T : unknown[]; | ||
| export type ExtractArray<T> = MapNeverTo<Extract<T, readonly unknown[]>, unknown[]>; | ||
| export type ExtractArray<T> = NormalizeArrayType<Extract<T, readonly unknown[]>>; | ||
| export const isArray = Array.isArray as <T>(x: T) => x is ExtractArray<T>; | ||
| export interface IsArray { | ||
| (x: unknown): x is unknown[]; | ||
| <T>(x: T): x is T extends readonly unknown[] ? T : never; | ||
| } | ||
| /** Returns `x` if `x` is an array. */ | ||
| export const toArray = <T>(x: T): ExtractArray<T> | _ => (isArray(x) ? x : _); | ||
| export const isArray: IsArray = Array.isArray; | ||
| /** Returns `x` if `x` is an array, otherwise returns `[]` (empty array). */ | ||
| export const asArray = <T>(x: T): ExtractArray<T> => (isArray(x) ? x : ([] as ExtractArray<T>)); | ||
| /** Returns `x` if `x` is an array. */ | ||
| export const toArray = <T>(x: T): ExtractArray<T> | _ => (isArray(x) ? (x as ExtractArray<T>) : _); | ||
| /** Returns `true` if `x` is an array and has at least one element. */ | ||
| export const isNonEmptyArray: IsArray = (x: unknown): x is unknown[] => isArray(x) && x.length > 0; | ||
| export const isNonEmptyArray = <T>(x: T): x is ExtractArray<T> => isArray(x) && x.length > 0; | ||
| /** Returns `x` if `x` is an array and has at least one element, otherwise returns `undefined`. */ | ||
| export const toNonEmptyArray = <T>(x: T): ExtractArray<T> | _ => (isNonEmptyArray(x) ? (x as ExtractArray<T>) : _); | ||
| export const toNonEmptyArray = <T>(x: T): ExtractArray<T> | _ => (isNonEmptyArray(x) ? x : _); | ||
| /** Returns `x` if `x` is an array, otherwise returns `[]` (empty array). */ | ||
| export const asArray = <T>(x: T): ExtractArray<T> => (isArray(x) ? (x as ExtractArray<T>) : ([] as ExtractArray<T>)); | ||
| export type ExtractObject<T> = MapNeverTo<Extract<T, object>, object>; | ||
| /** Returns `true` if `x` is an object (including array) and not null. */ | ||
| export const isObject = <T>(x: T): x is ExtractObject<T> => x !== null && typeof x === "object"; | ||
| /** Returns `x` if `x` is an object (including array). */ | ||
| export const toObject = <T>(x: T): ExtractObject<T> | _ => (isObject(x) ? x : _); | ||
| /** Returns `x` if `x` is an object (including array), otherwise returns `{}` (empty object). */ | ||
| export const asObject = <T>(x: T): ExtractObject<T> => (isObject(x) ? x : ({} as ExtractObject<T>)); | ||
| export interface PlainObject { | ||
@@ -89,30 +96,29 @@ [key: PropertyKey]: unknown; | ||
| /** Returns `true` if `x` is an object (including array) and not null. */ | ||
| export const isObject = (x: unknown): x is object => x !== null && typeof x === "object"; | ||
| export type ExtractPlainObject<T> = MapNeverTo<Exclude<Extract<T, object>, readonly unknown[]>, PlainObject>; | ||
| /** Returns `x` if `x` is an object (including array), otherwise returns `{}` (empty object). */ | ||
| export const asObject = (x: unknown): object => (isObject(x) ? x : {}); | ||
| /** Returns `true` if `x` is a plain object (shallow test), not `null` or array. */ | ||
| export const isPlainObject = (x: unknown): x is PlainObject => isObject(x) && !isArray(x); | ||
| export const isPlainObject = <T>(x: T): x is ExtractPlainObject<T> => isObject(x) && !isArray(x); | ||
| /** Returns `x` if `x` is a plain object. */ | ||
| export const toPlainObject = (x: unknown): PlainObject | _ => (isPlainObject(x) ? x : _); | ||
| export const toPlainObject = <T>(x: T): ExtractPlainObject<T> | _ => (isPlainObject(x) ? x : _); | ||
| /** Returns `x` if `x` is a plain object, otherwise returns `{}` (empty object). */ | ||
| export const asPlainObject = (x: unknown): PlainObject => (isPlainObject(x) ? x : {}); | ||
| export const asPlainObject = <T>(x: T): ExtractPlainObject<T> => (isPlainObject(x) ? x : ({} as ExtractPlainObject<T>)); | ||
| /** Returns `true` if `x` is a plain object and has at least one key. */ | ||
| export const isNonEmptyPlainObject = (x: unknown): x is PlainObject => isPlainObject(x) && Object.keys(x).length > 0; | ||
| export const isNonEmptyPlainObject = <T>(x: T): x is ExtractPlainObject<T> => | ||
| isPlainObject(x) && Object.keys(x).length > 0; | ||
| /** Returns `x` if `x` is a plain object and has at least one key. */ | ||
| export const toNonEmptyPlainObject = <T extends PlainObject>(x: T): T | _ => (isNonEmptyPlainObject(x) ? x : _); | ||
| export const toNonEmptyPlainObject = <T>(x: T): ExtractPlainObject<T> | _ => (isNonEmptyPlainObject(x) ? x : _); | ||
| /** Returns `true` if `x` is a plain object and has at least one key with non-undefined value. */ | ||
| export const isNonEmptyJSONObject = (x: unknown): x is PlainObject => | ||
| export const isNonEmptyJSONObject = <T>(x: T): x is ExtractPlainObject<T> => | ||
| isPlainObject(x) && Object.values(x).some(isDefined); | ||
| /** Returns `x` if `x` is a plain object and has at least one key with non-undefined value, otherwise returns `undefined`. */ | ||
| export const toNonEmptyJSONObject = <T extends PlainObject>(x: T): T | _ => (isNonEmptyJSONObject(x) ? x : _); | ||
| export const toNonEmptyJSONObject = <T>(x: T): ExtractPlainObject<T> | _ => (isNonEmptyJSONObject(x) ? x : _); | ||
| type ExtractPlainObjectValue<T> = ExtractPlainObject<T>[keyof ExtractPlainObject<T>]; | ||
| /** | ||
@@ -122,3 +128,6 @@ * Creates an object from `x` with keys `k` if `f(x[k])` returns `true`. | ||
| */ | ||
| export const toPlainObjectOf = <T>(x: unknown, f: (v: unknown) => v is T): { [key: PropertyKey]: T } | _ => { | ||
| export const toPlainObjectOf = <T, U extends ExtractPlainObjectValue<T>>( | ||
| x: T, | ||
| f: (v: ExtractPlainObjectValue<T>) => v is U | ||
| ): Record<PropertyKey, U> | _ => { | ||
| if (isPlainObject(x)) { | ||
@@ -128,6 +137,6 @@ let index = -1; | ||
| let length = props.length; | ||
| let result: { [key: PropertyKey]: T } | _; | ||
| let result: Record<PropertyKey, U> | _; | ||
| while (++index < length) { | ||
| let key = props[index]; | ||
| let key = props[index] as keyof typeof x; | ||
| let value = x[key]; | ||
@@ -144,3 +153,3 @@ if (f(value)) { | ||
| /** Filter props from object `x` whose values are `true`. */ | ||
| export const toPlainObjectOfTrue = (x: unknown): { [key: PropertyKey]: true } | _ => toPlainObjectOf(x, isTrue); | ||
| export const toPlainObjectOfTrue = (x: unknown): Record<PropertyKey, true> | _ => toPlainObjectOf(x, isTrue); | ||
@@ -147,0 +156,0 @@ /** |
73169
41.5%1328
45.14%190
265.38%