Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
presets: [
['@babel/preset-env', {
targets: {
node: '6.12.0'
}
}]
]
}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ results
.DS_Store
npm-debug.log
node_modules
secrets
secrets
dist
29 changes: 17 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
{
"name": "objectDiff",
"version": "0.0.0",
"name": "just-the-diff",
"description": "A diff tool that returns just the keys and values that have changed, nothing else. No meta data.",
"version": "0.1.0",
"description": "get diff object",
"entry": "dist/index.js",
"repository": {
"type": "git",
"url": "https://github.com/Evaw/objectDiff"
},
"scripts": {
"prepublish": "./node_modules/.bin/babel ./src -d dist --ignore node_modules --ignore dist",
"test": "grunt qunit"
},
"author": "",
"author": [
"Evaw (https://github.com/Evaw)",
"Noah Gray <noahgray@me.com> (https://github.com/OKNoah)"
],
"devDependencies": {
"grunt": "~0.4.2",
"grunt-browserify": "~1.3.1",
"grunt-contrib-clean": "~0.5.0",
"grunt-contrib-jshint": "~0.8.0",
"grunt-contrib-less": "~0.9.0",
"grunt-contrib-qunit": "~0.4.0",
"grunt-contrib-uglify": "~0.3.2",
"grunt-contrib-watch": "~0.5.3",
"grunt-debug-task": "~0.1.3",
"@babel/cli": "^7.0.0-beta.36",
"@babel/core": "^7.0.0-beta.36",
"@babel/preset-env": "^7.0.0-beta.36",
"babel-eslint": "^8.1.2",
"mocha": "^2.3.3"
}
}
46 changes: 46 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const isArray = function () {
if(Array && Array.isArray) {
return Array.isArray
}
return function (o) {
return Object.prototype.toString.call(o) === '[object Array]'
}
}

const getEquivalentEmpty = function (objOrArr) {
return isArray(objOrArr) ? [] : {}
}

const getFirstExtraKeys = function (first, second) {
let i
let curExtra
let childExtra
for (i in first) {
if (first.hasOwnProperty(i)) {
if (first[i] !== null && typeof first[i] === "object") {
/*
object includes arrays and objects, null is not
considered an object, so we make sure its not null
*/
childExtra = getFirstExtraKeys(first[i], (second || {})[i])
if (childExtra !== undefined) {
//curExtra = curExtra || {}
curExtra = curExtra || getEquivalentEmpty(first)
curExtra[i] = childExtra
}
} else {
if(second === undefined || second === null || second[i] !== first[i]){
//curExtra = curExtra || {}
curExtra = curExtra || getEquivalentEmpty(first)
curExtra[i] = first[i]
}
}
}
}
return curExtra
}

export default function (first, second) {
const res = getFirstExtraKeys({itm: first}, {itm: second})
return (res || {}).itm
}
46 changes: 0 additions & 46 deletions src/js/objectDiff.js

This file was deleted.

38 changes: 18 additions & 20 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/*global test:false, ok:false, deepEqual:false, objectDiff:false*/
"use strict";
var assert = require('assert');
var objectDiff = require('./src/js/objectDiff.js');
var deepEqual = assert.deepEqual;
var ok = assert.ok;
/* global test:false, ok:false, deepEqual:false, objectDiff:false */

var assert = require('assert')
var objectDiff = require('./dist/index.js').default
const deepEqual = assert.deepEqual
const ok = assert.ok

it('first has extra stuff', function () {
var a = {
let a = {
a: "hello"
},
b = {};
Expand Down Expand Up @@ -70,15 +71,14 @@ it('first has extra stuff', function () {

});
it('completely different', function () {
var a,b;
a = {
const a = {
steel: {
sword: {
weight: 20
}
}
};
b = {
const b = {
steel: {
train: {
contains: "coal"
Expand All @@ -89,7 +89,7 @@ it('completely different', function () {

});
it('eq obj', function () {
var check = function (obj) {
const check = function (obj) {
ok(objectDiff(obj, obj) === undefined);
};
check(null);
Expand All @@ -107,25 +107,24 @@ it('eq obj', function () {
});
});
it('arrays', function () {
var a = [1,2,3];
var b = [1];
var ans = [];
const a = [1,2,3];
const b = [1];
const ans = [];
ans[1] = 2;
ans[2] = 3;
deepEqual(objectDiff(a,b), ans);
});
it('array object mix', function(){
var a = {
let a = {
"arr": [1,2,3],
"k": "l"
};
var b;
var ans = a;
let b = undefined
let ans = a;
deepEqual(objectDiff(a,b), ans);
ans = undefined;
deepEqual(objectDiff(b,a), ans);


a = {
arr: [{o:"p"}]
};
Expand Down Expand Up @@ -160,5 +159,4 @@ it('array object mix', function(){
arr2: [{r: "a"}]
};
deepEqual(objectDiff(a,b), ans);

});
});
Loading