Skip to content

Commit

Permalink
utils: allows pickProperties on nested object
Browse files Browse the repository at this point in the history
With this we can use 'company.id' to pick a nested property
  • Loading branch information
vitorabner committed Apr 28, 2017
1 parent e30e1ac commit a5d02b5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "escriba",
"version": "1.0.0",
"version": "1.1.0",
"description": "Logging with steroids",
"main": "src/index.js",
"scripts": {
Expand Down
17 changes: 13 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,19 @@ const stringify = msg => {
}

const pickProperties = (message, propsToLog) => {
const isNull = prop => R.isNil(message[prop])
const pick = prop => R.pick([prop], message)
const propsToLogMap = R.map(R.ifElse(isNull, R.always({}), pick))
return R.pipe(propsToLogMap, R.mergeAll)(propsToLog)
const dotSplit = R.split('.')
const isNull = prop => R.isNil(R.path(R.split('.', prop), message))

const pickProp = prop => ({ path: dotSplit(prop), value: R.path(dotSplit(prop), message) })
const buildProps = R.map(R.ifElse(isNull, R.always({}), pickProp))
const mergePickedProps = pickedProps => R.reduce(addPickedProp, {}, pickedProps)

const addPickedProp = (merged, { path, value }) => {
if (!path) return merged
return R.set(R.lensPath(path), value, merged)
}

return R.pipe(buildProps, mergePickedProps)(propsToLog)
}

const generateLogLevel = statusCode => {
Expand Down

0 comments on commit a5d02b5

Please sign in to comment.