@saberhq/option-utils
Version:
Utilities for handling optional values in TypeScript.
66 lines • 1.68 kB
JavaScript
/**
* [[include:option-utils/README.md]]
* @module
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.exists = exports.isNotUndefined = exports.isNotNull = exports.mapSome = exports.mapN = void 0;
/**
* Applies a function to a list of null/undefined values, unwrapping the null/undefined value or passing it through.
*/
const mapN = (fn, ...args) => {
if (!args.every((arg) => arg !== undefined)) {
return undefined;
}
if (!args.every((arg) => arg !== null)) {
return null;
}
return fn(...args);
};
exports.mapN = mapN;
/**
* Applies a function to a null/undefined inner value if it is null or undefined,
* otherwise returns null/undefined.
*
* For consistency reasons, we recommend just using {@link mapN} in all cases.
*
* @deprecated use {@link mapN}
* @param obj
* @param fn
* @returns
*/
const mapSome = (obj, fn) => ((0, exports.exists)(obj) ? fn(obj) : obj);
exports.mapSome = mapSome;
/**
* Checks to see if the provided value is not null.
*
* Useful for preserving types in filtering out non-null values.
*
* @param value
* @returns
*/
const isNotNull = (value) => {
return value !== null;
};
exports.isNotNull = isNotNull;
/**
* Checks to see if the provided value is not undefined.
*
* @param value
* @returns
*/
const isNotUndefined = (value) => {
return value !== undefined;
};
exports.isNotUndefined = isNotUndefined;
/**
* Checks to see if the provided value is not null or undefined.
*
* @param value
* @returns
*/
const exists = (value) => {
return value !== null && value !== undefined;
};
exports.exists = exists;
//# sourceMappingURL=index.js.map
;