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

§ Quick Take

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

// are all values equal to null:
  allEqualTo({ a: null, c: null }, null),
// yes

// are all values equal to "false":
  allEqualTo({ a: false, c: "zzz" }, false),
// no

// are all values equal to "false"?
      a: {
        b: false,
        c: [
            d: false,
            e: false,
            g: false,
      c: false,
    false // reference value to check
  true // answer is, yes


allValuesEqualToWrapper(input, value)

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

§ API - Input

Input argumentTypeObligatory?DefaultDescription
inputWhateveryesundefinedAST tree, or object or array or whatever. Can be deeply-nested. Hopefully contains some nested plain objects. We love nested plain objects.
valueWhateveryesfalseWe will check, does input contain only value on every key. Please don't set it to undefined.

§ Optional Options Object

options object's keyType of its valueDefaultDescription
arraysMustNotContainPlaceholdersBooleantrueWhen 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.


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

let res2 = allValuesEqualTo([null], null, {
arraysMustNotContainPlaceholders: false,
// => 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.

§ Licence

MIT opens in a new tab

Copyright © 2010–2020 Roy Revelt and other contributors

Related packages:

📦 object-fill-missing-keys 7.10.28
Add missing keys into plain objects, according to a reference object
📦 object-boolean-combinations 2.11.66
Consumes a defaults object with booleans, generates all possible variations of it
📦 object-flatten-referencing 4.11.27
Flatten complex nested objects according to a reference objects
📦 object-delete-key 1.9.38
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-set-all-values-to 3.9.67
Recursively walk the input and set all found values in plain objects to something
📦 object-flatten-all-arrays 4.8.23
Merge and flatten any arrays found in all values within plain objects
📦 object-no-new-keys 2.9.11
Check, does a plain object (AST/JSON) has any unique keys, not present in a reference object (another AST/JSON)