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

fancy-proxy

v0.1.1

Published

Interface Around a Proxied Object.

Downloads

5

Readme

fancy-proxy

Interface Around a Proxied Object: npm i fancy-proxy.

  • Access to Nested Values
  • Middleware with Stringified Path
  • Handles (Custom Methods Before Accessing Values)
  • Deep Merge of Target Objects including Arrays
  • ES5 Object Fallback for IE11 Support
    • Except Handles & Middleware for read access

Usage

fancy(target, options): proxy

import fancy from 'fancy-proxy'

const target = {
  products: [
    'apple',
    'banana',
    'citron'
  ]
}
const options = {
  middleware: path => console.log(path.toString())
}
const proxy = fancy(target, options)

proxy.products.length === 3     // logs: products[length]
proxy.products[1] === 'banana'  // logs: products[1]

Features

Middleware

Middleware is a function that will receive the following arguments:

middleware: (path, value, type) => console.log(\${path}: ${value}`)`

path: A stringified representation of the path being accessed. The call shop.nested[0].count returns nested[0].count.

value: The value being accessed, or the value returned by a method. For read access the middleware will be called before the value is returned, while for call the middleware can only be called once the return value is available.

type: Either read or call for functions.

Handles

A handle consists of a target object and a handle function that will be wrapped around every call to a function found on the target. The handle receives the following arguments:

handle(inputs, target, value, path, type) => result

inputs: []: Array of arguments received when the function on the target is called.

target: {}: The whole proxy target (target + all handle targets).

value: func/any: The function or the value (if proxy available) being accessed.

path: string: The current path being accessed.

type: string: Type of the call, either 'call' or 'read' (only when proxies available).

Handles should call the value for calls or return the value for get access.

const proxy = fancy({
    count: 5
  }, {
  handles: [
    {
      target: {
        add: (inputs, target) => {
          // Add the sum of inputs to the count.
          const add = (a, b) => (a + b)
          const total = inputs.reduce(add, 0)
          target.count += total
          return total
        }
      },
      handler: (inputs, target, value, path, type) => {
        if (type === 'read') {
          return value
        }

        return value.apply(null, [inputs, target])
      }
    }
  ]
})

proxy.count     → 5
proxy.add()     → 0
proxy.count     → 5
proxy.add(4)    → 4
proxy.count     → 9
proxy.add(5, 6) → 11
proxy.count     → 20

Options

fancy(target, options): proxy

The second object can be used to further configure the proxy, with the following options:

fallback: boolean If true the object fallback will be used even if Proxy is available.

immutable: {} An optional object that will be exported by the proxy, but doesn't affect the target supplied to the handles.

Considerations

Merging Targets

When a path is accessed the target will be searched for a match first, if the path cannot be found there, then the handles are checked, first to last.

Access Order: target → first handle → ... → last handle

Upcoming Features

  • Types (TypeScript)
  • Setters when Proxy support assumed