Installation
Quick Take
API — allEq()
The main function allEq()
is imported like this:
It’s a function which takes three input arguments:
Input argument | Type | Obligatory | Description |
---|---|---|---|
input Type: Any JSON-friendly data type Obligatory: yes | |||
input | Any JSON-friendly data type | yes | Input data structure. |
value Type: Any JSON-friendly data type Obligatory: yes | |||
value | Any JSON-friendly data type | yes | We will check, does input contain only value on every key. |
opts Type: Plain object Obligatory: no | |||
opts | Plain object | no | Optional Options Object. |
The Optional Options Object has the following shape:
Key | Type | Default | Description |
---|---|---|---|
arraysMustNotContainPlaceholders Type: Boolean Default: true | |||
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 all defaults in one place for copying:
The function will return a boolean.
API — defaults
You can import defaults
:
It's a plain object:
The main function calculates the options to be used by merging the options you passed with these defaults.
API — version
You can import version
:
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.