UNPKG

@js-data-tools/js-helpers

Version:

A set of JavaScript / TypeScript helper functions for parsing, converting, transforming and formatting data.

109 lines (108 loc) 3.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.not = exports.or = exports.and = exports.isDefaultValue = exports.isEmptyValue = exports.isNonEmptyObject = exports.isEmptyObject = exports.isNonEmptyMap = exports.isEmptyMap = exports.isNonEmptySet = exports.isEmptySet = exports.isNonEmptyArray = exports.isEmptyArray = exports.notNullOrUndefined = exports.isNullOrUndefined = exports.isUndefined = exports.alwaysTrue = void 0; function alwaysTrue(input) { return true; } exports.alwaysTrue = alwaysTrue; function isUndefined(input) { return input === undefined; } exports.isUndefined = isUndefined; function isNullOrUndefined(input) { return input === undefined || input === null; } exports.isNullOrUndefined = isNullOrUndefined; function notNullOrUndefined(input) { return input !== undefined && input !== null; } exports.notNullOrUndefined = notNullOrUndefined; function isEmptyArray(input) { return !input || (Array.isArray(input) && !input.length); } exports.isEmptyArray = isEmptyArray; function isNonEmptyArray(input) { return Array.isArray(input) && input.length > 0; } exports.isNonEmptyArray = isNonEmptyArray; function isEmptySet(input) { return !input || !input.size; } exports.isEmptySet = isEmptySet; function isNonEmptySet(input) { return !!input && input.size > 0; } exports.isNonEmptySet = isNonEmptySet; function isEmptyMap(input) { return !input || !input.size; } exports.isEmptyMap = isEmptyMap; function isNonEmptyMap(input) { return !!input && input.size > 0; } exports.isNonEmptyMap = isNonEmptyMap; function isEmptyObject(input) { return !input || !Object.keys(input).length; } exports.isEmptyObject = isEmptyObject; function isNonEmptyObject(input) { return !!input && Object.keys(input).length > 0; } exports.isNonEmptyObject = isNonEmptyObject; function isEmptyValue(value) { if (value === null || value === undefined) { return true; } switch (typeof value) { case "string": return !value; case "object": if (Array.isArray(value)) { return !value.length; } return !Object.keys(value).length; } return false; } exports.isEmptyValue = isEmptyValue; function isDefaultValue(value) { if (value === null || value === undefined) { return true; } switch (typeof value) { case "object": if (Array.isArray(value)) { return !value.length; } return !Object.keys(value).length; default: return !value; } } exports.isDefaultValue = isDefaultValue; function and(...predicates) { const conditions = predicates?.filter(notNullOrUndefined); if (!conditions || conditions.length === 0) { return alwaysTrue; } if (conditions.length === 1) { return conditions[0]; } return (input) => conditions.every((test) => test(input)); } exports.and = and; function or(...predicates) { const conditions = predicates?.filter(notNullOrUndefined); if (!conditions || conditions.length === 0) { return alwaysTrue; } if (conditions.length === 1) { return conditions[0]; } return (input) => conditions.some((test) => test(input)); } exports.or = or; function not(predicate) { return (input) => !predicate(input); } exports.not = not;