From e4bd7c29b9d54f883a6f7f8b35c8067e719ec063 Mon Sep 17 00:00:00 2001 From: Maximilian Schmitt Date: Tue, 22 Mar 2016 15:23:09 +0100 Subject: [PATCH 1/2] Add option to filter values that should be skipped --- index.js | 3 ++- test/test.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 18bae05..af5348c 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,7 @@ function flatten(target, opts) { var delimiter = opts.delimiter || '.' var maxDepth = opts.maxDepth + var filter = opts.filter || function() { return true } var currentDepth = 1 var output = {} @@ -31,7 +32,7 @@ function flatten(target, opts) { maxDepth = currentDepth + 1; } - if (!isarray && !isbuffer && isobject && Object.keys(value).length && currentDepth < maxDepth) { + if (!isarray && !isbuffer && isobject && Object.keys(value).length && currentDepth < maxDepth && filter(value)) { ++currentDepth return step(value, newKey) } diff --git a/test/test.js b/test/test.js index 7cca10c..1073c06 100644 --- a/test/test.js +++ b/test/test.js @@ -284,6 +284,56 @@ suite('Unflatten', function() { }) }) + suite('.filter', function() { + var everything = function() { return false } + var nothing = function() { return true } + var ifHasName = function(obj) { return !obj.name } + + test('Should let a custom check decide if object should be flattened', function () { + var fixture = { + hello: { + world: 'hello', + good: { + bye: 'my friend' + }, + something: { + nested: { + pretty: { + deep: true + }, + name: 'hello' + } + }, + array: ['of', 'values'] + } + } + + assert.deepEqual(flat(fixture, { filter: everything }), fixture) + + assert.deepEqual(flat(fixture, { filter: nothing }), { + 'hello.world': 'hello', + 'hello.good.bye': 'my friend', + 'hello.something.nested.pretty.deep': true, + 'hello.something.nested.name': 'hello', + 'hello.array.0': 'of', + 'hello.array.1': 'values' + }) + + assert.deepEqual(flat(fixture, { filter: ifHasName }), { + 'hello.world': 'hello', + 'hello.good.bye': 'my friend', + 'hello.something.nested': { + pretty: { + deep: true + }, + name: 'hello' + }, + 'hello.array.0': 'of', + 'hello.array.1': 'values' + }) + }) + }) + suite('.safe', function() { test('Should protect arrays when true', function() { assert.deepEqual(flatten({ From 5349c0a6db4f8eccbacc9b126a0a3516767dd526 Mon Sep 17 00:00:00 2001 From: Maximilian Schmitt Date: Tue, 22 Mar 2016 15:28:54 +0100 Subject: [PATCH 2/2] update README --- README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/README.md b/README.md index 2e4cb3c..096887c 100644 --- a/README.md +++ b/README.md @@ -158,3 +158,27 @@ flatten({ // 'key3.a': { b: { c: 2 } } // } ``` + +### filter + +Decide if a value should be flattened any further + +```javascript +var flatten = require('flat') + +flatten({ + key1: { + keyA: 'valueI' + }, + key2: { + keyB: 'valueII' + } +}, { filter: (value) => !value.keyA }) // skip key1 + +// { +// key1: { +// keyA: 'valueI' +// }, +// 'key2.keyB': 'valueII' +// } +```