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

props-model

v1.0.1

Published

A model for properties including change events and derived properties

Downloads

7

Readme

props-model

A javascript package providing a model for properties, including change events and derived properties.

JavaScript Style Guide

npm Stable Build node NpmLicense

npm bundle size (minified) Libraries.io for GitHub npm GitHub issues GitHub pull requests GitHub last commit

Overview

This package provides the PropertyModel class which can serve as the M in an MVC pattern, or more generally to track and manage a set of named properties. In addition to get and modify access for the properties, the model also provides synchronous change event firing and listener creation for the managed properties. It also allows you to define derived properties whose values are automatically calculated anytime a property it depends on changes.

Install

With npm:

npm install --save props-model

Demo Usage

import PropsModel from 'props-model' // or `const { PropsModel } = require('props-model')`
import EventEmitter from 'events'

class MutableRectangle {
  constructor (initialLength, initialWidth) {
    // Define the properties of this object.
    const propModel = new PropsModel(new EventEmitter())
      // These are "primary" properties, they are not calculated from other properties.
      .defineProp('length', initialLength, isValidDimension)
      .defineProp('width', initialWidth, isValidDimension)
      // These are "derived" properties, they are automatically updated when any of
      // the properties they depend on change.
      .defineDerivedProp('area', ['length', 'width'], (length, width) => length * width)
      .defineDerivedProp('perimeter', ['length', 'width'], (length, width) => (2 * length) + (2 * width))
      .defineDerivedProp('aspectRatio', ['length', 'width'], (length, width) => length / width)

    // We can use the propModel as an implementation detail, and expose classical getters and setters
    // for our props. This method on the propModel does that for us.
    propModel.installAccessors(this, {
      // getter and setters for our primary properties.
      length: 'readwrite',
      width: 'readwrite',
      // It's not usually a good idea to allow derived properties to be set directly,
      // it breaks coherency. Only getters will be provided for these properties.
      area: 'readonly',
      perimeter: 'readonly',
      aspectRatio: 'readonly'
    })

    // Our propModel also provides a convenient JSON representation of our properties,
    // which we will adopt as our own.
    this.toJSON = () => propModel.toJSON()

    // Note that we don't need to keep propModel around as an instance property, it's
    // attached to the accessors' closures as needed; it's _generally_ good practice to
    // set up all uses of the propModel in the constructor, and *not* use it directly
    // after that.
  }
}

// Optional property value validators can be provided for primary properties.
function isValidDimension (dim) {
  if (typeof dim !== 'number') {
    throw new Error('Invalid dimension, must be a number')
  }
  if (dim < 0) {
    throw new Error('Invalid dimension, must be non-negative')
  }
}

// Let's exercise our new class a bit:
function main () {
  const rect = new MutableRectangle(10, 20)

  // JSON.stringify uses the toJSON() method attached ot the object.
  console.log(JSON.stringify(rect)) // {"length":10,"width":20,"area":200,"perimeter":60,"aspectRatio":0.5}

  // When we set a property...
  rect.setLength(15)

  // ...that property is updated ...
  console.log(rect.getLength()) // 15

  // ... and so are derived properties ...
  console.log(rect.getArea()) // 300
  console.log(JSON.stringify(rect)) // {"length":15,"width":20,"area":300,"perimeter":70,"aspectRatio":0.75}
}

main()