sfcode
An Online Competing and Development Environment
Plain Object

A plain object

  • Inherits directly from Object.prototype or null
  • Is not a constructor's prototype property

<tt>plain-object/is</tt>

Confirms if given object is a plain object

const isPlainObject = require("type/plain-object/is");
isPlainObject({}); // true
isPlainObject(Object.create(null)); // true
isPlainObject([]); // false

<tt>plain-object/ensure</tt>

If given argument is a plain object it is returned back. Otherwise TypeError is thrown.

const ensurePlainObject = require("type/plain-object/ensure");
ensurePlainObject({}); // {}
ensurePlainObject("foo"); // Thrown TypeError: foo is not a plain object

Confirming on keys

Keys can be validated by passing allowedKeys option. Note that in this case:

  • Error message lists up to three invalid keys
const allowedKeys = ["foo"];
ensurePlainObject({}, { allowedKeys }); // {}
ensurePlainObject({ foo: "bar" }, { allowedKeys }); // { foo: 'bar' }
/*
Below invocation with crash with:
TypeError: [object Object] is not a valid plain object.
Following keys are unexpected: lorem, ipsum
*/
ensurePlainObject({ foo: "bar", lorem: 1, ipsum: 2 }, { allowedKeys });

Confirming on property values

Property values can be validated by passing ensurePropertyValue option. Note that in this case:

  • A newly created instance of plain object with coerced values is returned
  • Error message lists up to three keys that contain invalid values
const ensureString = require("type/string/ensure");
ensurePlainObject({ foo: 12 }, { ensurePropertyValue: ensureString }); // { foo: '12' }
/*
Below invocation with crash with:
TypeError: [object Object] is not a valid plain object.
Valuees for following keys are invalid: lorem, ipsum
*/
ensurePlainObject({ foo: 23, lorem: {}, ipsum: {} }, { ensurePropertyValue: ensureString });