|
| 1 | +# unknownutil-deno |
| 2 | + |
| 3 | +[](https://deno.land/x/unknownutil) |
| 4 | +[](https://doc.deno.land/https/deno.land/x/unknownutil/mod.ts) |
| 5 | +[](https://github.com/lambdalisue/unknownutil-deno/actions?query=workflow%3ATest) |
| 6 | + |
| 7 | +A utility pack for handling `unknown` type. |
| 8 | + |
| 9 | +[deno]: https://deno.land/ |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +### isXXXXX |
| 14 | + |
| 15 | +The `unknownutil` provides the following predicate functions |
| 16 | + |
| 17 | +- `isString(x: unknown): x is string` |
| 18 | +- `isNumber(x: unknown): x is number` |
| 19 | +- `isArray<T extends unknown>(x: unknown, pred?: Predicate<T>): x is T[]` |
| 20 | +- `isObject<T extends unknown>(x: unknown, pred?: Predicate<T>): x is Record<string, T>` |
| 21 | +- `isFunction(x: unknown): x is (...args: unknown[]) => unknown` |
| 22 | +- `isNull(x: unknown): x is null` |
| 23 | +- `isUndefined(x: unknown): x is undefined` |
| 24 | +- `isNone(x: unknown): x is null | undefined` |
| 25 | + |
| 26 | +For example: |
| 27 | + |
| 28 | +```typescript |
| 29 | +import { isString } from "https://deno.land/x/unknownutil/mod.ts"; |
| 30 | + |
| 31 | +const a: unknown = "Hello"; |
| 32 | + |
| 33 | +if (isString(a)) { |
| 34 | + // 'a' is 'string' in this block |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +Additionally, `isArray` and `isObject` supports an inner predicate function to |
| 39 | +predicate `x` more precisely like: |
| 40 | + |
| 41 | +```typescript |
| 42 | +import { isArray, isString } from "https://deno.land/x/unknownutil/mod.ts"; |
| 43 | + |
| 44 | +const a: unknown = ["a", "b", "c"]; |
| 45 | + |
| 46 | +if (isArray(a)) { |
| 47 | + // 'a' is 'unknown[]' in this block |
| 48 | +} |
| 49 | + |
| 50 | +if (isArray(a, isString)) { |
| 51 | + // 'a' is 'string[]' in this block |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +### ensureXXXXX |
| 56 | + |
| 57 | +The `unknownutil` provides the following ensure functions which will raise |
| 58 | +`EnsureError` when a given `x` is not expected type. |
| 59 | + |
| 60 | +- `ensureString(x: unknown): assert x is string` |
| 61 | +- `ensureNumber(x: unknown): assert x is string` |
| 62 | +- `ensureArray<T extends unknown>(x: unknown, pred?: Predicate<T>): assert x is T[]` |
| 63 | +- `ensureObject<T extends unknown>(x: unknown, pred?: Predicate<T>): x ensure Record<string, T>` |
| 64 | +- `ensureFunction(x: unknown): x ensure (...args: unknown[]) => unknown` |
| 65 | +- `ensureNull(x: unknown): x ensure null` |
| 66 | +- `ensureUndefined(x: unknown): x ensure undefined` |
| 67 | +- `ensureNone(x: unknown): x ensure null | undefined` |
| 68 | + |
| 69 | +For example: |
| 70 | + |
| 71 | +```typescript |
| 72 | +import { ensureString } from "https://deno.land/x/unknownutil/mod.ts"; |
| 73 | + |
| 74 | +const a: unknown = "Hello"; |
| 75 | +ensureString(a); // Now 'a' is 'string' |
| 76 | + |
| 77 | +const b: unknown = 0; |
| 78 | +ensureString(b); // Raise EnsureError on above while 'b' is not string |
| 79 | +``` |
| 80 | + |
| 81 | +Additionally, `ensureArray` and `ensureObject` supports an inner predicate |
| 82 | +function to predicate `x` more precisely like: |
| 83 | + |
| 84 | +```typescript |
| 85 | +import { ensureArray, isString } from "https://deno.land/x/unknownutil/mod.ts"; |
| 86 | + |
| 87 | +const a: unknown = ["a", "b", "c"]; |
| 88 | +ensureArray(a); // Now 'a' is 'unknown[]' |
| 89 | +ensureArray(a, isString); // Now 'a' is 'string[]' |
| 90 | + |
| 91 | +const b: unknown = [0, 1, 2]; |
| 92 | +ensureArray(b); // Now 'b' is 'unknown[]' |
| 93 | +ensureArray(b, isString); // Raise EnsureError on above while 'b' is not string array |
| 94 | +``` |
| 95 | + |
| 96 | +## License |
| 97 | + |
| 98 | +The code follows MIT license written in [LICENSE](./LICENSE). Contributors need |
| 99 | +to agree that any modifications sent in this repository follow the license. |
0 commit comments