Skip to content

Commit

Permalink
stubs for non-es5 browsers, didn't break any unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Aug 23, 2011
1 parent 9ebde92 commit 559a6f1
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Traverse.prototype.clone = function () {
parents.push(src);
nodes.push(dst);

Object.keys(src).forEach(function (key) {
Object_keys(src).forEach(function (key) {
dst[key] = clone(src[key]);
});

Expand Down Expand Up @@ -126,7 +126,7 @@ function walk (root, cb, immutable) {
delete state.parent.node[state.key];
},
remove : function () {
if (Array.isArray(state.parent.node)) {
if (Array_isArray(state.parent.node)) {
state.parent.node.splice(state.key, 1);
}
else {
Expand All @@ -145,7 +145,7 @@ function walk (root, cb, immutable) {
if (!alive) return state;

if (typeof node === 'object' && node !== null) {
state.keys = Object.keys(node);
state.keys = Object_keys(node);

state.isLeaf = state.keys.length == 0;

Expand Down Expand Up @@ -201,7 +201,16 @@ function walk (root, cb, immutable) {
})(root).node;
}

Object.keys(Traverse.prototype).forEach(function (key) {
var Object_keys = Object.keys || function keys (obj) {
var res = [];
for (var key in obj) res.push(key)
return res;
};

var Array_isArray = Array.isArray || function isArray (xs) {
return Object.prototype.toString.call(xs) === '[object Array]';
};
Object_keys(Traverse.prototype).forEach(function (key) {
Traverse[key] = function (obj) {
var args = [].slice.call(arguments, 1);
var t = Traverse(obj);
Expand All @@ -213,7 +222,7 @@ function copy (src) {
if (typeof src === 'object' && src !== null) {
var dst;

if (Array.isArray(src)) {
if (Array_isArray(src)) {
dst = [];
}
else if (src instanceof Date) {
Expand All @@ -228,11 +237,18 @@ function copy (src) {
else if (src instanceof String) {
dst = new String(src);
}
else {
else if (Object.create && Object.getPrototypeOf) {
dst = Object.create(Object.getPrototypeOf(src));
}
else if (obj.__proto__ || obj.constructor.prototype) {
var proto = obj.__proto__ || obj.constructor.prototype || {};
var T = function () {};
T.prototype = proto;
dst = new T;
if (!dst.__proto__) dst.__proto__ = proto;
}

Object.keys(src).forEach(function (key) {
Object_keys(src).forEach(function (key) {
dst[key] = src[key];
});
return dst;
Expand Down

0 comments on commit 559a6f1

Please sign in to comment.