Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial checkin

  • Loading branch information...
commit d22305d1ff49b7fa4fe088d64aec7a99ea701403 0 parents
Enno Boland authored
Showing with 137 additions and 0 deletions.
  1. +23 −0 Makefile
  2. +53 −0 README.md
  3. +1 −0  index.js
  4. +35 −0 lib/objdiff.js
  5. +25 −0 package.json
23 Makefile
@@ -0,0 +1,23 @@
+
+REPORTER = dot
+
+all: build build-dev
+
+build:
+ @mkdir -p dist
+ @./node_modules/.bin/browserbuild \
+ -g ObjDiff \
+ -m objdiff -b lib/ \
+ lib > dist/objdiff.js
+
+build-dev:
+ @mkdir -p dist
+ @./node_modules/.bin/browserbuild \
+ -g ObjDiff \
+ -d -m objdiff -b lib/ \
+ lib > dist/objdiff-dev.js
+
+clean:
+ @rm -r dist
+
+.PHONY: clean
53 README.md
@@ -0,0 +1,53 @@
+[![Flattr this git repo](http://api.flattr.com/button/flattr-badge-large.png)](https://flattr.com/submit/auto?user_id=Gottox&url=https://github.com/Gottox/node-objdiff&title=node-objdiff&language=&tags=github&category=software)
+
+
+### ObjDiff
+
+This node library implements a very simple diff algorithm for javascript/json objects
+
+## Installation
+
+### Node
+```
+# npm install objdiff
+```
+
+### Browser
+
+```
+# git clone git://github.com/Gottox/node-objdiff.git
+# cd node-objdiff
+# make build
+```
+
+This compiles a [browserbuild](https://github.com/LearnBoost/browserbuild)
+version to dist/objdiff
+
+
+## Usage
+
+### Node
+
+``` javascript
+var objdiff = require('urlify').objdiff
+objdiff({a:1}, {b:2});
+```
+
+### Browser
+
+``` html
+<!DOCTYPE html>
+<html>
+ <head>
+ <meta charset="utf-8" />
+ <title></title>
+ <script type="text/javascript" src="dist/objdiff.js"></script>
+ </head>
+ <body>
+ <script type="text/javascript">
+ var urlify = ObjDiff.objdiff
+ document.write(JSON.stringify(objdiff({a:1}, {b:2})));
+ </script>
+ </body>
+</html>
+```
1  index.js
@@ -0,0 +1 @@
+module.exports = require('./lib/objdiff');
35 lib/objdiff.js
@@ -0,0 +1,35 @@
+var util = require('util');
+
+var diffbody = function(oldObj, newObj, key, diff) {
+ if((key in oldObj) !== (key in newObj) || oldObj[key] !== newObj[key]) {
+ diff[key] = { '$changed': true };
+ if(key in oldObj)
+ diff[key]['-'] = oldObj[key];
+ if(key in newObj)
+ diff[key]['+'] = newObj[key];
+ }
+ else if(oldObj[key] instanceof Object || oldObj[key] instanceof Array) {
+ var subDiff = objdiff(oldObj[key], newObj[key]);
+ for(var subKey in subDiff) {
+ diff[key] = subDiff;
+ break;
+ }
+ }
+}
+
+exports.objdiff = function(oldObj, newObj) {
+ var diff = {}
+ for(var key in oldObj) {
+ diffbody(oldObj, newObj, key, diff);
+ }
+ for(var key in newObj) {
+ if(!(key in oldObj))
+ diffbody(oldObj, newObj, key, diff);
+ }
+
+ return diff;
+}
+
+exports.isdiff = function(obj) {
+ return obj.$changed === true;
+}
25 package.json
@@ -0,0 +1,25 @@
+{
+ "name": "objdiff",
+ "version": "0.1",
+ "author": "Enno Boland <eb@s01.de>",
+ "description": "Diffs two javascript objects.",
+ "main": "./index",
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/Gottox/node-objdiff.git"
+ },
+ "keywords": [
+ "url",
+ "urlify"
+ ],
+ "dependencies" : {
+ },
+ "devDependencies": {
+ "browserbuild": "*"
+ },
+ "scripts" : { "test" : "make test" },
+ "license": "MIT",
+ "engine": {
+ "node": ">=0.6"
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.