From 727f8d6c43b97062fe65a9e8673b2a0dcb7ec605 Mon Sep 17 00:00:00 2001 From: Rodolfo Ribeiro Barreto Date: Mon, 26 Nov 2018 21:52:08 -0200 Subject: [PATCH 1/3] Add key and value interceptor callback --- README.md | 20 ++++++++++++++++++-- index.js | 8 ++++++++ test/test.js | 24 ++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3295f75..7e21929 100644 --- a/README.md +++ b/README.md @@ -159,9 +159,25 @@ flatten({ // } ``` +### Key interceptor for flatten + +Give the possibility to manipulate the key (Ex: Translate) + +``` javascript +var flatten = require('flat') +var data = { name: 'Rodolfo', age: 29 }; + +flatten(data, { maxDepth: 2, keyInterceptor: key => I18n.t(`myModule.${key}`) }) + +// { +// 'Nome completo': 'Rodolfo', +// 'Idade': 29 +// } +``` + ## Command Line Usage -`flat` is also available as a command line tool. You can run it with +`flat` is also available as a command line tool. You can run it with [`npx`](https://ghub.io/npx): ```sh @@ -169,7 +185,7 @@ npx flat foo.json ``` Or install the `flat` command globally: - + ```sh npm i -g flat && flat foo.json ``` diff --git a/index.js b/index.js index ca8904a..ae2fc60 100644 --- a/index.js +++ b/index.js @@ -32,6 +32,14 @@ function flatten (target, opts) { return step(value, newKey, currentDepth + 1) } + if (opts.keyInterceptor) { + newKey = opts.keyInterceptor(newKey) + } + + if (opts.valueInterceptor) { + value = opts.valueInterceptor(value) + } + output[newKey] = value }) } diff --git a/test/test.js b/test/test.js index 541c9d7..1f4222c 100644 --- a/test/test.js +++ b/test/test.js @@ -510,4 +510,28 @@ suite('Arrays', function () { } }) }) + + test('Should be translate the keys', function () { + var originalData = { + name: 'Rodolfo', age: 27 + }; + + var expectedData = { + 'Nome': 'Rodolfo', + 'Idade': 27 + }; + + var translates = { + name: 'Nome', + age: 'Idade' + }; + + var options = { + keyInterceptor: function(key) { + return translates[key]; + } + } + + assert.deepEqual(flatten(originalData, options), expectedData); + }) }) From 4025ceba4388d260ff2c3f1033af8c57fd23e0eb Mon Sep 17 00:00:00 2001 From: Rodolfo Ribeiro Barreto Date: Mon, 26 Nov 2018 22:02:44 -0200 Subject: [PATCH 2/3] Add value test --- index.js | 2 +- test/test.js | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index ae2fc60..49e0b26 100644 --- a/index.js +++ b/index.js @@ -37,7 +37,7 @@ function flatten (target, opts) { } if (opts.valueInterceptor) { - value = opts.valueInterceptor(value) + value = opts.valueInterceptor(newKey, value) } output[newKey] = value diff --git a/test/test.js b/test/test.js index 1f4222c..a65f708 100644 --- a/test/test.js +++ b/test/test.js @@ -534,4 +534,22 @@ suite('Arrays', function () { assert.deepEqual(flatten(originalData, options), expectedData); }) + + test('Should be sum the value', function () { + var originalData = { + name: 'Rodolfo', age: 20 + }; + + var expectedData = { + name: 'Rodolfo', age: 50 + }; + + var options = { + valueInterceptor: function(key, value) { + return key === 'age' ? value + 30 : value + } + } + + assert.deepEqual(flatten(originalData, options), expectedData); + }) }) From f6802afae2e059dae35e11334908386b213d053d Mon Sep 17 00:00:00 2001 From: Rodolfo Ribeiro Barreto Date: Mon, 26 Nov 2018 22:22:09 -0200 Subject: [PATCH 3/3] Move interception functions to manipulate key by key --- index.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 49e0b26..8bc0102 100644 --- a/index.js +++ b/index.js @@ -23,14 +23,7 @@ function flatten (target, opts) { type === '[object Array]' ) - var newKey = prev - ? prev + delimiter + key - : key - - if (!isarray && !isbuffer && isobject && Object.keys(value).length && - (!opts.maxDepth || currentDepth < maxDepth)) { - return step(value, newKey, currentDepth + 1) - } + var newKey = key if (opts.keyInterceptor) { newKey = opts.keyInterceptor(newKey) @@ -40,6 +33,15 @@ function flatten (target, opts) { value = opts.valueInterceptor(newKey, value) } + newKey = prev + ? prev + delimiter + newKey + : newKey + + if (!isarray && !isbuffer && isobject && Object.keys(value).length && + (!opts.maxDepth || currentDepth < maxDepth)) { + return step(value, newKey, currentDepth + 1) + } + output[newKey] = value }) }