UNPKG

@naturalcycles/js-lib

Version:

Standard library for universal (browser + Node.js) javascript

68 lines (67 loc) 2.09 kB
import { _sortBy } from './array/array.util.js'; /** * Symbol to indicate END of Sequence. */ export const END = Symbol('END'); /** * Symbol to indicate SKIP of item (e.g in AbortableMapper) */ export const SKIP = Symbol('SKIP'); /** * Symbol to indicate cache miss. * To distinguish from cache returning `undefined` or `null`. */ export const MISS = Symbol('MISS'); export const _passthroughMapper = item => item; export const _passUndefinedMapper = () => undefined; /** * Function that does nothings and returns `undefined`. */ export const _noop = (..._args) => undefined; export const _passthroughPredicate = () => true; export const _passNothingPredicate = () => false; /** * Like _stringMapValues, but values are sorted. */ export function _stringMapValuesSorted(map, mapper, dir = 'asc') { return _sortBy(_stringMapValues(map), mapper, { dir }); } /** * Needed due to https://github.com/microsoft/TypeScript/issues/13778 * Only affects typings, no runtime effect. */ export const _stringMapValues = Object.values; /** * Needed due to https://github.com/microsoft/TypeScript/issues/13778 * Only affects typings, no runtime effect. */ export const _stringMapEntries = Object.entries; /** * Alias of `Object.keys`, but returns keys typed as `keyof T`, not as just `string`. * This is how TypeScript should work, actually. */ export const _objectKeys = Object.keys; /** * Alias of `Object.entries`, but returns better-typed output. * * So e.g you can use _objectEntries(obj).map([k, v] => {}) * and `k` will be `keyof obj` instead of generic `string`. */ export const _objectEntries = Object.entries; /** * Utility function that helps to cast *existing variable* to needed type T. * * @example * try {} catch (err) { * // err is unknown here * _typeCast<AppError>(err) * // now err is of type AppError * err.data = {} // can be done, because it was casted * } */ export function _typeCast(v) { } /** * Type-safe Object.assign that checks that part is indeed a Partial<T> */ export const _objectAssign = Object.assign; /* eslint-enable */