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

static-props

v1.1.2

Published

defines static object attributes using Object.defineProperties

Downloads

530

Readme

static-props

defines static object attributes using Object.defineProperties

Installation | Usage | Annotated source | License

NPM version Build Status No deps JavaScript Style Guide KLP

Installation

With npm do

npm install static-props --save

Usage

Let's create a classic Point2d class and add constant attributes.

'use strict'

const staticProps = require('static-props')
// Or use ES6: import staticProps from 'static-props'

class Point2d {
  constructor (x, y, label) {
    // Suppose you have few static attributes in your class.
    const color = 'red'

    // Create a getter.
    const norm = () => x * x + y * y

    // Add constant attributes quickly.
    staticProps(this)({label, color, norm})

    // Add enumerable attributes.
    const enumerable = true
    staticProps(this)({x, y}, enumerable)
  }
}

// Add a static class attribute.
staticProps(Point2d)({ dim: 2 })

const norm = (x, y) => x * x + y * y
// A particular case are static methods, since they are functions
// they must be wrapped otherwise are considered as getters.
staticProps(Point2d)({ norm: () => norm })

After instantiating the class, we can check that its props cannot be changed.

const p = new Point2d(1, 2)

// Trying to modify a static prop will throw, as expected.
p.label = 'B'
// TypeError: Cannot assign to read only property 'label' of #<Point2d>

Props label and color are values, while norm is a getter.

console.log(p.label) // 'A'
console.log(p.color) // 'red'
console.log(p.norm) // 5 = 1 * 1 + 2 * 2

Attributes x, y were configured to be enumerable.

console.log(p) // Point2d { x: 1, y: 2 }

You can access static class attributes and methods.

console.log(Point2d.dim) // 2
console.log(Point2d.norm(1, 2)) // 5

Annotated source

API is staticProps(obj)(props[, enumerable]).

Add every prop to obj as not writable nor configurable, i.e. static. If prop is a function use it as a getter, otherwise use it as a value. Finally, apply the Object.defineProperties function.

/**
 * @param {Object} obj
 * @returns {Function}
 */
function staticProps (obj) {
  /**
   * @param {Object} props
   * @param {Boolean} [enumerable]
   */
  return function (props, enumerable) {
    var staticProps = {}

    for (var propName in props) {
      var staticProp = {
        configurable: false,
        enumerable: enumerable
      }

      var prop = props[propName]

      if (typeof prop === 'function') {
        staticProp.get = prop
      } else {
        staticProp.value = prop

        staticProp.writable = false
      }

      staticProps[propName] = staticProp
    }

    Object.defineProperties(obj, staticProps)
  }
}

Export function, supporting both CommonJS and ES6.

module.exports = exports.default = staticProps

License

MIT