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

identity-proxy

v0.0.1

Published

Proxy that is kind of an identity element

Downloads

4

Readme

Identity Proxy

Proxy that is kind of an identity element.

Installation

npm install --save identity-proxy

Motivation

In a React project, I want to make a higher order component to accomplish following task:

import React from "react";
import { connect } from "react-redux";

function withSubscription(
  WrappedComponent,
  mapStateToProps,
  mapDispatchToProps
) {
  class WithSubscription extends React.Component {
    componentDidMount() {
      // for each prop injected by connect from mapDispatchToProps (someAction),
      // someAction();
    }

    componentDidUpdate(prevProps) {
      // for each prop injected by connect from mapStateToProps (someState),
      // if (prevProps.someState != this.props.someState) {
      //   someAction(someState);
      // }
    }

    render() {
      return <WrappedComponent {...this.props} />;
    }
  }

  return connect(
    mapStateToProps,
    mapDispatchToProps
  )(WithSubscription);
}

I wonder how to get the name of someAction and someState without explicitly provided an array of name strings. mapDispatchToProps is usually an object, so someAction can be easily retrieved by using Object.keys(mapDispatchToProps). But mapStateToProps is a function. How can I get the keys of the return object? My answer is this package.

Usage

Any action acting on the proxy will return the proxy itself.

import createIdentityProxy from "identity-proxy";

const identityProxy = createIdentityProxy();

const foo = identityProxy.foo;
const bar = identityProxy();

console.log(foo === identityProxy); // true
console.log(bar === identityProxy); // true
console.log(foo === bar); // true

With the above behaviour, my original question can be answered:

const mapStateToProps = state => ({
  bar: state.foo.bar,
  baz: state.foo.baz
});

const identityProxy = createIdentityProxy();

console.log(Object.keys(mapStateToProps(identityProxy))); // ['bar', 'baz']

The implementation of the function mapStateToProps does not matter. What's more, type can be perserved upon operation on a primitive type:

const identityProxy = createIdentityProxy();
console.log(typeof (identityProxy * 1 - 1 + 1)); // 'number'
console.log(typeof `${identityProxy}`); // 'string'

The goal of all the above behaviours is to not break any function executed with the identityProxy arguments.

Remark

This package relies on Proxy and Symbol.toPrimitive. Need to use suitable polyfills to target old browsers.