Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | 1x 4x 11x 3x 3x 3x 16x 11x 6x 3x 9x 3x 3x 19x 3x 3x 6x 3x 1x 8x 22x 8x 8x 31x 3x 25x 3x 3x 5x 2x 1x 5x 2x 1x 6x 5x 5x 5x 5x 10x 10x 10x 4x 5x 2x 1x 4x 3x 2x 2x 1x | const _ = undefined;
export type _ = undefined;
/** Returns `true` if `x` is not `undefined`. */
export const isDefined = <T>(x: T | undefined): x is T => x !== _;
export const isTrue = (x: unknown): x is true => x === true;
/** Returns `true` if `x` is `true`, otherwise returns `undefined`. */
export const toTrue = (x: unknown): true | _ => (x === true ? true : _);
/** Returns `true` if `x` is `true`, otherwise returns `false`. */
export const asTrue = (x: unknown): boolean => (x === true ? x : false);
export type Falsy = false | null | undefined | 0 | "";
export interface IsFalsy {
(x: unknown): x is Falsy;
<T>(x: T): x is Extract<T, Falsy>;
}
/** Returns `true` if `Boolean(x)` is `false`. */
export const isFalsy: IsFalsy = (x: unknown): x is Falsy => !x;
/** Returns `true` if `Boolean(x)` is `true`. */
export const isTruthy = <T>(x: T): x is Exclude<T, Falsy> => !!x;
/** Returns `x` if `Boolean(x)` is `true`, otherwise returns `undefined`. */
export const toTruthy = <T>(x: T): Exclude<T, Falsy> | _ => (isTruthy(x) ? x : _);
export const isBoolean = (x: unknown): x is boolean => x === true || x === false;
/** Returns `x` if `x` is `true` or `false`, otherwise returns `undefined`. */
export const toBoolean = (x: unknown): boolean | _ => (isBoolean(x) ? x : _);
/** Returns `true` if `x` is a number, returns `false` if `x` is `NaN` or other value. */
export const isNumber = (x: unknown): x is number => typeof x === "number" && x === x;
/** Returns `x` if `x` is a number, `NaN` and other value will be coerced to `undefined`. */
export const toNumber = (x: unknown): number | _ => (isNumber(x) ? x : _);
/** Returns `x` if `x` is a number, `NaN` and other value will be coerced to `0`. */
export const asNumber = (x: unknown): number => (isNumber(x) ? x : 0);
/** Returns `true` if `x` is a string. */
export const isString = (x: unknown): x is string => typeof x === "string";
/** Returns `x` if `x` is a string, otherwise returns `undefined`. */
export const toString = (x: unknown): string | _ => (isString(x) ? x : _);
/** Returns `x` if `x` is a string, otherwise returns `''` (empty string). */
export const asString = (x: unknown): string => (isString(x) ? x : "");
/** Returns `true` if `x` is a string and not `''`. */
export const isNonEmptyString = (x: unknown): x is string => isString(x) && x !== "";
/** Returns `x` if `x` is a string and not empty, otherwise returns `undefined`. */
export const toNonEmptyString = (x: unknown): string | _ => (isNonEmptyString(x) ? x : _);
export type ExtractArray<T> = T extends readonly unknown[] ? T : unknown[];
export interface IsArray {
(x: unknown): x is unknown[];
<T>(x: T): x is T extends readonly unknown[] ? T : never;
}
export const isArray: IsArray = Array.isArray;
/** 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;
/** 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>) : _);
/** 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 interface PlainObject {
[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";
/** 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);
/** Returns `x` if `x` is a plain object. */
export const toPlainObject = (x: unknown): PlainObject | _ => (isPlainObject(x) ? x : _);
/** Returns `x` if `x` is a plain object, otherwise returns `{}` (empty object). */
export const asPlainObject = (x: unknown): PlainObject => (isPlainObject(x) ? x : {});
/** 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;
/** 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 : _);
/** 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 =>
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 : _);
/**
* Creates an object from `x` with keys `k` if `f(x[k])` returns `true`.
* If `x` is not a plain object, or there's no passed props, returns `undefined`.
*/
export const toPlainObjectOf = <T>(x: unknown, f: (v: unknown) => v is T): { [key: PropertyKey]: T } | _ => {
if (isPlainObject(x)) {
let index = -1;
let props = Object.keys(x);
let length = props.length;
let result: { [key: PropertyKey]: T } | _;
while (++index < length) {
let key = props[index];
let value = x[key];
if (f(value)) {
(result ??= {})[key] = value;
}
}
return result;
}
};
/** Filter props from object `x` whose values are `true`. */
export const toPlainObjectOfTrue = (x: unknown): { [key: PropertyKey]: true } | _ => toPlainObjectOf(x, isTrue);
/**
* Returns `x` if `x` is string, otherwise returns `''` (empty string) if
* `x` is `null` or `undefined`, otherwise returns `JSON.stringify(x)`.
* This is very useful to show a value inside a React component.
*/
export const print = (x: unknown): string => {
if (isString(x)) return x;
if (x == null) return "";
try {
return JSON.stringify(x, null, 2);
} catch {
// Insane case is not handled: x = { toString: () => { throw x } }
return x + "";
}
};
|