Giter Site home page Giter Site logo

is-subset's Introduction

Coveralls – test coverage Travis – build status David – status of dependencies Code style: airbnb

is-subset

Check if an object is contained within another one.

Installation

$ npm install is-subset

Usage

  1. Import the module:
import isSubset from 'is-subset/module';

// …or:
var isSubset = require('is-subset');
  1. 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 superset
  • Object subset

Return value:

  • Boolean

License

MIT © Studio B12 GmbH

is-subset's People

Contributors

petergphillips avatar

Stargazers

JCPedroza avatar Raphael D. Pinheiro avatar Dingding Zhang avatar Rick Power avatar Hector Zarco  avatar Jannis R avatar Matt Blessed avatar Tobias Davis avatar Mak Jacobsen avatar Adrian Perez avatar Martin Hansen avatar Vincenzo Ferrari avatar  avatar G. Sitthi avatar Nishchal Gautam avatar Xie Guanglei avatar Saikat Guha avatar Dr. Simon Harrer avatar Oliver Kopp avatar Anton Zhukov avatar Franco Correa avatar DUNCAN MAK avatar Shahar "Dawn" Or avatar Jack Allan avatar Denny Harijanto avatar  avatar Eirik L. Vullum avatar Tomek Wiszniewski avatar

Watchers

James Cloos avatar

is-subset's Issues

is-subset always returns true for date values

For values that are dates is-subset will ignore the values and return true when the dates are different.

For example:

is-subset(
    { date, new Date(0)}
    { date, new Date()}
)

will return true even though the dates are different. Looking at the code this is because Object.keys() for a Date will return an empty array.

Something like

  if (
    superset instanceof Date && subset instanceof Date
  ) return superset.valueOf() === subset.valueOf();

after the typeof object check should fix it. I started trying to fix the issue but couldn't get any of the tests to run - getting TypeError: undefined is not a function for each test.

isSubset returns false when a child array's component is of a different order

It returns false on the following case, where it should've been true.

SUPERSET:
[ { id: 1,
short_name: 'apple',
long_name: 'pen pinapple apple pen',
picture_urls: [ 'fake url 1a', 'fake url 1b' ] },
{ id: 2,
short_name: 'banana',
long_name: 'cavendish banana',
picture_urls: [ 'fake url 2b', 'fake url 2a' ] } ]

SUBSET:
[ { short_name: 'apple',
long_name: 'pen pinapple apple pen',
picture_urls: [ 'fake url 1a', 'fake url 1b' ] },
{ short_name: 'banana',
long_name: 'cavendish banana',
picture_urls: [ 'fake url 2a', 'fake url 2b' ] } ]

Shallow variant

Would it be possible to get a flag or variant of this which doesn't work recursively? Specifically, from the examples in the README, the following to be false, not true:

isSubset(
  {a: 1, bcd: [1, 2, 3]},
  {a: 1, bcd: [1, 2]}
);

I.e. false because the value (the array) of bcd is not equal.

Decide on the final API

There seems to be some disagreement in the community as to where data should land.

  1. Some say data should come last. Not without reason.
  2. There’s a new function bind proposal which can result in very elegant code. There seems to be a lot of excitement about that.
  3. At the moment we have traditional here – data as the first parameter.

Should we keep it this way (3)? Should we go with the flow (2)? Should we perhaps allow different APIs for different purposes?

const isSubset = require('is-subset/data-last');     // 1
const isSubset = require('is-subset/data-as-this');  // 2
const isSubset = require('is-subset');               // 3

I hope for some discussion here :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.