@plandek-utils/plain-object
TypeScript icon, indicating that this package has built-in type declarations

2.2.0 • Public • Published

@plandek-utils/plain-object

npm version CI Maintainability Test Coverage

TypeScript types and predicate utils isPlainObject and isPlainObjectValue. Also includes fast checks isPlainObjectValueAnObject and `isPlainObjectValueAnArrayOfObjects.

A PlainObject is a POJO where all values are PlainObjectValue.

A PlainObjectValue is one of:

  • null or undefined
  • Finite number (no NaN or Infinite)
  • String
  • Boolean
  • Dayjs
  • Array of PlainObjectValue (or readonly array of PlainObjectValue)
  • PlainObject

Usage

import { isPlainObject, isPlainObjectValue } from "@plandek-utils/plain-object";

isPlainObjectValue(null); // => true
isPlainObjectValue(undefined); // => true
isPlainObjectValue(1); // => true
isPlainObjectValue(Infinite); // => false
isPlainObjectValue(NaN); // => false
isPlainObjectValue("something"); // => true
isPlainObjectValue(true); // => true
isPlainObjectValue(false); // => true
isPlainObjectValue([]); // => true
isPlainObjectValue([1, 2]); // => true
isPlainObjectValue([1, "something", 2]); // => true
isPlainObjectValue([1, "something", [2, 3]]); // => true
isPlainObjectValue(() => "oh no"); // => false
isPlainObjectValue({ a: 1, b: "stuff", c: [1], d: {}, e: { e1: true } }); // => true
isPlainObjectValue({ a: 1, b: "stuff", c: [1], d: {}, e: { e1: () => "oh no" } }); // => false

isPlainObject(null); // => false
isPlainObject(undefined); // => false
isPlainObject(1); // => false
isPlainObject(Infinite); // => false
isPlainObject(NaN); // => false
isPlainObject("something"); // => false
isPlainObject(true); // => false
isPlainObject(false); // => false
isPlainObject([]); // => false
isPlainObject([1, 2]); // => false
isPlainObject([1, "something", 2]); // => false
isPlainObject([1, "something", [2, 3]]); // => false
isPlainObject(() => "oh no"); // => false
isPlainObject({ a: 1, b: "stuff", c: [1], d: {}, e: { e1: true } }); // => true
isPlainObject({ a: 1, b: "stuff", c: [1], d: {}, e: { e1: () => "oh no" } }); // => false

isPlainObject will check exhaustively all values of the given object using zod. This can be slow for large quantities of data. If we already know that the given data is a PlainObjectValue, we can use isPlainObjectValueAnObject or isPlainObjectValueAnArrayOfObjects to check if the value is an object or an array of objects respectively.

import { isPlainObjectValueAnObject, isPlainObjectValueAnArrayOfObjects } from "@plandek-utils/plain-object";

isPlainObjectValueAnObject({ a: 1, b: "stuff", c: [1], d: {}, e: { e1: true } }); // => true
isPlainObjectValueAnArrayOfObjects([{ a: 1, b: "stuff", c: [1], d: {}, e: { e1: true } }]); // => true

Development

TypeScript types and predicate isPlainObject and isPlainObjectValue. PlainObject = POJO where all values are PlainObjectValue. PlainObjectValue = serializable value (Dayjs, nil, number, string, boolean, PlainObjectValue[], PlainObject)

Readme

Keywords

none

Package Sidebar

Install

npm i @plandek-utils/plain-object

Weekly Downloads

671

Version

2.2.0

License

MIT

Unpacked Size

42 kB

Total Files

10

Last publish

Collaborators

  • eturino
  • plandek-dev