ts-data-forge
Version:
[](https://www.npmjs.com/package/ts-data-forge) [](https://www.npmjs.com/package/ts-data-forge) [ • 5.07 kB
JavaScript
/**
* A collection of type-safe object utility functions providing functional
* programming patterns for object manipulation, including pick, omit, shallow
* equality checks, and more.
*
* All functions maintain TypeScript type safety and support both direct and
* curried usage patterns for better composition with pipe operations.
*/
var Obj;
(function (Obj) {
/**
* Performs a shallow equality check on two records using a configurable
* equality function. Verifies that both records have the same number of
* entries and that for every key in the first record, the corresponding value
* passes the equality test with the value in the second record.
*
* @example
*
* ```ts
* const obj1 = { name: 'Alice', age: 30 };
*
* const obj2 = { name: 'Alice', age: 30 };
*
* const obj3 = { name: 'Alice', age: 31 };
*
* assert.isTrue(Obj.shallowEq(obj1, obj2));
*
* assert.isFalse(Obj.shallowEq(obj1, obj3));
*
* // Custom equality function
* const obj4 = { value: 1 };
*
* const obj5 = { value: 1.00001 };
*
* const closeEnough = (a: unknown, b: unknown): boolean => {
* if (typeof a === 'number' && typeof b === 'number') {
* return Math.abs(a - b) < 0.001;
* }
*
* return Object.is(a, b);
* };
*
* assert.isTrue(Obj.shallowEq(obj4, obj5, closeEnough));
* ```
*
* @param a - The first record to compare
* @param b - The second record to compare
* @param eq - Optional equality function (defaults to Object.is for strict
* equality)
* @returns `true` if the records are shallowly equal according to the
* equality function, `false` otherwise
*/
Obj.shallowEq = (a, b, eq = Object.is) => {
const aEntries = Object.entries(a);
const bEntries = Object.entries(b);
if (aEntries.length !== bEntries.length)
return false;
return aEntries.every(([k, v]) => eq(b[k], v));
};
function pick(...args) {
switch (args.length) {
case 2: {
const [record, keys] = args;
const keysSet = new Set(keys);
return (
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
Object.fromEntries(Object.entries(record).filter(([k, _v]) => keysSet.has(k))));
}
case 1: {
const [keys] = args;
return (record) => pick(record, keys);
}
}
}
Obj.pick = pick;
function omit(...args) {
switch (args.length) {
case 2: {
const [record, keys] = args;
const keysSet = new Set(keys);
return (
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
Object.fromEntries(Object.entries(record).filter(([k, _v]) => !keysSet.has(k))));
}
case 1: {
const [keys] = args;
return (record) => {
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
const result = omit(record, keys);
return result;
};
}
}
}
Obj.omit = omit;
/**
* Creates an object from an array of key-value pairs with precise TypeScript
* typing. This is a type-safe wrapper around `Object.fromEntries` that
* provides better type inference and compile-time guarantees about the
* resulting object structure.
*
* **Type Behavior**:
*
* - When entries is a fixed-length tuple, the exact object type is inferred
* - When entries has dynamic length with union key types, `Partial` is applied
* to prevent incorrect assumptions about which keys will be present
*
* @example
*
* ```ts
* // Fixed-length tuple - exact type inferred
* const entries1 = [
* ['name', 'David'],
* ['age', 25],
* ['active', true],
* ] as const;
*
* const obj1 = Obj.fromEntries(entries1);
*
* assert.deepStrictEqual(obj1, {
* name: 'David',
* age: 25,
* active: true,
* });
*
* // Dynamic length array - Partial type applied
* const dynamicEntries: (readonly ['x' | 'y', number])[] = [
* ['x', 10],
* ['y', 20],
* ];
*
* const obj2 = Obj.fromEntries(dynamicEntries);
*
* assert.deepStrictEqual(obj2, { x: 10, y: 20 });
* ```
*
* @template Entries - The readonly array type of key-value entry tuples
* @param entries - An array of readonly key-value entry tuples `[key, value]`
* @returns An object created from the entries with precise typing
*/
Obj.fromEntries = (entries) =>
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
Object.fromEntries(entries);
})(Obj || (Obj = {}));
export { Obj };
//# sourceMappingURL=object.mjs.map