Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it compatible with Node.js & browserify #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
95 changes: 95 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
function Observe(root, callback, curr, path) {
var base, curr_path, item, key, type_of_curr, _i, _len;
if (curr == null) {
curr = null;
}
if (path == null) {
path = null;
}
curr = curr || root;
type_of_curr = curr.constructor.name;
base = path;
if (type_of_curr === "Array") {
for (key = _i = 0, _len = curr.length; _i < _len; key = ++_i) {
item = curr[key];
if (typeof item === "object") {
if (base) {
path = "" + base + "[" + key + "]";
}
if (!base) {
path = "" + key;
}
new Observe(root, callback, item, path);
path = "";
}
}
}
if (type_of_curr === "Object") {
for (key in curr) {
item = curr[key];
if (typeof item === "object") {
if (base) {
path = "" + base + "." + key;
}
if (!base) {
path = "" + key;
}
new Observe(root, callback, item, path);
path = "";
}
}
}
if (curr.constructor.name === "Array") {
curr_path = path;
Array.observe(curr, function(changes) {
var original, result;
result = {};
original = {};
base = path;
changes.forEach(function(change, i) {
var part;
path = "" + base + "[" + change.index + "]";
part = {
path: curr_path,
value: change.object
};
if (change.addedCount > 0 && typeof part.value === "object") {
new Observe(root, callback, part.value, part.path);
}
result[i] = part;
return original[i] = change;
});
return callback(result, original);
});
}
if (curr.constructor.name === "Object") {
base = "" + path;
Object.observe(curr, function(changes) {
var original, result;
result = {};
original = {};
changes.forEach(function(change, i) {
var part;
curr_path = path;
if (base) {
path = "" + base + "." + change.name;
}
if (!base) {
path = "" + change.name;
}
part = {
path: path,
value: change.object[change.name]
};
if (change.type === "add" && typeof part.value === "object") {
new Observe(root, callback, part.value, part.path);
}
result[i] = part;
return original[i] = change;
});
return callback(result, original);
});
}
}

module.exports = Observe;
101 changes: 0 additions & 101 deletions lib/index.js

This file was deleted.

5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
"name": "observe",
"version": "0.0.2",
"description": "Recursive object Observer",
"main": "lib/index.js",
"scripts": {
"build": "./node_modules/.bin/coffee --bare --compile --output lib src",
"test": "node node_modules/karma/bin/karma start test/karma.config.js --browsers PhantomJS --single-run --no-auto-watch"
"test": "karma start test/karma.config.js --browsers PhantomJS --single-run --no-auto-watch"
},
"repository": {
"type": "git",
Expand All @@ -25,7 +23,6 @@
"url": "https://github.com/valtido/Observe/issues"
},
"homepage": "https://github.com/valtido/Observe",
"dependencies": {},
"devDependencies": {
"coffee-script": "^1.8.0",
"karma": "^0.12.24",
Expand Down
70 changes: 0 additions & 70 deletions src/index.coffee

This file was deleted.