object-all-values-equal-to2.1.0

Does the AST/nested-plain-object/array/whatever contain only one kind of value?

Quick Take

import { strict as assert } from "assert";
import { allEq } from "object-all-values-equal-to";

// are all values equal to null:
assert.equal(allEq({ a: null, c: null }, null), true);
// yes

// are all values equal to "false":
assert.equal(
  allEq({ a: false, c: "zzz" }, false),
  false
);
// no

// are all values equal to "false"?
assert.equal(
  allEq(
    {
      a: {
        b: false,
        c: [
          {
            d: false,
            e: false,
          },
          {
            g: false,
          },
        ],
      },
      c: false,
    },
    false // reference value to check
  ),
  true // answer is, yes
);

API

allEq(input, value)

In other words, it's a function which takes two input arguments, both obligatory.

API - Input

Input argument Type Obligatory? Default Description
input Whatever yes undefined AST tree, or object or array or whatever. Can be deeply-nested. Hopefully contains some nested plain objects. We love nested plain objects.
value Whatever yes false We will check, does input contain only value on every key. Please don't set it to undefined.

Optional Options Object

options object's key Type of its value Default Description
arraysMustNotContainPlaceholders Boolean true When set to true, value within array should not be present and will yield false result. Set this to false to allow one or more value's within arrays in the input.

Here are the Optional Options Object's defaults in one place (in case you ever want to copy and tweak it):

{
arraysMustNotContainPlaceholders: true,
}

API - Output

Boolean: true or false.

opts.arraysMustNotContainPlaceholders

When working with data structures, this library would be used to check, is the certain piece of JSON data (some key's value, a nested object) is all blank, that is, contains only placeholders everywhere.

Now, with regards to arrays, default arrays should not contain placeholders directly. For example key b is customised, it's not a placeholder:

{
"a": false,
"b": [false]
}

It should be instead:

{
"a": false,
"b": []
}

When checking against second argument false, this library will yield false for former and true for latter.

Now, this is relevant only when working with data structures. When dealing with all other kinds of nested objects and arrays, placeholders within arrays count as placeholders and should yield true.

For that, turn off the opts.arraysMustNotContainPlaceholders, set it to false.

Observe:

let res1 = allValuesEqualTo([null], null);
console.log(res1);
// => false

let res2 = allValuesEqualTo([null], null, {
arraysMustNotContainPlaceholders: false,
});
console.log(res2);
// => true

Why we need this

For example, we were working on object-fill-missing-keys. The library takes an object, a reference object, and fills in missing keys according to the reference. We were implementing a feature, a options switch, which let to skip filling for chosen keys if they currently contain only placeholders.

You'll need this library when you want to check, does the AST contain only certain value throughout the whole tree. Also, it can be a simple object, in which case, we'd be checking, are all values of all keys equal to something.

Changelog

See it in the monorepo opens in a new tab, on GitHub.

Contributing

To report bugs or request features or assistance, raise an issue on GitHub opens in a new tab.

Any code contributions welcome! All Pull Requests will be dealt promptly.

Licence

MIT opens in a new tab

Copyright © 2010–2021 Roy Revelt and other contributors

Related packages:

📦 object-fill-missing-keys 8.1.0
Add missing keys into plain objects, according to a reference object
📦 object-no-new-keys 3.1.0
Check, does a plain object (AST/JSON) has any unique keys, not present in a reference object (another AST/JSON)
📦 object-flatten-all-arrays 5.1.0
Merge and flatten any arrays found in all values within plain objects
📦 object-merge-advanced 12.1.0
Recursively, deeply merge of anything (objects, arrays, strings or nested thereof), which weighs contents by type hierarchy to ensure the maximum content is retained
📦 object-flatten-referencing 5.1.0
Flatten complex nested objects according to a reference objects
📦 object-delete-key 2.1.0
Delete keys from all arrays or plain objects, nested within anything, by key or by value or by both, and clean up afterwards. Accepts wildcards
📦 object-boolean-combinations 4.1.0
Consumes a defaults object with booleans, generates all possible variations of it