get-diff

0.0.3 • Public • Published

get-diff

Build Status

Installation

npm install get-diff --save

Syntax

getDiff(variableA, variableB)

Usage

Diff a shallow object
import getDiff from 'get-diff';
 
const userA = {username: 'Max'};
const userB = {username: 'Jane'};
 
getDiff(userA, userB)
//  [
//    {
//      keyPath: 'username',
//      values: ['Max','Jane'],
//      types: ['isSTRING','isSTRING']
//    }
//  ]
 
Diff a deep object
import getDiff from 'get-diff';
 
const userA = {
  work: {
    'title': 'Developer'
  }
};
 
const userB = {
  work: {}
};
 
getDiff(userA, userB)
//  [
//    {
//      keyPath: 'work.title',
//      values: ['Developer', undefined],
//      types: ['isSTRING','isUNDEFINED']
//    }
//  ]
 
Diff everything...
import getDiff from 'get-diff';
 
const genderFemale = Symbol('female');
 
const userA = {
  userId: 1,
  firstname: 'Maxine',
  surname: 'Mustermann',
  age: 20,
  sex: genderFemale,
  level: Symbol('admin'),
  hobbies: ['golf', 'tennis'],
  music: ['Blur'],
  work: {
    title: 'Developer',
    senior: 'yes'
  },
  cool: false,
  happy: true,
  birthDath: new Date('1984-01-01'),
  recentLogin: new Date('2015-01-01')
};
 
const userB = {
  userId: 2,
  firstname: 'Maxine',
  surname: 'Johansson',
  age: 20,
  sex: genderFemale,
  level: Symbol('admin'),
  hobbies: ['golf', 'tennis'],
  music: ['Pulp'],
  work: {
    title: 'Developer',
    senior: 'no'
  },
  cool: false,
  happy: true,
  birthDath: new Date('1984-01-01'),
  recentLogin: new Date('2015-01-02')
};
 
getDiff(userA, userB)
//  [{
//    keyPath: 'userId',
//    values: [1, 2],
//    types: ['isNUMBER', 'isNUMBER']
//  }, {
//    keyPath: 'surname',
//    values: ['Mustermann', 'Johansson'],
//    types: ['isSTRING', 'isSTRING']
//  }, {
//    keyPath: 'level',
//    values: [{}, {}],
//    types: ['isSYMBOL', 'isSYMBOL']
//  }, {
//    keyPath: 'music.0',
//    values: ['Blur', 'Pulp'],
//    types: ['isSTRING', 'isSTRING']
//  }, {
//    keyPath: 'work.senior',
//    values: ['yes', 'no'],
//    types: ['isSTRING', 'isSTRING']
//  }, {
//    keyPath: 'recentLogin',
//    values: [Thu Jan 01 2015 01: 00: 00 GMT + 0100(CET),
//      Fri Jan 02 2015 01: 00: 00 GMT + 0100(CET)
//    ],
//    types: ['isDATE', 'isDATE']
//  }]

Tests

npm test

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.

Release History

  • 0.0.3 Fixed date bug
  • 0.0.2 Date support
  • 0.0.1 Initial alpha release

Readme

Keywords

none

Package Sidebar

Install

npm i get-diff

Weekly Downloads

1

Version

0.0.3

License

ISC

Last publish

Collaborators

  • janjarfalk