Skip to content

Object Utilities

Object helpers are exported as standalone functions. Prefer named function imports for tree-shakeable and readable call sites.

ts
import { diffObjects, hasObjectDifferences, parseObject, serializeDecimalValues } from '@querry-kit/nest-util/object';

parseObject(obj)

Parses common query-string values into JavaScript values.

ts
parseObject({
  page: '1',
  enabled: 'true',
  deletedAt: 'null',
  tags: ['1', '2'],
  orderBy: '{"createdAt":"desc"}',
  'user.name': 'Ada',
});

// {
//   page: 1,
//   enabled: true,
//   deletedAt: null,
//   tags: [1, 2],
//   orderBy: { createdAt: 'desc' },
//   user: { name: 'Ada' }
// }

JSON object and array strings are parsed recursively when they are valid JSON. Invalid JSON-like strings and empty strings are preserved.

hasObjectDifferences(a, b)

Returns true when two values differ. It compares primitives, arrays, and plain objects recursively.

ts
hasObjectDifferences({ role: 'admin' }, { role: 'user' });
// true

hasObjectDifferences([1, 2], [1, 2]);
// false

diffObjects(a, b)

Returns a nested object with old and new values for changed fields.

ts
diffObjects({ age: 30 }, { age: 31 });
// { age: { old: 30, new: 31 } }

Nested objects are diffed recursively:

ts
diffObjects({ address: { city: 'Berlin' } }, { address: { city: 'Hamburg' } });

// {
//   address: {
//     city: { old: 'Berlin', new: 'Hamburg' }
//   }
// }

serializeDecimalValues(value)

Recursively converts Decimal-like objects with a toNumber() method into numbers. This is useful for serializing Prisma Decimal values without importing Prisma at runtime.

ts
serializeDecimalValues({
  total: { toNumber: () => 12.5 },
  nested: [{ value: { toNumber: () => 2 } }],
});

// {
//   total: 12.5,
//   nested: [{ value: 2 }]
// }

Use isDecimalLike(value) when you only need the predicate.

Predicate Helpers

These helpers are exported directly:

MethodReturns true when
isObject(obj)typeof obj === 'object'.
isNumber(obj)obj is a number or can be converted to a number.
isBoolean(obj)obj is true, false, 'true', or 'false'.
isPlainObject(value)value is a non-array object and not null.
isDecimalLike(value)value exposes a Decimal-like toNumber method.

Backwards Compatibility

The package root still exports the older parse, diff, and hasDifferences aliases. New code should prefer the clearer object names from @querry-kit/nest-util/object.

Released under the MIT License.