underscore.deep
Advanced tools
Comparing version 0.0.1 to 0.0.2
{ | ||
"name": "underscore.deep", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Underscore mixins for deeply nested objects", | ||
@@ -5,0 +5,0 @@ "main": "underscore.deep.js", |
@@ -31,15 +31,15 @@ # underscore.deep | ||
### _.flatten(obj) | ||
### _.deepToFlat(obj) | ||
Takes an object and produces a new object with no nested objects, converting any nested objects to sets of fields with dot-notation keys, recursively. | ||
describe '_.flatten', -> | ||
describe '_.deepToFlat', -> | ||
it 'does nothing with shallow objects', -> | ||
assert.deepEqual _.flatten({}), {} | ||
assert.deepEqual _.flatten( shallow: 1 ), shallow: 1 | ||
assert.deepEqual _.deepToFlat({}), {} | ||
assert.deepEqual _.deepToFlat( shallow: 1 ), shallow: 1 | ||
it 'flattens nested objects', -> | ||
assert.deepEqual _.flatten( deeply: { nested: 2 } ), 'deeply.nested': 2 | ||
assert.deepEqual _.flatten( | ||
it 'deepToFlats nested objects', -> | ||
assert.deepEqual _.deepToFlat( deeply: { nested: 2 } ), 'deeply.nested': 2 | ||
assert.deepEqual _.deepToFlat( | ||
user1: | ||
@@ -55,16 +55,16 @@ name: | ||
### _.deepen(obj) | ||
### _.deepFromFlat(obj) | ||
Takes an object and produces a new object with no dot-notation keys, converting any set of dot-notation keys with the same prefix to a nested object, recursively. | ||
**Warning:** Any keys with a dot (`.`) in the input object will be converted to nested objects, so if you use dots in your keys you may want to replace them before you deepen. | ||
**Warning:** Any keys with a dot (`.`) in the input object will be converted to nested objects, so if you use dots in your keys you may want to replace them before you call `_.deepFromFlat`. | ||
describe '_.deepen', -> | ||
describe '_.deepFromFlat', -> | ||
it 'does nothing with objects with no dot-notation', -> | ||
assert.deepEqual _.deepen({}), {} | ||
assert.deepEqual _.deepen( shallow: 1 ), shallow: 1 | ||
assert.deepEqual _.deepFromFlat({}), {} | ||
assert.deepEqual _.deepFromFlat( shallow: 1 ), shallow: 1 | ||
it 'deepens a flat object', -> | ||
assert.deepEqual _.deepen( 'deeply.nested': 2 ), deeply: { nested: 2 } | ||
assert.deepEqual _.deepen( | ||
it 'deepFromFlats a flat object', -> | ||
assert.deepEqual _.deepFromFlat( 'deeply.nested': 2 ), deeply: { nested: 2 } | ||
assert.deepEqual _.deepFromFlat( | ||
'user1.name.first': 'Deep' | ||
@@ -80,17 +80,17 @@ 'user1.name.last': 'Blue' | ||
### _.flatten and _.deepen | ||
### _.deepToFlat and _.deepFromFlat | ||
Taken as a pair, `_.flatten` and `_.deepen` have an interesting relationship: | ||
Taken as a pair, `_.deepToFlat` and `_.deepFromFlat` have an interesting relationship: | ||
describe '_.flatten and _.deepen', -> | ||
describe '_.deepToFlat and _.deepFromFlat', -> | ||
it 'they undo each other', -> | ||
deepObj = a: 1, b: { c: 2 } | ||
flatObj = a: 1, 'b.c': 2 | ||
assert.deepEqual flatObj, _.flatten deepObj | ||
assert.deepEqual deepObj, _.deepen flatObj | ||
assert.deepEqual flatObj, _.deepToFlat deepObj | ||
assert.deepEqual deepObj, _.deepFromFlat flatObj | ||
They are inverses (of a sort)! We can reformulate this as a property that holds for any `flatObj` and `deepObj`: | ||
assert.deepEqual flatObj, _.flatten _.deepen flatObj | ||
assert.deepEqual deepObj, _.deepen _.flatten deepObj | ||
assert.deepEqual flatObj, _.deepToFlat _.deepFromFlat flatObj | ||
assert.deepEqual deepObj, _.deepFromFlat _.deepToFlat deepObj | ||
@@ -109,3 +109,3 @@ ### _.deepClone(obj) | ||
'than astronomy is about telescopes.' | ||
sayer: 'Dikstra' | ||
sayer: 'Dijkstra' | ||
pools: [ | ||
@@ -124,4 +124,4 @@ { depth: 10 } | ||
it 'is equivalent to the composition of _.deepen, _.clone, and _.flatten', -> | ||
copy2 = _.deepen _.clone _.flatten orig | ||
it 'is equivalent to the composition of _.deepFromFlat, _.clone, and _.deepToFlat', -> | ||
copy2 = _.deepFromFlat _.clone _.deepToFlat orig | ||
assert.deepEqual copy2, orig | ||
@@ -128,0 +128,0 @@ assert.notEqual copy2, orig |
@@ -85,3 +85,3 @@ // Generated by CoffeeScript 1.6.3 | ||
}, | ||
flatten: function(obj) { | ||
deepToFlat: function(obj) { | ||
var recurse, res; | ||
@@ -106,3 +106,3 @@ res = {}; | ||
}, | ||
deepen: function(o) { | ||
deepFromFlat: function(o) { | ||
var k, key, oo, part, parts, t; | ||
@@ -109,0 +109,0 @@ oo = {}; |
Sorry, the diff of this file is not supported yet
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
17348