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

computed-props

v1.0.10

Published

Lightweight library to allow for use of computed properties

Downloads

16

Readme

npm version npm downloads

computed-props

npm i computed-props

const compute = require('computed-props');

A library for using traditional computed properties in JS

The computed property updates on lookup, which means that we A) retain normal object shape B) don't have to invoke anything and C) can use the computed property in expected ways.

- Issues
- Functionality
- How do I use this?

ISSUES

Known issues

Does not yet support use of the spread operator when extending the computed object

Please submit issues as you discover them

FUNCTIONALITY

I would be delighted to know how you use the computed properties and the cool things you create! Feel free to submit your examples to the wiki or email them to me at [email protected]

THE COMPUTE FUNCTION


Use on plain objects

  1. Create the object with all standard (non-computed) properties
    let obj = {           BE SURE TO USE THE LET OR VAR KEYWORD HERE, YOU WILL REASSIGN LATER
      name: 'Jon',
      age: '26',
      hobby: 'trying to break JS'
    }
  2. Construct the compute argument
  • the compute function takes an object argument, and all properties of the object should have a function value
  • the function values should make use of computing variables!
  • you can either pass an object literal or object reference
    const computedObj = {
      ageLastYear: function() { return this.age - 1 },
      nameBackwards: function() {
        const reversed = this.name.toLowerCase().split('').reverse();
        reversed[0] = reversed[0].toUpperCase();
        return reversed.join('');
      }
    }
  • compute returns a function that, when called on the static object, recursively generates proxies to handle computed property lookups
  1. Final call
   obj = compute(computedObj)(obj)
    //try changing any of the static properties then checking their related computed properties!
    
   console.log(obj)
       /*
       -- LOGS --
        {
          age: 26,
          ageLastYear: 25,
          hobby: "trying to break JS",
          name: "Jon",
          nameBackwards: "Noj"
        } 
        */
        
   obj.name = 'Alexia'
   console.log(obj)
        /*
        -- LOGS --
        {
          age: 26,
          ageLastYear: 25,
          hobby: "trying to break JS",
          name: "Alexia",
          nameBackwards: "Aixela" // notice how this is computed based on the newly updated name property?
        }
        */

For use in classes

In its current state, you can freely use it on functional, functional-shared, and prototypal classes. For use with ES6 or pseudoclassical styles, the most trustworthy way is to wrap the desired computed property and associated properties in a holder object. Then, call the compute with the holder object as the re-assigned object:

let Person = function(age) {
  this.ages = {
    age
  };
  this.ages = compute({
    agenextyear: function() {
      return this.age + 1
    }
  })(this.ages);
  
  // alternatively:
  /*
  this.ages = compute({
    agenextyear: function() {
      return this.age + 1;
    }
  })({age})
  */
}