save-our-sanity

1.0.1 • Public • Published

Save Our Sanity

Quickly wrap any object in a logging object proxy that will log all access and manipulation of the proxied target.

Useful for instance to temporarily verify that a 3rd-party library really reads the expected keys from a passed opts hash. Or if a method you expect to be invoked really was called with expected arguments and what the produced return value was.

Pretty much a general purpose sanity checker, a companion tool for test driven development.

It currently supports logging for the following operations get, set, invoke and in. But feel free to implement additional logging hooks from the Reflect API if required.

Please avoid using this module in production, even though the performance impact should be minimal it will eventually flood your console with mostly useless information, unless ofcourse.. you're in a situation where you start questioning your own sanity.

Installation

Installation shouldn't be needed, because "SOS" should never be used in production. to install the module temporary while hacking on some random project:

npm i --no-save save-our-sanity

To install as a devel-dependency use the regular proceedure:

npm i --saveDev save-our-sanity
# or 
yarn add -D save-our-sanity

Usage

const SOS = require('save-our-sanity')

const mObj = {
  a: 3,
  f (x) {
    return x + 5
  }
}

const proxy = SOS(mObj) // target can be anything

// All access is transparently proxied to original object
// but is also logged at the same time
const resA = proxy.a // => 3
const resB = proxy.f(2) // => 7
const resC = proxy.anUndefinedKey // => undefined
proxy.x = resA + resB // => 10

console.log('Results:', resA, resB, resC)

Running the above snippet produces the following output:

$ node example.js
  SOS get a 3 +0ms
  SOS get f f (x) {
    return x + 5
  } +1ms
  SOS invoke f [ 2 ] 7 +1ms
  SOS get anUndefinedKey undefined +1ms
  SOS set x 10 +0ms
Results: 3 7 undefined

API

SOS(target, tag = 'SOS', opts = {})

Creates a new proxy with specified target, an optional log-tag and an optional opts object

tag - string, defaults to 'SOS'

opts.logger - function that will be invoked instead of the default debug logger

License

GNU GPLv3

Readme

Keywords

none

Package Sidebar

Install

npm i save-our-sanity

Weekly Downloads

1

Version

1.0.1

License

GPL-3.0-or-later

Unpacked Size

43.5 kB

Total Files

7

Last publish

Collaborators

  • telamon