From 70be7d66e731149f110a33340b88b62f83a4570a Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Sat, 31 Oct 2015 11:34:27 +1300 Subject: [PATCH 1/5] Add option prefix, which adds a prefix to each key. --- index.js | 6 ++++-- test/test.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 4d6914a..eddb712 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,7 @@ function flatten(target, opts) { var delimiter = opts.delimiter || '.' var maxDepth = opts.maxDepth + var prefix = opts.prefix || '' var currentDepth = 1 var output = {} @@ -23,7 +24,7 @@ function flatten(target, opts) { var newKey = prev ? prev + delimiter + key - : key + : prefix + key if (!opts.maxDepth) { maxDepth = currentDepth + 1; @@ -48,6 +49,7 @@ function unflatten(target, opts) { var delimiter = opts.delimiter || '.' var overwrite = opts.overwrite || false + var prefix = opts.prefix || '' var result = {} var isbuffer = isBuffer(target) @@ -68,7 +70,7 @@ function unflatten(target, opts) { } Object.keys(target).forEach(function(key) { - var split = key.split(delimiter) + var split = key.slice(prefix.length).split(delimiter) var key1 = getkey(split.shift()) var key2 = getkey(split[0]) var recipient = result diff --git a/test/test.js b/test/test.js index 707901b..ef43b18 100644 --- a/test/test.js +++ b/test/test.js @@ -103,6 +103,21 @@ suite('Flatten', function() { }) }) + test('Custom Prefix', function() { + assert.deepEqual(flatten({ + hello: { + world: { + again: 'good morning' + } + } + }, { + prefix: 'urn:', + delimiter: ':' + }), { + 'urn:hello:world:again': 'good morning' + }) + }) + test('Empty Objects', function() { assert.deepEqual(flatten({ hello: { @@ -215,6 +230,21 @@ suite('Unflatten', function() { })) }) + test('Custom Prefix', function() { + assert.deepEqual({ + hello: { + world: { + again: 'good morning' + } + } + }, unflatten({ + 'eh, hello world again': 'good morning' + }, { + delimiter: ' ', + prefix: 'eh, ' + })) + }) + test('Overwrite', function() { assert.deepEqual({ travis: { From 32d9dfb5b04ad99e484a0d2eff2126ab239a1063 Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Sat, 31 Oct 2015 11:42:11 +1300 Subject: [PATCH 2/5] Delimiter option defaults to prefix if it is defined. --- index.js | 4 ++-- test/test.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index eddb712..edb8fcc 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,7 @@ flatten.unflatten = unflatten function flatten(target, opts) { opts = opts || {} - var delimiter = opts.delimiter || '.' + var delimiter = opts.delimiter || opts.prefix || '.' var maxDepth = opts.maxDepth var prefix = opts.prefix || '' var currentDepth = 1 @@ -47,7 +47,7 @@ function flatten(target, opts) { function unflatten(target, opts) { opts = opts || {} - var delimiter = opts.delimiter || '.' + var delimiter = opts.delimiter || opts.prefix || '.' var overwrite = opts.overwrite || false var prefix = opts.prefix || '' var result = {} diff --git a/test/test.js b/test/test.js index ef43b18..fd743c5 100644 --- a/test/test.js +++ b/test/test.js @@ -118,6 +118,20 @@ suite('Flatten', function() { }) }) + test('Custom Delimiter defaults to Prefix', function() { + assert.deepEqual(flatten({ + hello: { + world: { + again: 'good morning' + } + } + }, { + prefix: ':', + }), { + ':hello:world:again': 'good morning' + }) + }) + test('Empty Objects', function() { assert.deepEqual(flatten({ hello: { @@ -245,6 +259,20 @@ suite('Unflatten', function() { })) }) + test('Custom Delimiter defaults to Prefix', function() { + assert.deepEqual({ + hello: { + world: { + again: 'good morning' + } + } + }, unflatten({ + ':hello:world:again': 'good morning' + }, { + prefix: ':' + })) + }) + test('Overwrite', function() { assert.deepEqual({ travis: { From e3461b0651b5a86585f45b56f3b163dd4fa75820 Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Sat, 31 Oct 2015 12:20:41 +1300 Subject: [PATCH 3/5] Update README.md --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 2e4cb3c..43b6634 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,10 @@ unflatten({ Use a custom delimiter for (un)flattening your objects, instead of `.`. +### prefix + +Use a custom prefix for (un)flattening your objects, instead of ``. The prefix is added to each key name. `delimiter` default to this value if it is defined. + ### safe When enabled, both `flat` and `unflatten` will preserve arrays and their From 5dc7238f864d0b1b0233573464a11aa83ada4e81 Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Sat, 31 Oct 2015 12:22:45 +1300 Subject: [PATCH 4/5] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 43b6634..b3c5d56 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ Use a custom delimiter for (un)flattening your objects, instead of `.`. ### prefix -Use a custom prefix for (un)flattening your objects, instead of ``. The prefix is added to each key name. `delimiter` default to this value if it is defined. +Use a custom prefix for (un)flattening your objects. The `prefix` is added to the start each key name. `delimiter` defaults to this value if it is defined. ### safe From 4e6a8c09478abab9e188d3314c498631a7ab3e8d Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Sat, 31 Oct 2015 12:23:55 +1300 Subject: [PATCH 5/5] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b3c5d56..9f22806 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ Use a custom delimiter for (un)flattening your objects, instead of `.`. ### prefix -Use a custom prefix for (un)flattening your objects. The `prefix` is added to the start each key name. `delimiter` defaults to this value if it is defined. +Use a custom prefix for (un)flattening your objects. The `prefix` is added to the start of each key name. `delimiter` defaults to this value if it is defined. ### safe