Comparing version 2.9.1 to 2.10.0
@@ -174,28 +174,48 @@ // Load modules | ||
// Move shallow copy items to storage | ||
var storage = internals.store(source, keys); // Move shallow copy items to storage | ||
var copy = exports.clone(source); // Deep copy the rest | ||
internals.restore(copy, source, storage); // Shallow copy the stored items and restore | ||
return copy; | ||
}; | ||
internals.store = function (source, keys) { | ||
var storage = {}; | ||
for (var i = 0, il = keys.length; i < il; ++i) { | ||
var key = keys[i]; | ||
if (source.hasOwnProperty(key)) { | ||
storage[key] = source[key]; | ||
source[key] = undefined; | ||
var value = exports.reach(source, key); | ||
if (value !== undefined) { | ||
storage[key] = value; | ||
internals.reachSet(source, key, undefined); | ||
} | ||
} | ||
// Deep copy the rest | ||
return storage; | ||
}; | ||
var copy = exports.clone(source); | ||
// Shallow copy the stored items and restore | ||
internals.restore = function (copy, source, storage) { | ||
for (i = 0; i < il; ++i) { | ||
key = keys[i]; | ||
if (storage.hasOwnProperty(key)) { | ||
source[key] = storage[key]; | ||
copy[key] = storage[key]; | ||
var keys = Object.keys(storage); | ||
for (var i = 0, il = keys.length; i < il; ++i) { | ||
var key = keys[i]; | ||
internals.reachSet(copy, key, storage[key]); | ||
internals.reachSet(source, key, storage[key]); | ||
} | ||
}; | ||
internals.reachSet = function (obj, key, value) { | ||
var path = key.split('.'); | ||
var ref = obj; | ||
for (var i = 0, il = path.length; i < il; ++i) { | ||
var segment = path[i]; | ||
if (i + 1 === il) { | ||
ref[segment] = value; | ||
} | ||
ref = ref[segment]; | ||
} | ||
return copy; | ||
}; | ||
@@ -222,27 +242,5 @@ | ||
// Move shallow copy items to storage | ||
var storage = {}; | ||
for (var i = 0, il = keys.length; i < il; ++i) { | ||
var key = keys[i]; | ||
if (options.hasOwnProperty(key)) { | ||
storage[key] = options[key]; | ||
options[key] = undefined; | ||
} | ||
} | ||
// Deep copy the rest | ||
exports.merge(copy, options, false, false); | ||
// Shallow copy the stored items | ||
for (i = 0; i < il; ++i) { | ||
key = keys[i]; | ||
if (storage.hasOwnProperty(key)) { | ||
options[key] = storage[key]; | ||
copy[key] = storage[key]; | ||
} | ||
} | ||
var storage = internals.store(options, keys); // Move shallow copy items to storage | ||
exports.merge(copy, options, false, false); // Deep copy the rest | ||
internals.restore(copy, options, storage); // Shallow copy the stored items and restore | ||
return copy; | ||
@@ -249,0 +247,0 @@ }; |
{ | ||
"name": "hoek", | ||
"description": "General purpose node utilities", | ||
"version": "2.9.1", | ||
"version": "2.10.0", | ||
"repository": "git://github.com/hapijs/hoek", | ||
@@ -6,0 +6,0 @@ "main": "index", |
@@ -709,2 +709,140 @@ // Load modules | ||
it('shallow copies the nested keys (override)', function (done) { | ||
var defaults = { | ||
a: { | ||
b: 5 | ||
}, | ||
c: { | ||
d: 7, | ||
g: 1 | ||
} | ||
}; | ||
var options = { | ||
a: { | ||
b: 4 | ||
}, | ||
c: { | ||
d: 6, | ||
g: { | ||
h: 8 | ||
} | ||
} | ||
}; | ||
var merged = Hoek.applyToDefaultsWithShallow(defaults, options, ['c.g']); | ||
expect(merged).to.deep.equal({ a: { b: 4 }, c: { d: 6, g: { h: 8 } } }); | ||
expect(merged.c.g).to.equal(options.c.g); | ||
done(); | ||
}); | ||
it('shallow copies the nested keys (missing)', function (done) { | ||
var defaults = { | ||
a: { | ||
b: 5 | ||
} | ||
}; | ||
var options = { | ||
a: { | ||
b: 4 | ||
}, | ||
c: { | ||
g: { | ||
h: 8 | ||
} | ||
} | ||
}; | ||
var merged = Hoek.applyToDefaultsWithShallow(defaults, options, ['c.g']); | ||
expect(merged).to.deep.equal({ a: { b: 4 }, c: { g: { h: 8 } } }); | ||
expect(merged.c.g).to.equal(options.c.g); | ||
done(); | ||
}); | ||
it('shallow copies the nested keys (override)', function (done) { | ||
var defaults = { | ||
a: { | ||
b: 5 | ||
}, | ||
c: { | ||
g: { | ||
d: 7 | ||
} | ||
} | ||
}; | ||
var options = { | ||
a: { | ||
b: 4 | ||
}, | ||
c: { | ||
g: { | ||
h: 8 | ||
} | ||
} | ||
}; | ||
var merged = Hoek.applyToDefaultsWithShallow(defaults, options, ['c.g']); | ||
expect(merged).to.deep.equal({ a: { b: 4 }, c: { g: { h: 8 } } }); | ||
expect(merged.c.g).to.equal(options.c.g); | ||
done(); | ||
}); | ||
it('shallow copies the nested keys (deeper)', function (done) { | ||
var defaults = { | ||
a: { | ||
b: 5 | ||
} | ||
}; | ||
var options = { | ||
a: { | ||
b: 4 | ||
}, | ||
c: { | ||
g: { | ||
r: { | ||
h: 8 | ||
} | ||
} | ||
} | ||
}; | ||
var merged = Hoek.applyToDefaultsWithShallow(defaults, options, ['c.g.r']); | ||
expect(merged).to.deep.equal({ a: { b: 4 }, c: { g: { r: { h: 8 } } } }); | ||
expect(merged.c.g.r).to.equal(options.c.g.r); | ||
done(); | ||
}); | ||
it('shallow copies the nested keys (not present)', function (done) { | ||
var defaults = { | ||
a: { | ||
b: 5 | ||
} | ||
}; | ||
var options = { | ||
a: { | ||
b: 4 | ||
}, | ||
c: { | ||
g: { | ||
r: { | ||
h: 8 | ||
} | ||
} | ||
} | ||
}; | ||
var merged = Hoek.applyToDefaultsWithShallow(defaults, options, ['x.y']); | ||
expect(merged).to.deep.equal({ a: { b: 4 }, c: { g: { r: { h: 8 } } } }); | ||
done(); | ||
}); | ||
it('shallow copies the listed keys in the defaults', function (done) { | ||
@@ -711,0 +849,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
141620
2427