@zachsents/zippy
v1.1.1
Published
A TypeScript utility library.
Downloads
1,398
Readme
@zachsents/zippy
Small TypeScript utility functions for finite iterables, objects, guards, math, sets, and zipping values.
Most collection helpers can be called data-first or data-last for piping. The
examples below use the data-first form. Helpers that accept selectors take a
callback or a type-safe property/dot path on the base helper name, such as
sum(values, "count") or sum("count")(values).
Functions
Iterables
castArray- Keep arrays as-is, return an empty array forundefined, otherwise wrap a value in an array.filter- Filter a finite iterable with a predicate or type guard. LikeArray.from(values).filter(predicate).filterOut- Remove values that match a predicate or type guard. LikeArray.from(values).filter((value, index, source) => !predicate(value, index, source)).map- Map finite iterable values. LikeArray.from(values).map(mapper).mapAsync- Map finite iterable values with async support. LikePromise.all(Array.from(values).map(mapper)), or pass{ concurrency }to limit parallel mapper calls.unique- Remove duplicate values, optionally by a selected key. Like[...new Set(values)].
Math
mean- Return the arithmetic average of numbers, or numbers selected from each value. Returnsundefinedfor an empty finite iterable.median- Return the median of numbers, or numbers selected from each value. Returnsundefinedfor an empty finite iterable.mode- Return the most common value, or the first value whose selected key is most common. Returnsundefinedfor an empty finite iterable.sum- Add numbers, or numbers selected from each value. Returns0for an empty finite iterable.
Objects
deepMerge- Recursively merge plain object values, with source values overriding destination values.mapEntries- Transform object entries into a new object. LikeObject.fromEntries(Object.entries(values).map((entry, index) => mapper(entry, index, values))).mapEntriesAsync- Transform object entries with async support. LikeObject.fromEntries(await Promise.all(Object.entries(values).map((entry, index) => mapper(entry, index, values)))), or pass{ concurrency }to limit parallel mapper calls.mapKeys- Transform object keys. LikeObject.fromEntries(Object.entries(values).map(([key, value]) => [mapper(value, key, values), value])).mapKeysAsync- Transform object keys with async support. LikemapKeyswith awaited keys, or pass{ concurrency }to limit parallel mapper calls.mapValues- Transform object values. LikeObject.fromEntries(Object.entries(values).map(([key, value]) => [key, mapper(value, key, values)])).mapValuesAsync- Transform object values with async support. LikemapValueswith awaited values, or pass{ concurrency }to limit parallel mapper calls.merge- Shallow merge two objects. Like{ ...destination, ...source }.
Matching and Zipping
match- Pair values by matching each left value with the first unmatched right value using a required matcher callback or shared property path. Pass a merger callback as the fourth argument to map each matched pair.matchMerge- Match finite iterables of objects with a required matcher and shallow merge each matched pair.zip- Pair values from two finite iterables by index. Like materializing both sides and mapping to[leftValue, rightValue]pairs up to the shorter side. Pass a merger callback as the third argument to map each positional pair.
Guards
isDefined- Check whether a value is notundefined.isFalsy- Check whether a value is falsy. Like!value.isNullish- Check whether a value isnullorundefined. Likevalue == null.isNonNullish- Check whether a value is notnullorundefined.isPlainObject- Check whether a value is a plain object.isReadonlyArray- Check whether a value is an array, preserving readonly array types.isTruthy- Check whether a value is truthy. LikeBoolean(value).isUndefined- Check whether a value isundefined. Likevalue === undefined.propIsDefined,propIsFalsy,propIsNullish,propIsNonNullish,propIsPlainObject,propIsTruthy, andpropIsUndefined- Return a guard that checks a type-safe property/dot path.
Other
identity- Return the input value unchanged. Like(value) => value.tuple- Return arguments as a tuple. Like(...values) => values.
