npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

spy-property-reads

v1.0.3

Published

JavaScript offers various ways for an object to store another object. This library give a uniform way to trap the various ways in which properties are extracted from an object.

Downloads

8

Readme

Spy-property-reads

JavaScript offers various ways for an object to store another object. This library give a uniform way to trap the various ways in which properties are extracted from an object.

Usage

Spy property getters


const { spyPropertyReads } = require('spy-property-reads')

const o = { a: 42, b: 42 }

const spyCallback = function(source, query, getResult) {
  const result = getResult()
  console.log({ query, result })
  return result
}

const handler = spyPropertyReads(spyCallback)
const spy = new Proxy(o, handler)

// accessing 42 using the property 'a'
spy.a
// => 42
//  console.log() => { query: 'get("a")', result: 42 } 

spy.b
// => 42
//  console.log() => { query: 'get("b")', result: 42 } 

Spy functions return values


const { spyPropertyReads } = require('spy-property-reads')

const o = function() { return 42 }

const spyCallback = function(source, query,  getResult) {
  const result = getResult()
  console.log({ query, result })
  return result
}

const handler = spyPropertyReads(spyCallback)
const spy = new Proxy(o, handler)

// accessing 42 by calling the function
spy()
// => 42
//  console.log() => { query: 'apply()', result: 42 } 

Spy property descriptors


const { spyPropertyReads } = require('spy-property-reads')

const o = {
  a: 42,
  get b() { return 10 }
}

const spyCallback = function(source, query,  getResult) {
  const result = getResult()
  console.log({ query, result })
  return result
}

const handler = spyPropertyReads(spyCallback)
const spy = new Proxy(o, handler)

Object.getOwnPropertyDescriptor(spy, 'a').value
// => 42
//  console.log() => { query: 'getOwnPropertyDescriptor("a").value', result: 42 } 

Object.getOwnPropertyDescriptor(spy, 'b').get()
// => 10
//  console.log() => { query: 'getOwnPropertyDescriptor("b").get()', result: 10 } 

Spy access to the prototype


const { spyPropertyReads } = require('spy-property-reads')

const o = Object.create({ foo: 'bar' })

const spyCallback = function(source, query,  getResult) {
  const result = getResult()
  console.log({ query, result })
  return result
}

const handler = spyPropertyReads(spyCallback)
const spy = new Proxy(o, handler)

Object.getPrototypeOf(spy)
// => { foo: 'bar' }
//  console.log() => { query: 'getPrototypeOf()', result: { foo: 'bar' } } 

Composing proxy handlers

The proxy handlers created by 'spy-property-reads' can wrap other proxy handlers. This allows composition of 'proxy enhancers' to create sophisticated proxy behavior.


const { spyPropertyReads } = require('spy-property-reads')

const o = {}
// 1 - every property return 42
const handler1 = { get: () => 42 }

const spyCallback = function(source, query, getResult) {
  const result = getResult()
  console.log({ query, result })
  return result
}
// 2 - spy on property reads
const handler2 = spyPropertyReads(spyCallback, handler1)

// 3 - add un-spyable property
const handler3 = {
  ...handler2,
  get: function(target, prop) {
    if (prop === 'secret') { return 'foo' }
    else { return handler2.get(...arguments) }
  }
}

const spy = new Proxy(o, handler3)

spy.a
// => 42
//  console.log() => { query: 'get("a")', result: 42 } 
spy.b
// => 42
//  console.log() => { query: 'get("b")', result: 42 } 
spy.c
// => 42
//  console.log() => { query: 'get("c")', result: 42 } 
spy.secret
// => foo
// No call to "console.log"

API

spyPropertyReads(callback, handler)

callback

A function which takes 3 arguments and returns the property querried.

Arguments:

1 - source: the object being spied 2 - query: a string representation of the function which triggered the callback 3 - getResult: a function which returns the property querried.

The reading operation on the spied object will return the value returned by the callback. To keep the return value unchanged, return the value returned by getResult(). Aternatively, we can modify the value, substitute it, or not call getResult at all and return something totally different.

handler (optional)

A proxy handler which will be wrapped. This permits to create sophisticated proxy behaviors by composing simple handlers together.

Return value

This function returns a proxy handler. A proxy can be instanciated with this handler or with another handler which wraps it to adds more features.