What is is-subset?
The is-subset npm package is a utility that allows you to check if one set of values is a subset of another. This can be particularly useful for validating data structures, filtering data, and performing set operations in JavaScript.
What are is-subset's main functionalities?
Basic Subset Check
This feature allows you to check if one object is a subset of another. In this example, object `b` is a subset of object `a` because all key-value pairs in `b` exist in `a`.
const isSubset = require('is-subset');
const a = { foo: 'bar', baz: 42 };
const b = { foo: 'bar' };
console.log(isSubset(a, b)); // true
Array Subset Check
This feature allows you to check if one array is a subset of another. In this example, array `b` is a subset of array `a` because all elements in `b` exist in `a`.
const isSubset = require('is-subset');
const a = [1, 2, 3, 4];
const b = [2, 3];
console.log(isSubset(a, b)); // true
Nested Object Subset Check
This feature allows you to check if one nested object is a subset of another. In this example, object `b` is a subset of object `a` because the nested structure and values in `b` exist in `a`.
const isSubset = require('is-subset');
const a = { foo: { bar: 'baz' }, qux: 42 };
const b = { foo: { bar: 'baz' } };
console.log(isSubset(a, b)); // true
Other packages similar to is-subset
lodash.isequal
Lodash's `isEqual` function can be used to perform deep comparisons between two values to determine if they are equivalent. While it is more general-purpose and can compare any two values for equality, it does not specifically check for subset relationships.
underscore
Underscore.js provides a variety of utility functions for JavaScript, including `_.isMatch`, which can be used to check if an object contains certain key-value pairs. This is similar to checking for subsets but is more focused on object properties.
deep-equal
The `deep-equal` package allows for deep comparison between two values to determine if they are equivalent. Like `lodash.isequal`, it is more general-purpose and does not specifically check for subset relationships.
is-subset
Check if an object is contained within another one.
Installation
$ npm install is-subset
Usage
- Import the module:
import isSubset from 'is-subset/module';
var isSubset = require('is-subset');
- These are true:
isSubset(
{a: 1, b: 2},
{a: 1}
);
isSubset(
{a: 1, b: {c: 3, d: 4}, e: 5},
{a: 1, b: {c: 3}}
);
isSubset(
{a: 1, bcd: [1, 2, 3]},
{a: 1, bcd: [1, 2]}
);
…and these are false:
isSubset(
{a: 1},
{a: 2}
);
isSubset(
{a: 1},
{a: 1, b: 2}
);
isSubset(
{a: 1, bcd: [1, 2, 3]},
{a: 1, bcd: [1, 3]}
);
See the specs for more info.
API
isSubset(superset, subset)
Check if an object is contained within another object.
Returns true
if:
- all enumerable keys of subset are also enumerable in superset, and
- every value assigned to an enumerable key of subset strictly equals the value assigned to the same key of superset – or is a subset of it.
Parameters:
Object
supersetObject
subset
Return value:
License
MIT © Studio B12 GmbH