v0.1.1
🦕 A lightweight utility pack for handling unknown type
Attributes
Very Popular
Repository
Current version released
4 years ago
Versions
- v3.18.1Latest
- v3.18.0
- v3.17.3
- v3.17.2
- v3.17.1
- v3.17.0
- v3.16.3
- v3.16.2
- v3.16.1
- v3.16.0
- v3.15.0
- v3.14.1
- v3.14.0
- v3.13.0
- v3.12.1
- v3.12.0
- v3.11.0
- v3.10.0
- v3.9.0
- v3.8.0
- v3.7.0
- v3.6.0
- v3.5.1
- v3.5.1
- v3.5.0
- v3.4.0
- v3.3.1
- v3.3.1
- v3.3.0
- v3.2.0
- v3.1.1
- v3.1.0
- v3.0.1
- v3.0.0
- v2.1.1
- v2.1.0
- v2.0.0
- v1.2.1
- v1.2.0
- v1.1.4
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.0
- v0.1.1
- v0.1.0
unknownutil-deno
A utility pack for handling unknown type.
Usage
isXXXXX
The unknownutil provides the following predicate functions
isString(x: unknown): x is stringisNumber(x: unknown): x is numberisArray<T extends unknown>(x: unknown, pred?: Predicate<T>): x is T[]isObject<T extends unknown>(x: unknown, pred?: Predicate<T>): x is Record<string, T>isFunction(x: unknown): x is (...args: unknown[]) => unknownisNull(x: unknown): x is nullisUndefined(x: unknown): x is undefinedisNone(x: unknown): x is null | undefined
For example:
import { isString } from "https://deno.land/x/unknownutil/mod.ts";
const a: unknown = "Hello";
if (isString(a)) {
// 'a' is 'string' in this block
}Additionally, isArray and isObject supports an inner predicate function to
predicate x more precisely like:
import { isArray, isString } from "https://deno.land/x/unknownutil/mod.ts";
const a: unknown = ["a", "b", "c"];
if (isArray(a)) {
// 'a' is 'unknown[]' in this block
}
if (isArray(a, isString)) {
// 'a' is 'string[]' in this block
}ensureXXXXX
The unknownutil provides the following ensure functions which will raise
EnsureError when a given x is not expected type.
ensureString(x: unknown): assert x is stringensureNumber(x: unknown): assert x is stringensureArray<T extends unknown>(x: unknown, pred?: Predicate<T>): assert x is T[]ensureObject<T extends unknown>(x: unknown, pred?: Predicate<T>): x ensure Record<string, T>ensureFunction(x: unknown): x ensure (...args: unknown[]) => unknownensureNull(x: unknown): x ensure nullensureUndefined(x: unknown): x ensure undefinedensureNone(x: unknown): x ensure null | undefined
For example:
import { ensureString } from "https://deno.land/x/unknownutil/mod.ts";
const a: unknown = "Hello";
ensureString(a); // Now 'a' is 'string'
const b: unknown = 0;
ensureString(b); // Raise EnsureError on above while 'b' is not stringAdditionally, ensureArray and ensureObject supports an inner predicate
function to predicate x more precisely like:
import { ensureArray, isString } from "https://deno.land/x/unknownutil/mod.ts";
const a: unknown = ["a", "b", "c"];
ensureArray(a); // Now 'a' is 'unknown[]'
ensureArray(a, isString); // Now 'a' is 'string[]'
const b: unknown = [0, 1, 2];
ensureArray(b); // Now 'b' is 'unknown[]'
ensureArray(b, isString); // Raise EnsureError on above while 'b' is not string arrayLicense
The code follows MIT license written in LICENSE. Contributors need to agree that any modifications sent in this repository follow the license.