@softwareventures/nullable
Advanced tools
+1
-0
@@ -9,2 +9,3 @@ /** Tests if the specified value is null or undefined. | ||
| export declare function isNotNull<T>(value: T | null | undefined): value is T; | ||
| export declare function notNull<T>(value: T | null | undefined): T; | ||
| /** Returns the specified value, or the default value if the specified value | ||
@@ -11,0 +12,0 @@ * is null or undefined. |
+8
-1
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.mapNullFn = exports.mapNull = exports.mapFn = exports.map = exports.defaultValueFn = exports.defaultValue = exports.isNotNull = exports.isNull = void 0; | ||
| exports.mapNullFn = exports.mapNull = exports.mapFn = exports.map = exports.defaultValueFn = exports.defaultValue = exports.notNull = exports.isNotNull = exports.isNull = void 0; | ||
| /** Tests if the specified value is null or undefined. | ||
@@ -18,2 +18,9 @@ * | ||
| exports.isNotNull = isNotNull; | ||
| function notNull(value) { | ||
| if (value == null) { | ||
| throw new TypeError(`value is ${String(value)}`); | ||
| } | ||
| return value; | ||
| } | ||
| exports.notNull = notNull; | ||
| /** Returns the specified value, or the default value if the specified value | ||
@@ -20,0 +27,0 @@ * is null or undefined. |
+1
-1
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;;AAAA;;8DAE8D;AAC9D,SAAgB,MAAM,CAAI,KAAc;IACpC,OAAO,KAAK,IAAI,IAAI,CAAC;AACzB,CAAC;AAFD,wBAEC;AAED;;8DAE8D;AAC9D,SAAgB,SAAS,CAAI,KAA2B;IACpD,OAAO,KAAK,IAAI,IAAI,CAAC;AACzB,CAAC;AAFD,8BAEC;AAED;;;;8BAI8B;AAC9B,SAAgB,YAAY,CAAI,QAA8B,EAAE,YAAe;IAC3E,OAAO,QAAQ,aAAR,QAAQ,cAAR,QAAQ,GAAI,YAAY,CAAC;AACpC,CAAC;AAFD,oCAEC;AAED;;;;gCAIgC;AAChC,SAAgB,cAAc,CAAI,YAAe;IAC7C,OAAO,QAAQ,CAAC,EAAE,CAAC,QAAQ,aAAR,QAAQ,cAAR,QAAQ,GAAI,YAAY,CAAC;AAChD,CAAC;AAFD,wCAEC;AAED;;;wCAGwC;AACxC,SAAgB,GAAG,CAAO,QAA8B,EAAE,CAAoB;;IAC1E,OAAO,QAAQ,IAAI,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,MAAA,CAAC,CAAC,QAAQ,CAAC,mCAAI,IAAI,CAAC;AACzD,CAAC;AAFD,kBAEC;AAED;;;;;;;qCAOqC;AACrC,SAAgB,KAAK,CAAO,CAAoB;IAC5C,OAAO,QAAQ,CAAC,EAAE,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC;AACxC,CAAC;AAFD,sBAEC;AAED;;;;2BAI2B;AAC3B,SAAgB,OAAO,CAAW,QAA8B,EAAE,CAAU;IACxE,OAAO,QAAQ,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC;AAC7C,CAAC;AAFD,0BAEC;AAED;;;;;2BAK2B;AAC3B,SAAgB,SAAS,CAAW,CAAU;IAC1C,OAAO,QAAQ,CAAC,EAAE,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC;AAC5C,CAAC;AAFD,8BAEC","sourcesContent":["/** Tests if the specified value is null or undefined.\n *\n * Useful as the predicate of filter functions and similar. */\nexport function isNull<T>(value: unknown): value is null | undefined {\n return value == null;\n}\n\n/** Tests if the specified value is null or undefined.\n *\n * Useful as the predicate of filter functions and similar. */\nexport function isNotNull<T>(value: T | null | undefined): value is T {\n return value != null;\n}\n\n/** Returns the specified value, or the default value if the specified value\n * is null or undefined.\n *\n * If the default value is expensive to compute, consider using\n * {@link mapNull} instead. */\nexport function defaultValue<T>(nullable: T | undefined | null, defaultValue: T): T {\n return nullable ?? defaultValue;\n}\n\n/** Returns a function that returns the specified value, or the default value\n * if the specified value is null or undefined.\n *\n * If the default value is expensive to compute, consider using\n * {@link mapNullFn} instead. */\nexport function defaultValueFn<T>(defaultValue: T): (nullable: T | undefined | null) => T {\n return nullable => nullable ?? defaultValue;\n}\n\n/** If the specified value is null or undefined, returns null.\n *\n * Otherwise, passes the specified value to the provided function and returns\n * the return value of that function. */\nexport function map<T, U>(nullable: T | undefined | null, f: (element: T) => U): U | null {\n return nullable == null ? null : f(nullable) ?? null;\n}\n\n/** Returns a function that takes a nullable value as its argument.\n *\n * If the function is called with null or undefined, it returns null.\n *\n * Otherwise, the argument is passed to the callback `f` and the\n * return value of `f` is returned.\n *\n * Curried variant of {@link map}. */\nexport function mapFn<T, U>(f: (element: T) => U): (nullable: T | undefined | null) => U | null {\n return nullable => map(nullable, f);\n}\n\n/** Returns the specified value or, if the value is null or undefined, calls\n * the provided callback and returns the result of that function call.\n *\n * Useful as an alternative to {@link defaultValue} if the default value is\n * expensive to compute. */\nexport function mapNull<T, U = T>(nullable: T | undefined | null, f: () => U): T | U {\n return nullable == null ? f() : nullable;\n}\n\n/** Returns a function that returns the specified value or, if the value is\n * null or undefined, calls the provided callback and returns the result of\n * that function call.\n *\n * Useful as an alternative to {@link defaultValueFn} if the default value is\n * expensive to compute. */\nexport function mapNullFn<T, U = T>(f: () => U): (nullable: T | undefined | null) => T | U {\n return nullable => mapNull(nullable, f);\n}\n"]} | ||
| {"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;;AAAA;;8DAE8D;AAC9D,SAAgB,MAAM,CAAI,KAAc;IACpC,OAAO,KAAK,IAAI,IAAI,CAAC;AACzB,CAAC;AAFD,wBAEC;AAED;;8DAE8D;AAC9D,SAAgB,SAAS,CAAI,KAA2B;IACpD,OAAO,KAAK,IAAI,IAAI,CAAC;AACzB,CAAC;AAFD,8BAEC;AAED,SAAgB,OAAO,CAAI,KAA2B;IAClD,IAAI,KAAK,IAAI,IAAI,EAAE;QACf,MAAM,IAAI,SAAS,CAAC,YAAY,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;KACpD;IAED,OAAO,KAAK,CAAC;AACjB,CAAC;AAND,0BAMC;AAED;;;;8BAI8B;AAC9B,SAAgB,YAAY,CAAI,QAA8B,EAAE,YAAe;IAC3E,OAAO,QAAQ,aAAR,QAAQ,cAAR,QAAQ,GAAI,YAAY,CAAC;AACpC,CAAC;AAFD,oCAEC;AAED;;;;gCAIgC;AAChC,SAAgB,cAAc,CAAI,YAAe;IAC7C,OAAO,QAAQ,CAAC,EAAE,CAAC,QAAQ,aAAR,QAAQ,cAAR,QAAQ,GAAI,YAAY,CAAC;AAChD,CAAC;AAFD,wCAEC;AAED;;;wCAGwC;AACxC,SAAgB,GAAG,CAAO,QAA8B,EAAE,CAAoB;;IAC1E,OAAO,QAAQ,IAAI,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,MAAA,CAAC,CAAC,QAAQ,CAAC,mCAAI,IAAI,CAAC;AACzD,CAAC;AAFD,kBAEC;AAED;;;;;;;qCAOqC;AACrC,SAAgB,KAAK,CAAO,CAAoB;IAC5C,OAAO,QAAQ,CAAC,EAAE,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC;AACxC,CAAC;AAFD,sBAEC;AAED;;;;2BAI2B;AAC3B,SAAgB,OAAO,CAAW,QAA8B,EAAE,CAAU;IACxE,OAAO,QAAQ,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC;AAC7C,CAAC;AAFD,0BAEC;AAED;;;;;2BAK2B;AAC3B,SAAgB,SAAS,CAAW,CAAU;IAC1C,OAAO,QAAQ,CAAC,EAAE,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC;AAC5C,CAAC;AAFD,8BAEC","sourcesContent":["/** Tests if the specified value is null or undefined.\n *\n * Useful as the predicate of filter functions and similar. */\nexport function isNull<T>(value: unknown): value is null | undefined {\n return value == null;\n}\n\n/** Tests if the specified value is null or undefined.\n *\n * Useful as the predicate of filter functions and similar. */\nexport function isNotNull<T>(value: T | null | undefined): value is T {\n return value != null;\n}\n\nexport function notNull<T>(value: T | null | undefined): T {\n if (value == null) {\n throw new TypeError(`value is ${String(value)}`);\n }\n\n return value;\n}\n\n/** Returns the specified value, or the default value if the specified value\n * is null or undefined.\n *\n * If the default value is expensive to compute, consider using\n * {@link mapNull} instead. */\nexport function defaultValue<T>(nullable: T | undefined | null, defaultValue: T): T {\n return nullable ?? defaultValue;\n}\n\n/** Returns a function that returns the specified value, or the default value\n * if the specified value is null or undefined.\n *\n * If the default value is expensive to compute, consider using\n * {@link mapNullFn} instead. */\nexport function defaultValueFn<T>(defaultValue: T): (nullable: T | undefined | null) => T {\n return nullable => nullable ?? defaultValue;\n}\n\n/** If the specified value is null or undefined, returns null.\n *\n * Otherwise, passes the specified value to the provided function and returns\n * the return value of that function. */\nexport function map<T, U>(nullable: T | undefined | null, f: (element: T) => U): U | null {\n return nullable == null ? null : f(nullable) ?? null;\n}\n\n/** Returns a function that takes a nullable value as its argument.\n *\n * If the function is called with null or undefined, it returns null.\n *\n * Otherwise, the argument is passed to the callback `f` and the\n * return value of `f` is returned.\n *\n * Curried variant of {@link map}. */\nexport function mapFn<T, U>(f: (element: T) => U): (nullable: T | undefined | null) => U | null {\n return nullable => map(nullable, f);\n}\n\n/** Returns the specified value or, if the value is null or undefined, calls\n * the provided callback and returns the result of that function call.\n *\n * Useful as an alternative to {@link defaultValue} if the default value is\n * expensive to compute. */\nexport function mapNull<T, U = T>(nullable: T | undefined | null, f: () => U): T | U {\n return nullable == null ? f() : nullable;\n}\n\n/** Returns a function that returns the specified value or, if the value is\n * null or undefined, calls the provided callback and returns the result of\n * that function call.\n *\n * Useful as an alternative to {@link defaultValueFn} if the default value is\n * expensive to compute. */\nexport function mapNullFn<T, U = T>(f: () => U): (nullable: T | undefined | null) => T | U {\n return nullable => mapNull(nullable, f);\n}\n"]} |
+1
-1
| { | ||
| "name": "@softwareventures/nullable", | ||
| "version": "1.2.2", | ||
| "version": "1.3.0", | ||
| "description": "Pure functional utilities for nullable types", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
12267
5.13%130
6.56%