Socket
Book a DemoInstallSign in
Socket

object-looks-like

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

object-looks-like

An utility function for comparing if object A looks like object B.

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

object-looks-like

An utility function for comparing if object A looks like object B.

objectLooksLike is not shallow comparing nor deep comparing object A and object B. Rather, it iterates properties of object B and checks if they are matching object A properties.

Usage

const objectLooksLike = require('./index');

const objectA = {
  name: 'Mike',
  age: 21,
  height: '180cm',
  weight: '60kg',
  hobbies: ['basketball', 'football', 'swimming'],
  parent: {
    name: 'Daniel',
    age: 55,
    height: '175cm',
    weight: '50kg',
    hobbies: ['reading'],
    parent: null,
  },
};

const objectB = {
  name: 'Mike',
  age: 21,
  hobbies: ['basketball', 'football', 'swimming'],
  parent: {
    name: 'Daniel',
    age: 55,
  },
};

// Returns true because objectB is a subset of objectA.
objectLooksLike(objectA, objectB);

const objectC = {
  name: 'Mike',
  age: age => age < 50, // value can be a function
  parent: {
    name: 'Daniel',
    age: 55,
  },
};

// Returns true because objectC is a subset of objectA.
objectLooksLike(objectA, objectC);

const objectD = {
  name: 'Mike',
  age: 21,
  parent: {
    name: 'Sam', // unmatching property value
    age: 55,
  },
};

// Returns false because objectD has unmatching property value.
objectLooksLike(objectA, objectD);

const objectE = {
  name: 'Mike',
  age: 21,
  skin: 'white', // unknown property
  parent: {
    name: 'Daniel',
    age: 55,
  },
};

// Returns false because objectE has unknown property.
objectLooksLike(objectA, objectE);

Keywords

compare

FAQs

Package last updated on 19 Feb 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.