Comparing version 5.1.0 to 5.2.0
21
index.js
@@ -13,5 +13,6 @@ var isBuffer = require('is-buffer') | ||
var coercion = opts.coercion | ||
var filters = opts.filters | ||
var output = {} | ||
function transform (coercion, key, value) { | ||
function transform (key, value) { | ||
if (!coercion) { return value } | ||
@@ -27,3 +28,15 @@ var transformed = value | ||
function shouldTraverse (value, transformedValue, currentDepth) { | ||
function isFiltered (key, value) { | ||
if (!filters) { return false } | ||
var filtered = false | ||
filters.forEach(function (filter) { | ||
if (filter.test(key, value)) { | ||
filtered = true | ||
} | ||
}) | ||
return filtered | ||
} | ||
function shouldTraverse (value, transformedValue, currentDepth, filters) { | ||
var type = Object.prototype.toString.call(value) | ||
@@ -54,5 +67,5 @@ var isarray = opts.safe && Array.isArray(value) | ||
const transformedValue = transform(coercion, key, value) | ||
const transformedValue = transform(key, value) | ||
if (shouldTraverse(value, transformedValue, currentDepth)) { | ||
if (shouldTraverse(value, transformedValue, currentDepth) && !isFiltered(key, value)) { | ||
return step(value, newKey, currentDepth + 1) | ||
@@ -59,0 +72,0 @@ } |
{ | ||
"name": "flatley", | ||
"version": "5.1.0", | ||
"version": "5.2.0", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -8,3 +8,3 @@ # flatley [![Build Status](https://secure.travis-ci.org/antony/flatley.png?branch=master)](http://travis-ci.org/antony/flatley) | ||
Based on 'flatley' by Hugh Kennedy (http://hughskennedy.com) | ||
Based on 'flat' by Hugh Kennedy (http://hughskennedy.com) | ||
@@ -191,2 +191,33 @@ ## Installation | ||
// } | ||
``` | ||
### filtering | ||
Optionally run a test/set of tests on your incoming key/value(s) and don't transform this key's children if it matches. | ||
```javascript | ||
const someObject = { | ||
prop1: 'abc', | ||
prop2: 'def' | ||
} | ||
var filters = [{ | ||
test: function (key, value) { return value.prop1 === 'abc' } | ||
}] | ||
var options = { filters: filters } | ||
flatten({ | ||
group1: { | ||
someObject: someObject | ||
} | ||
}, options) | ||
// { | ||
// 'group1.someObject': { | ||
// 'prop1': 'abc', | ||
// 'prop2': 'def' | ||
// } | ||
// } | ||
``` |
@@ -601,1 +601,75 @@ /* globals suite test */ | ||
}) | ||
suite('Filtering', function () { | ||
test('Should allow filtering of specific objects so that they are not flattened', function () { | ||
const SomeObject = function () { | ||
this.type = 'sometype' | ||
this.value = 'xxx' | ||
} | ||
var filters = [{ | ||
test: function (key, value) { return value.type === 'sometype' } | ||
}] | ||
var options = { filters: filters } | ||
var object = new SomeObject() | ||
assert.deepEqual(flatten({ | ||
group1: { | ||
object: object | ||
} | ||
}, options), { | ||
'group1.object': object | ||
}) | ||
}) | ||
test('Should allow multiple filters', function () { | ||
const SomeObject = function () { | ||
this.type = 'secondtype' | ||
this.value = 'xxx' | ||
} | ||
var filters = [ | ||
{ | ||
test: function (key, value) { return value.type === 'firsttype' } | ||
}, | ||
{ | ||
test: function (key, value) { return value.type === 'secondtype' } | ||
} | ||
] | ||
var options = { filters: filters } | ||
var object = new SomeObject() | ||
assert.deepEqual(flatten({ | ||
group1: { | ||
object: object | ||
} | ||
}, options), { | ||
'group1.object': object | ||
}) | ||
}) | ||
test('If filter tests do not pass, object is not filtered', function () { | ||
const SomeObject = function () { | ||
this.type = 'sometype' | ||
this.value = 'xxx' | ||
} | ||
var filters = [{ | ||
test: function (key, value) { return value.type === 'othertype' } | ||
}] | ||
var options = { filters: filters } | ||
var object = new SomeObject() | ||
assert.deepEqual(flatten({ | ||
group1: { | ||
object: object | ||
} | ||
}, options), { | ||
'group1.object.type': 'sometype', | ||
'group1.object.value': 'xxx' | ||
}) | ||
}) | ||
}) |
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
25471
731
221