Skip to content
This repository has been archived by the owner on Dec 8, 2020. It is now read-only.

Pluck util #35

Open
leebenson opened this issue Jan 4, 2019 · 1 comment
Open

Pluck util #35

leebenson opened this issue Jan 4, 2019 · 1 comment
Assignees
Labels
feature request A request for a new feature in the library @timberio/tools Helper utils

Comments

@leebenson
Copy link
Member

leebenson commented Jan 4, 2019

A common requirement for building logging plugins is pulling context from the environment, for attaching along with the log.

An example is a Koa request, a sample of which could look like this...

ctx = { request:
   { secure: true,
     headers: { 'content-type': 'text/plain' },
     href: 'http://google.com/?q=whatever' },
  response: { status: 200 },
  length: 40,
  message: null
}

We could do with a generic pluck util in @timberio/tools which lets us request a 'path' of deeply nested object keys, and returns just those keys in a new object - which we can then attach along with the request.

For example, given the `ctx above...

keys = ["length", "request.headers", "response.status"]
pluck(keys, ctx)

... we'd get back this:

// `request.href` and `message` are omitted
{ request: { secure: true, headers: { 'content-type': 'text/plain' } },
  length: 40,
  response: { status: 200 } }

There are utils that already get close to this - object-path, Rambd's path, etc - but all the tools I've seen return only single values.

Instead, we want the full, original object path, returned as a new object.

It'd also be nice to avoid using a large library like lodash / Ram(b)da, and have our own minimal function.

@aks- -- this is a good one for you.

@leebenson leebenson added @timberio/tools Helper utils feature request A request for a new feature in the library labels Jan 4, 2019
@aks-
Copy link
Contributor

aks- commented Jan 7, 2019

@leebenson thanks, i will take start working on it today.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature request A request for a new feature in the library @timberio/tools Helper utils
Projects
None yet
Development

No branches or pull requests

2 participants