Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add value coercion #61

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,27 @@ flatten({
// 'key3.a': { b: { c: 2 } }
// }
```

### coercion

Optionally run a test/set of tests on your incoming key/value(s) and transform the resulting value if it matches.

```javascript
var ObjectId = mongoose.Types.ObjectId

var coercion = [{
test: function (key, value) { return key === '_id' && ObjectId.isValid(value) }
transform: function (value) { return value.valueOf() }
}]
var options = { coercion: coercion }

flatten({
group1: {
prop1: ObjectId('aaabbbcccdddeee')
}
}, options)

// {
// 'group1.prop1': 'aaabbbcccdddeee'
// }
```
43 changes: 33 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,53 @@ function flatten (target, opts) {

var delimiter = opts.delimiter || '.'
var maxDepth = opts.maxDepth
var coercion = opts.coercion
var output = {}

function transform (coercion, key, value) {
if (!coercion) { return value }
let transformed = value

coercion.forEach(function (c) {
transformed = c.test(key, transformed) ? c.transform(transformed) : transformed
})

return transformed
}

function shouldTraverse (value, transformedValue, currentDepth) {
var type = Object.prototype.toString.call(value)
var isarray = opts.safe && Array.isArray(value)
var isbuffer = isBuffer(value)
var isobject = (
type === '[object Object]' ||
type === '[object Array]'
)

return transformedValue === value &&
!isarray &&
!isbuffer &&
isobject &&
Object.keys(value).length &&
(!opts.maxDepth || currentDepth < maxDepth)
}

function step (object, prev, currentDepth) {
currentDepth = currentDepth || 1
Object.keys(object).forEach(function (key) {
var value = object[key]
var isarray = opts.safe && Array.isArray(value)
var type = Object.prototype.toString.call(value)
var isbuffer = isBuffer(value)
var isobject = (
type === '[object Object]' ||
type === '[object Array]'
)

var newKey = prev
? prev + delimiter + key
: key

if (!isarray && !isbuffer && isobject && Object.keys(value).length &&
(!opts.maxDepth || currentDepth < maxDepth)) {
const transformedValue = transform(coercion, key, value)

if (shouldTraverse(value, transformedValue, currentDepth)) {
return step(value, newKey, currentDepth + 1)
}

output[newKey] = value
output[newKey] = transformedValue
})
}

Expand Down
Loading