emery
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -5,263 +5,37 @@ 'use strict'; | ||
/** | ||
* Asserts that a condition is `true`, ensuring that whatever condition is being | ||
* checked must be true for the remainder of the containing scope. | ||
* | ||
* @throws when the condition is `false` | ||
* @returns void | ||
*/ | ||
// NOTE: The narrow type of `boolean` instead of something like `unknown` is an | ||
// intentional design decision. The goal is to promote consideration from | ||
// consumers when dealing with potentially ambiguous conditions like `0` or | ||
// `''`, which can introduce "subtle" bugs. | ||
function assert(condition) { | ||
var message = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'Assert failed'; | ||
var assertions_dist_emeryAssertions = require('../assertions/dist/emery-assertions.cjs.dev.js'); | ||
var number = require('./number-1c017a79.cjs.dev.js'); | ||
var guards_dist_emeryGuards = require('../guards/dist/emery-guards.cjs.dev.js'); | ||
var opaques_dist_emeryOpaques = require('../opaques/dist/emery-opaques.cjs.dev.js'); | ||
var object = require('./object-e58860b1.cjs.dev.js'); | ||
if (!condition) { | ||
developmentDebugger(); | ||
throw new TypeError(message); | ||
} | ||
} | ||
/** | ||
* Asserts that allegedly unreachable code has been executed. | ||
* | ||
* @throws always | ||
* @returns void | ||
*/ | ||
function assertNever(condition) { | ||
developmentDebugger(); | ||
throw new Error("Unexpected call to assertNever: '".concat(condition, "'")); | ||
} | ||
/** Pause execution in development to aid debugging. */ | ||
function developmentDebugger() { | ||
if (process.env.NODE_ENV === 'production') { | ||
return; | ||
} // eslint-disable-next-line no-debugger | ||
debugger; | ||
} | ||
/** | ||
* Returns a new function for checking *all* cases against a value, a bit | ||
* like `pipe` for predicates. | ||
*/ | ||
function checkAll() { | ||
for (var _len = arguments.length, predicates = new Array(_len), _key = 0; _key < _len; _key++) { | ||
predicates[_key] = arguments[_key]; | ||
} | ||
return function (value) { | ||
return predicates.every(function (p) { | ||
return p(value); | ||
}); | ||
}; | ||
} | ||
/** Apply *all* checks against a value. */ | ||
function checkAllWith(value) { | ||
for (var _len2 = arguments.length, predicates = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { | ||
predicates[_key2 - 1] = arguments[_key2]; | ||
} | ||
return checkAll.apply(void 0, predicates)(value); | ||
} | ||
/** Returns a new negated version of the stated predicate function. */ | ||
function negate(predicate) { | ||
return function (value) { | ||
return !predicate(value); | ||
}; | ||
} | ||
/** Checks whether a number is a finite */ | ||
var isFinite = Number.isFinite; | ||
/** Checks whether a number is a infinite */ | ||
var isInfinite = negate(isFinite); | ||
/** Checks whether a number is an integer */ | ||
var isInteger = Number.isInteger; | ||
/** Checks whether a number is a float */ | ||
var isFloat = negate(isInteger); | ||
/** Checks whether a number is even. */ | ||
var isEven = function isEven(value) { | ||
return value % 2 === 0; | ||
}; | ||
/** Checks whether a number is odd. */ | ||
var isOdd = function isOdd(value) { | ||
return Math.abs(value % 2) === 1; | ||
}; | ||
/** Checks whether a number is negative zero */ | ||
var isNegativeZero = function isNegativeZero(value) { | ||
return 1 / value === Number.NEGATIVE_INFINITY; | ||
}; | ||
/** Checks whether a number is negative */ | ||
var isNegative = function isNegative(value) { | ||
return value < 0; | ||
}; | ||
/** Checks whether a number is positive */ | ||
var isPositive = function isPositive(value) { | ||
return value > 0; | ||
}; | ||
/** Checks whether a number is non-negative */ | ||
var isNonNegative = function isNonNegative(value) { | ||
return value >= 0; | ||
}; | ||
/** Checks whether a number is non-positive */ | ||
var isNonPositive = function isNonPositive(value) { | ||
return value <= 0; | ||
}; | ||
// Primitives | ||
// ------------------------------ | ||
/** Checks whether a value is a boolean */ | ||
function isBoolean(value) { | ||
return typeof value === 'boolean'; | ||
} | ||
/** Checks whether a value is null */ | ||
function isNull(value) { | ||
return value === null; | ||
} | ||
/** Checks whether a value is a number */ | ||
function isNumber(value) { | ||
return typeof value === 'number' && !isNaN(value); | ||
} | ||
/** Checks whether a value is a string */ | ||
function isString(value) { | ||
return typeof value === 'string'; | ||
} | ||
/** Checks whether a value is undefined */ | ||
function isUndefined(value) { | ||
return value === undefined; | ||
} // Array | ||
// ------------------------------ | ||
/** Checks whether or not an array is empty. */ | ||
function isNonEmptyArray(value) { | ||
return value.length > 0; | ||
} // Convenience | ||
// ------------------------------ | ||
/** Checks whether a value is null or undefined */ | ||
function isNullish(value) { | ||
return value === null || value === undefined; | ||
} | ||
/** Checks whether a value is defined */ | ||
function isDefined(value) { | ||
return !isNullish(value); | ||
} | ||
/** | ||
* A generic helper function that takes a primitive value, and returns the value | ||
* after casting it to the provided opaque type. | ||
*/ | ||
// 1. extend `Opaque` to exclude transparent types e.g. `castToOpaque<number>(1)` | ||
// 2. default `never` to prohibit unfulfilled type e.g. `castToOpaque(1)` | ||
// 3. explicit `Transparent` to prevent invalid values e.g. `castToOpaque<OpaqueString>(1)` | ||
// 4. cast `unknown` first to avoid invalid expression instantiation | ||
function castToOpaque(value) { | ||
/* 4. */ | ||
return value; | ||
} | ||
function _typeof(obj) { | ||
"@babel/helpers - typeof"; | ||
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { | ||
return typeof obj; | ||
} : function (obj) { | ||
return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}, _typeof(obj); | ||
} | ||
/** | ||
* Simplifies `error` handling in `try...catch` statements. | ||
* | ||
* JavaScript is weird, you can `throw` anything. Since it's possible for | ||
* library authors to throw something unexpected, we have to take precautions. | ||
*/ | ||
function getErrorMessage(error) { | ||
var fallbackMessage = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'Unknown error'; | ||
if (isErrorLike(error)) { | ||
return error.message; | ||
} | ||
return error ? JSON.stringify(error) : fallbackMessage; | ||
} | ||
/** Handle situations where the error object isn't an _actual_ error. */ | ||
function isErrorLike(error) { | ||
return _typeof(error) === 'object' && error !== null && 'message' in error && typeof error.message === 'string'; | ||
} | ||
/** | ||
* An alternative to `Object.entries()` that avoids type widening. | ||
* | ||
* @example | ||
* Object.entries({ foo: 1, bar: 2 }) // [string, number][] | ||
* typedEntries({ foo: 1, bar: 2 }) // ["foo" | "bar", number][] | ||
*/ | ||
function typedEntries(value) { | ||
return Object.entries(value); | ||
} | ||
/** | ||
* An alternative to `Object.keys()` that avoids type widening. | ||
* | ||
* @example | ||
* Object.keys({ foo: 1, bar: 2 }) // string[] | ||
* typedKeys({ foo: 1, bar: 2 }) // ("foo" | "bar")[] | ||
*/ | ||
function typedKeys(value) { | ||
return Object.keys(value); | ||
} | ||
exports.assert = assert; | ||
exports.assertNever = assertNever; | ||
exports.castToOpaque = castToOpaque; | ||
exports.checkAll = checkAll; | ||
exports.checkAllWith = checkAllWith; | ||
exports.getErrorMessage = getErrorMessage; | ||
exports.isBoolean = isBoolean; | ||
exports.isDefined = isDefined; | ||
exports.isEven = isEven; | ||
exports.isFinite = isFinite; | ||
exports.isFloat = isFloat; | ||
exports.isInfinite = isInfinite; | ||
exports.isInteger = isInteger; | ||
exports.isNegative = isNegative; | ||
exports.isNegativeZero = isNegativeZero; | ||
exports.isNonEmptyArray = isNonEmptyArray; | ||
exports.isNonNegative = isNonNegative; | ||
exports.isNonPositive = isNonPositive; | ||
exports.isNull = isNull; | ||
exports.isNullish = isNullish; | ||
exports.isNumber = isNumber; | ||
exports.isOdd = isOdd; | ||
exports.isPositive = isPositive; | ||
exports.isString = isString; | ||
exports.isUndefined = isUndefined; | ||
exports.negate = negate; | ||
exports.typedEntries = typedEntries; | ||
exports.typedKeys = typedKeys; | ||
exports.assert = assertions_dist_emeryAssertions.assert; | ||
exports.assertNever = assertions_dist_emeryAssertions.assertNever; | ||
exports.checkAll = number.checkAll; | ||
exports.checkAllWith = number.checkAllWith; | ||
exports.isEven = number.isEven; | ||
exports.isFinite = number.isFinite; | ||
exports.isFloat = number.isFloat; | ||
exports.isInfinite = number.isInfinite; | ||
exports.isInteger = number.isInteger; | ||
exports.isNegative = number.isNegative; | ||
exports.isNegativeZero = number.isNegativeZero; | ||
exports.isNonNegative = number.isNonNegative; | ||
exports.isNonPositive = number.isNonPositive; | ||
exports.isOdd = number.isOdd; | ||
exports.isPositive = number.isPositive; | ||
exports.negate = number.negate; | ||
exports.isBoolean = guards_dist_emeryGuards.isBoolean; | ||
exports.isDefined = guards_dist_emeryGuards.isDefined; | ||
exports.isNonEmptyArray = guards_dist_emeryGuards.isNonEmptyArray; | ||
exports.isNull = guards_dist_emeryGuards.isNull; | ||
exports.isNullish = guards_dist_emeryGuards.isNullish; | ||
exports.isNumber = guards_dist_emeryGuards.isNumber; | ||
exports.isString = guards_dist_emeryGuards.isString; | ||
exports.isUndefined = guards_dist_emeryGuards.isUndefined; | ||
exports.castToOpaque = opaques_dist_emeryOpaques.castToOpaque; | ||
exports.getErrorMessage = object.getErrorMessage; | ||
exports.typedEntries = object.typedEntries; | ||
exports.typedKeys = object.typedKeys; |
@@ -5,251 +5,37 @@ 'use strict'; | ||
/** | ||
* Asserts that a condition is `true`, ensuring that whatever condition is being | ||
* checked must be true for the remainder of the containing scope. | ||
* | ||
* @throws when the condition is `false` | ||
* @returns void | ||
*/ | ||
// NOTE: The narrow type of `boolean` instead of something like `unknown` is an | ||
// intentional design decision. The goal is to promote consideration from | ||
// consumers when dealing with potentially ambiguous conditions like `0` or | ||
// `''`, which can introduce "subtle" bugs. | ||
function assert(condition) { | ||
var message = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'Assert failed'; | ||
var assertions_dist_emeryAssertions = require('../assertions/dist/emery-assertions.cjs.prod.js'); | ||
var number = require('./number-738a07d0.cjs.prod.js'); | ||
var guards_dist_emeryGuards = require('../guards/dist/emery-guards.cjs.prod.js'); | ||
var opaques_dist_emeryOpaques = require('../opaques/dist/emery-opaques.cjs.prod.js'); | ||
var object = require('./object-7c001aec.cjs.prod.js'); | ||
if (!condition) { | ||
throw new TypeError(message); | ||
} | ||
} | ||
/** | ||
* Asserts that allegedly unreachable code has been executed. | ||
* | ||
* @throws always | ||
* @returns void | ||
*/ | ||
function assertNever(condition) { | ||
throw new Error("Unexpected call to assertNever: '".concat(condition, "'")); | ||
} | ||
/** | ||
* Returns a new function for checking *all* cases against a value, a bit | ||
* like `pipe` for predicates. | ||
*/ | ||
function checkAll() { | ||
for (var _len = arguments.length, predicates = new Array(_len), _key = 0; _key < _len; _key++) { | ||
predicates[_key] = arguments[_key]; | ||
} | ||
return function (value) { | ||
return predicates.every(function (p) { | ||
return p(value); | ||
}); | ||
}; | ||
} | ||
/** Apply *all* checks against a value. */ | ||
function checkAllWith(value) { | ||
for (var _len2 = arguments.length, predicates = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { | ||
predicates[_key2 - 1] = arguments[_key2]; | ||
} | ||
return checkAll.apply(void 0, predicates)(value); | ||
} | ||
/** Returns a new negated version of the stated predicate function. */ | ||
function negate(predicate) { | ||
return function (value) { | ||
return !predicate(value); | ||
}; | ||
} | ||
/** Checks whether a number is a finite */ | ||
var isFinite = Number.isFinite; | ||
/** Checks whether a number is a infinite */ | ||
var isInfinite = negate(isFinite); | ||
/** Checks whether a number is an integer */ | ||
var isInteger = Number.isInteger; | ||
/** Checks whether a number is a float */ | ||
var isFloat = negate(isInteger); | ||
/** Checks whether a number is even. */ | ||
var isEven = function isEven(value) { | ||
return value % 2 === 0; | ||
}; | ||
/** Checks whether a number is odd. */ | ||
var isOdd = function isOdd(value) { | ||
return Math.abs(value % 2) === 1; | ||
}; | ||
/** Checks whether a number is negative zero */ | ||
var isNegativeZero = function isNegativeZero(value) { | ||
return 1 / value === Number.NEGATIVE_INFINITY; | ||
}; | ||
/** Checks whether a number is negative */ | ||
var isNegative = function isNegative(value) { | ||
return value < 0; | ||
}; | ||
/** Checks whether a number is positive */ | ||
var isPositive = function isPositive(value) { | ||
return value > 0; | ||
}; | ||
/** Checks whether a number is non-negative */ | ||
var isNonNegative = function isNonNegative(value) { | ||
return value >= 0; | ||
}; | ||
/** Checks whether a number is non-positive */ | ||
var isNonPositive = function isNonPositive(value) { | ||
return value <= 0; | ||
}; | ||
// Primitives | ||
// ------------------------------ | ||
/** Checks whether a value is a boolean */ | ||
function isBoolean(value) { | ||
return typeof value === 'boolean'; | ||
} | ||
/** Checks whether a value is null */ | ||
function isNull(value) { | ||
return value === null; | ||
} | ||
/** Checks whether a value is a number */ | ||
function isNumber(value) { | ||
return typeof value === 'number' && !isNaN(value); | ||
} | ||
/** Checks whether a value is a string */ | ||
function isString(value) { | ||
return typeof value === 'string'; | ||
} | ||
/** Checks whether a value is undefined */ | ||
function isUndefined(value) { | ||
return value === undefined; | ||
} // Array | ||
// ------------------------------ | ||
/** Checks whether or not an array is empty. */ | ||
function isNonEmptyArray(value) { | ||
return value.length > 0; | ||
} // Convenience | ||
// ------------------------------ | ||
/** Checks whether a value is null or undefined */ | ||
function isNullish(value) { | ||
return value === null || value === undefined; | ||
} | ||
/** Checks whether a value is defined */ | ||
function isDefined(value) { | ||
return !isNullish(value); | ||
} | ||
/** | ||
* A generic helper function that takes a primitive value, and returns the value | ||
* after casting it to the provided opaque type. | ||
*/ | ||
// 1. extend `Opaque` to exclude transparent types e.g. `castToOpaque<number>(1)` | ||
// 2. default `never` to prohibit unfulfilled type e.g. `castToOpaque(1)` | ||
// 3. explicit `Transparent` to prevent invalid values e.g. `castToOpaque<OpaqueString>(1)` | ||
// 4. cast `unknown` first to avoid invalid expression instantiation | ||
function castToOpaque(value) { | ||
/* 4. */ | ||
return value; | ||
} | ||
function _typeof(obj) { | ||
"@babel/helpers - typeof"; | ||
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { | ||
return typeof obj; | ||
} : function (obj) { | ||
return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}, _typeof(obj); | ||
} | ||
/** | ||
* Simplifies `error` handling in `try...catch` statements. | ||
* | ||
* JavaScript is weird, you can `throw` anything. Since it's possible for | ||
* library authors to throw something unexpected, we have to take precautions. | ||
*/ | ||
function getErrorMessage(error) { | ||
var fallbackMessage = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'Unknown error'; | ||
if (isErrorLike(error)) { | ||
return error.message; | ||
} | ||
return error ? JSON.stringify(error) : fallbackMessage; | ||
} | ||
/** Handle situations where the error object isn't an _actual_ error. */ | ||
function isErrorLike(error) { | ||
return _typeof(error) === 'object' && error !== null && 'message' in error && typeof error.message === 'string'; | ||
} | ||
/** | ||
* An alternative to `Object.entries()` that avoids type widening. | ||
* | ||
* @example | ||
* Object.entries({ foo: 1, bar: 2 }) // [string, number][] | ||
* typedEntries({ foo: 1, bar: 2 }) // ["foo" | "bar", number][] | ||
*/ | ||
function typedEntries(value) { | ||
return Object.entries(value); | ||
} | ||
/** | ||
* An alternative to `Object.keys()` that avoids type widening. | ||
* | ||
* @example | ||
* Object.keys({ foo: 1, bar: 2 }) // string[] | ||
* typedKeys({ foo: 1, bar: 2 }) // ("foo" | "bar")[] | ||
*/ | ||
function typedKeys(value) { | ||
return Object.keys(value); | ||
} | ||
exports.assert = assert; | ||
exports.assertNever = assertNever; | ||
exports.castToOpaque = castToOpaque; | ||
exports.checkAll = checkAll; | ||
exports.checkAllWith = checkAllWith; | ||
exports.getErrorMessage = getErrorMessage; | ||
exports.isBoolean = isBoolean; | ||
exports.isDefined = isDefined; | ||
exports.isEven = isEven; | ||
exports.isFinite = isFinite; | ||
exports.isFloat = isFloat; | ||
exports.isInfinite = isInfinite; | ||
exports.isInteger = isInteger; | ||
exports.isNegative = isNegative; | ||
exports.isNegativeZero = isNegativeZero; | ||
exports.isNonEmptyArray = isNonEmptyArray; | ||
exports.isNonNegative = isNonNegative; | ||
exports.isNonPositive = isNonPositive; | ||
exports.isNull = isNull; | ||
exports.isNullish = isNullish; | ||
exports.isNumber = isNumber; | ||
exports.isOdd = isOdd; | ||
exports.isPositive = isPositive; | ||
exports.isString = isString; | ||
exports.isUndefined = isUndefined; | ||
exports.negate = negate; | ||
exports.typedEntries = typedEntries; | ||
exports.typedKeys = typedKeys; | ||
exports.assert = assertions_dist_emeryAssertions.assert; | ||
exports.assertNever = assertions_dist_emeryAssertions.assertNever; | ||
exports.checkAll = number.checkAll; | ||
exports.checkAllWith = number.checkAllWith; | ||
exports.isEven = number.isEven; | ||
exports.isFinite = number.isFinite; | ||
exports.isFloat = number.isFloat; | ||
exports.isInfinite = number.isInfinite; | ||
exports.isInteger = number.isInteger; | ||
exports.isNegative = number.isNegative; | ||
exports.isNegativeZero = number.isNegativeZero; | ||
exports.isNonNegative = number.isNonNegative; | ||
exports.isNonPositive = number.isNonPositive; | ||
exports.isOdd = number.isOdd; | ||
exports.isPositive = number.isPositive; | ||
exports.negate = number.negate; | ||
exports.isBoolean = guards_dist_emeryGuards.isBoolean; | ||
exports.isDefined = guards_dist_emeryGuards.isDefined; | ||
exports.isNonEmptyArray = guards_dist_emeryGuards.isNonEmptyArray; | ||
exports.isNull = guards_dist_emeryGuards.isNull; | ||
exports.isNullish = guards_dist_emeryGuards.isNullish; | ||
exports.isNumber = guards_dist_emeryGuards.isNumber; | ||
exports.isString = guards_dist_emeryGuards.isString; | ||
exports.isUndefined = guards_dist_emeryGuards.isUndefined; | ||
exports.castToOpaque = opaques_dist_emeryOpaques.castToOpaque; | ||
exports.getErrorMessage = object.getErrorMessage; | ||
exports.typedEntries = object.typedEntries; | ||
exports.typedKeys = object.typedKeys; |
@@ -1,235 +0,5 @@ | ||
/** | ||
* Asserts that a condition is `true`, ensuring that whatever condition is being | ||
* checked must be true for the remainder of the containing scope. | ||
* | ||
* @throws when the condition is `false` | ||
* @returns void | ||
*/ | ||
// NOTE: The narrow type of `boolean` instead of something like `unknown` is an | ||
// intentional design decision. The goal is to promote consideration from | ||
// consumers when dealing with potentially ambiguous conditions like `0` or | ||
// `''`, which can introduce "subtle" bugs. | ||
function assert(condition) { | ||
var message = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'Assert failed'; | ||
if (!condition) { | ||
developmentDebugger(); | ||
throw new TypeError(message); | ||
} | ||
} | ||
/** | ||
* Asserts that allegedly unreachable code has been executed. | ||
* | ||
* @throws always | ||
* @returns void | ||
*/ | ||
function assertNever(condition) { | ||
developmentDebugger(); | ||
throw new Error("Unexpected call to assertNever: '".concat(condition, "'")); | ||
} | ||
/** Pause execution in development to aid debugging. */ | ||
function developmentDebugger() { | ||
if (process.env.NODE_ENV === 'production') { | ||
return; | ||
} // eslint-disable-next-line no-debugger | ||
debugger; | ||
} | ||
/** | ||
* Returns a new function for checking *all* cases against a value, a bit | ||
* like `pipe` for predicates. | ||
*/ | ||
function checkAll() { | ||
for (var _len = arguments.length, predicates = new Array(_len), _key = 0; _key < _len; _key++) { | ||
predicates[_key] = arguments[_key]; | ||
} | ||
return function (value) { | ||
return predicates.every(function (p) { | ||
return p(value); | ||
}); | ||
}; | ||
} | ||
/** Apply *all* checks against a value. */ | ||
function checkAllWith(value) { | ||
for (var _len2 = arguments.length, predicates = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { | ||
predicates[_key2 - 1] = arguments[_key2]; | ||
} | ||
return checkAll.apply(void 0, predicates)(value); | ||
} | ||
/** Returns a new negated version of the stated predicate function. */ | ||
function negate(predicate) { | ||
return function (value) { | ||
return !predicate(value); | ||
}; | ||
} | ||
/** Checks whether a number is a finite */ | ||
var isFinite = Number.isFinite; | ||
/** Checks whether a number is a infinite */ | ||
var isInfinite = negate(isFinite); | ||
/** Checks whether a number is an integer */ | ||
var isInteger = Number.isInteger; | ||
/** Checks whether a number is a float */ | ||
var isFloat = negate(isInteger); | ||
/** Checks whether a number is even. */ | ||
var isEven = function isEven(value) { | ||
return value % 2 === 0; | ||
}; | ||
/** Checks whether a number is odd. */ | ||
var isOdd = function isOdd(value) { | ||
return Math.abs(value % 2) === 1; | ||
}; | ||
/** Checks whether a number is negative zero */ | ||
var isNegativeZero = function isNegativeZero(value) { | ||
return 1 / value === Number.NEGATIVE_INFINITY; | ||
}; | ||
/** Checks whether a number is negative */ | ||
var isNegative = function isNegative(value) { | ||
return value < 0; | ||
}; | ||
/** Checks whether a number is positive */ | ||
var isPositive = function isPositive(value) { | ||
return value > 0; | ||
}; | ||
/** Checks whether a number is non-negative */ | ||
var isNonNegative = function isNonNegative(value) { | ||
return value >= 0; | ||
}; | ||
/** Checks whether a number is non-positive */ | ||
var isNonPositive = function isNonPositive(value) { | ||
return value <= 0; | ||
}; | ||
// Primitives | ||
// ------------------------------ | ||
/** Checks whether a value is a boolean */ | ||
function isBoolean(value) { | ||
return typeof value === 'boolean'; | ||
} | ||
/** Checks whether a value is null */ | ||
function isNull(value) { | ||
return value === null; | ||
} | ||
/** Checks whether a value is a number */ | ||
function isNumber(value) { | ||
return typeof value === 'number' && !isNaN(value); | ||
} | ||
/** Checks whether a value is a string */ | ||
function isString(value) { | ||
return typeof value === 'string'; | ||
} | ||
/** Checks whether a value is undefined */ | ||
function isUndefined(value) { | ||
return value === undefined; | ||
} // Array | ||
// ------------------------------ | ||
/** Checks whether or not an array is empty. */ | ||
function isNonEmptyArray(value) { | ||
return value.length > 0; | ||
} // Convenience | ||
// ------------------------------ | ||
/** Checks whether a value is null or undefined */ | ||
function isNullish(value) { | ||
return value === null || value === undefined; | ||
} | ||
/** Checks whether a value is defined */ | ||
function isDefined(value) { | ||
return !isNullish(value); | ||
} | ||
/** | ||
* A generic helper function that takes a primitive value, and returns the value | ||
* after casting it to the provided opaque type. | ||
*/ | ||
// 1. extend `Opaque` to exclude transparent types e.g. `castToOpaque<number>(1)` | ||
// 2. default `never` to prohibit unfulfilled type e.g. `castToOpaque(1)` | ||
// 3. explicit `Transparent` to prevent invalid values e.g. `castToOpaque<OpaqueString>(1)` | ||
// 4. cast `unknown` first to avoid invalid expression instantiation | ||
function castToOpaque(value) { | ||
/* 4. */ | ||
return value; | ||
} | ||
function _typeof(obj) { | ||
"@babel/helpers - typeof"; | ||
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { | ||
return typeof obj; | ||
} : function (obj) { | ||
return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}, _typeof(obj); | ||
} | ||
/** | ||
* Simplifies `error` handling in `try...catch` statements. | ||
* | ||
* JavaScript is weird, you can `throw` anything. Since it's possible for | ||
* library authors to throw something unexpected, we have to take precautions. | ||
*/ | ||
function getErrorMessage(error) { | ||
var fallbackMessage = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'Unknown error'; | ||
if (isErrorLike(error)) { | ||
return error.message; | ||
} | ||
return error ? JSON.stringify(error) : fallbackMessage; | ||
} | ||
/** Handle situations where the error object isn't an _actual_ error. */ | ||
function isErrorLike(error) { | ||
return _typeof(error) === 'object' && error !== null && 'message' in error && typeof error.message === 'string'; | ||
} | ||
/** | ||
* An alternative to `Object.entries()` that avoids type widening. | ||
* | ||
* @example | ||
* Object.entries({ foo: 1, bar: 2 }) // [string, number][] | ||
* typedEntries({ foo: 1, bar: 2 }) // ["foo" | "bar", number][] | ||
*/ | ||
function typedEntries(value) { | ||
return Object.entries(value); | ||
} | ||
/** | ||
* An alternative to `Object.keys()` that avoids type widening. | ||
* | ||
* @example | ||
* Object.keys({ foo: 1, bar: 2 }) // string[] | ||
* typedKeys({ foo: 1, bar: 2 }) // ("foo" | "bar")[] | ||
*/ | ||
function typedKeys(value) { | ||
return Object.keys(value); | ||
} | ||
export { assert, assertNever, castToOpaque, checkAll, checkAllWith, getErrorMessage, isBoolean, isDefined, isEven, isFinite, isFloat, isInfinite, isInteger, isNegative, isNegativeZero, isNonEmptyArray, isNonNegative, isNonPositive, isNull, isNullish, isNumber, isOdd, isPositive, isString, isUndefined, negate, typedEntries, typedKeys }; | ||
export { assert, assertNever } from '../assertions/dist/emery-assertions.esm.js'; | ||
export { l as checkAll, m as checkAllWith, i as isEven, a as isFinite, b as isFloat, c as isInfinite, d as isInteger, e as isNegative, f as isNegativeZero, g as isNonNegative, h as isNonPositive, j as isOdd, k as isPositive, n as negate } from './number-556a10ae.esm.js'; | ||
export { isBoolean, isDefined, isNonEmptyArray, isNull, isNullish, isNumber, isString, isUndefined } from '../guards/dist/emery-guards.esm.js'; | ||
export { castToOpaque } from '../opaques/dist/emery-opaques.esm.js'; | ||
export { g as getErrorMessage, t as typedEntries, a as typedKeys } from './object-d266a626.esm.js'; |
{ | ||
"name": "emery", | ||
"version": "1.0.0", | ||
"description": "Utilities to help polish the parts of TypeScript that are a bit rough.", | ||
"version": "1.1.0", | ||
"description": "Utilities to help polish the rough parts of TypeScript.", | ||
"main": "dist/emery.cjs.js", | ||
"module": "dist/emery.esm.js", | ||
"repository": "https://github.com/thinkmill/emery.git", | ||
"homepage": "https://emery-ts.vercel.app", | ||
"author": "jossmac <jossmac@users.noreply.github.com>", | ||
@@ -13,4 +14,15 @@ "license": "MIT", | ||
"dist", | ||
"src" | ||
"src", | ||
"assertions", | ||
"checks", | ||
"guards", | ||
"opaques", | ||
"utils" | ||
], | ||
"keywords": [ | ||
"ts", | ||
"typescript", | ||
"utils", | ||
"utilities" | ||
], | ||
"scripts": { | ||
@@ -38,2 +50,12 @@ "format": "prettier --write ./src", | ||
}, | ||
"preconstruct": { | ||
"entrypoints": [ | ||
"index.ts", | ||
"assertions.ts", | ||
"checks/index.ts", | ||
"guards.ts", | ||
"opaques.ts", | ||
"utils/index.ts" | ||
] | ||
}, | ||
"devDependencies": { | ||
@@ -40,0 +62,0 @@ "@babel/core": "^7.17.12", |
# Emery | ||
Utilities to help polish the parts of TypeScript that are a bit rough. | ||
Utilities to help polish the rough parts of TypeScript. | ||
@@ -5,0 +5,0 @@ ```ts |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
68948
85
1569
0
7
1