File size: 1,903 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# Comparing object properties
This module exports a helper that creates customized comparator functions to compare
objects. The comparator can be configured to compare only selected properties and
ignore all other ones, and compare some properties shallowly and other ones deeply.
If no options are specified, the comparator will compare all properties shallowly:
```js
const comparator = compareProps();
// returns true: the property values are strictly equal
comparator( { a: 1 }, { a: 1 } );
// returns false: the property values are not strictly equal
comparator( { a: [] }, { a: [] } );
```
## `ignore` option
Specify an `ignore` list of properties that should be ignored by the comparator:
```js
const comparator = compareProps( { ignore: [ 'irrelevant' ] } );
// returns true: the `a` properties are strictly equal and other are irrelevant
comparator(
{
a: 1,
irrelevant: 'whatever1',
},
{
a: 1,
irrelevant: 'whatever2',
}
);
```
## `deep` option
To compare selected properties deeply, specify a `deep` option:
```js
const comparator = compareProps( { deep: [ 'query' ] } );
// returns true: the `page` properties are are strictly equal and
// the `query` objects are deeply equal, although not identical
comparator(
{
query: { text: 'plugin' },
page: 2,
},
{
query: { text: 'plugin' },
page: 2,
}
);
```
## `shallow` option
If you want to compare only some selected properties shallowly, it's more convenient
to enumerate them in a `shallow` option instead of a long `ignore` list. This option
also comes handy if the compared objects can have arbitrary extra properties that are
not relevant for the comparison.
```js
const comparator = compareProps( { shallow: [ 'id' ] } );
// returns true: the `id` properties are strictly equal and all others are ignored
comparator(
{
id: 1,
is_jetpack: true,
},
{
id: 1,
is_domain_only: true,
}
);
```
|