You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/**
* 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.
*
* @param {Object} superset
* @param {Object} subset
*
* @returns {Boolean}
*
* @module is-subset
* @function default
* @alias isSubset
*/
const isSubset = (superset, subset) => {
if (
(typeof superset !== 'object' || superset === null) ||
(typeof subset !== 'object' || subset === null)
) return false;
return Object.keys(subset).every((key) => {
if (!superset.propertyIsEnumerable(key)) return false;
const subsetItem = subset[key];
const supersetItem = superset[key];
if (
(typeof subsetItem === 'object' && subsetItem !== null) ?
!isSubset(supersetItem, subsetItem) :
supersetItem !== subsetItem
) return false;
return true;
});
};
export {isSubset as default};