Skip to content

Commit

Permalink
feat: add META symbol to exports (#84)
Browse files Browse the repository at this point in the history
Two things to note:

- The key is the string `'META'`
- The value is `Symbol('proc-log.meta')`

So it could be used like the following. All of the following is up to
producers/consumers to implement with shared conventions. All `proc-log`
is doing to allowing them to share a unique `Symbol`.

```js
const { output, META } = require('../')

// An example of how consumers would see if a META object
// was the last argument from an event
const getMeta = (args) => {
  let meta = {}
  const last = args.at(-1)
  if (last && typeof last === 'object' && Object.hasOwn(last, META)) {
    meta = args.pop()
  }
  return [meta, ...args]
}

process.on('output', (level, ...rawArgs) => {
  const [{ force = false }, ...args] = getMeta(rawArgs)
  console.log(level, { force, args })
})

// in this implementation the value does not matter, just the key
output.standard('arg1', 'arg2', { [META]: null, force: true })
output.standard('arg1', 'arg2')
output.standard('arg1', null)
output.standard(null)
output.standard()

// Will log the following:
// standard { force: true, args: [ 'arg1', 'arg2' ] }
// standard { force: false, args: [ 'arg1', 'arg2' ] }
// standard { force: false, args: [ 'arg1', null ] }
// standard { force: false, args: [ null ] }
// standard { force: false, args: [] }
```

Closes: #81
  • Loading branch information
lukekarrys committed Apr 16, 2024
1 parent 3dbd032 commit 66b7da7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const META = Symbol('proc-log.meta')
module.exports = {
META: META,
output: {
LEVELS: [
'standard',
Expand Down
10 changes: 9 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@ const t = require('tap')
const procLog = require('../')

// This makes sure we are testing all known exported methods.
t.plan(4)
t.plan(5)

for (const method in procLog) {
if (method === 'META') {
t.test(method, t => {
t.type(procLog[method], 'symbol')
t.end()
})
continue
}

t.test(method, t => {
const log = procLog[method]
const { LEVELS, KEYS } = log
Expand Down
42 changes: 42 additions & 0 deletions test/meta.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const t = require('tap')
const { META, output } = require('../')

// just to show how this would be implemented in consumers
const getMeta = (...args) => {
let meta = {}
const lastArg = args[args.length - 1]
if (
lastArg &&
typeof lastArg === 'object' &&
Object.prototype.hasOwnProperty.call(lastArg, META)
) {
meta = args.pop()
}
return [meta, ...args]
}

// an example of wrapping a handler
const wrapMeta = (handler) => (level, ...args) => handler(level, ...getMeta(...args))

t.test('meta', t => {
process.once('output', (level, ...rawArgs) => {
const [meta, ...args] = getMeta(...rawArgs)
t.equal(level, 'standard')
t.ok(meta.force)
t.strictSame(args, ['arg1', 'arg2'])
t.end()
})

output.standard('arg1', 'arg2', { [META]: 0, force: true })
})

t.test('wrap handler', t => {
process.once('output', wrapMeta((level, meta, ...args) => {
t.equal(level, 'standard')
t.ok(meta.force)
t.strictSame(args, ['arg1', { META: true }])
t.end()
}))

output.standard('arg1', { META: true }, { [META]: null, force: true })
})

0 comments on commit 66b7da7

Please sign in to comment.