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

crystal_constants

v1.1.0

Published

A little JavaScript / CoffeeScript utility class for frozen 'constant' objects.

Downloads

8

Readme

Crystal Constants

A little JavaScript / CoffeeScript utility class for frozen 'constant' objects. Crystal Constants works in JavaScript environments with support for Object.defineProperty and Object.defineProperties, like Node.js and modern browsers.

Installation

The most common use of Crystal is to use it as a library. In that case, just include it in your Node.js project by adding a line for Crystal in your pacakge.json. For more information about your package.json file, you should check out the npm documentation by running npm help json.

Alternatively, you can quickly install Crystal for use in a project by running

npm install crystal_constants

which will install crystal_constants in your node_modules/ folder.

Examples

// Getting Started
// ===============
var Crystal = require('crystal').Crystal
var CONST = Crystal.create({DAYS_IN_YEAR: 365})

CONST.DAYS_IN_YEAR // 365

try {
  CONST.DAYS_IN_YEAR = 364
} catch (err) {
  // Assignment to a 'constant' property throws an error.
}

// You can assign new constants:
CONST.define('MONTHS_IN_YEAR', 12)

// Your value cannot be re-assigned:
try {
  CONST.MONTHS_IN_YEAR = 11
} catch (err) {
  // Throws an error.
}

// Test if a constant is already defined:
CONST.isDefined('MONTHS_IN_YEAR') // true

// You can assign multiple constants at once:
CONST.define({
  JAN: 31,
, FEB: 28
, MAR: 31
})

// Inherit on Your Own Prototypes
// ==============================
var Crystal = require('crystal').Crystal
var util = require('util')

function Widget(spec) {
  this.define({
    WIDTH: spec.width
  , HEIGHT: spec.height
  })
}

util.inherits(Widget, Crystal)

Widget.prototype.area = function () {
  return this.WIDTH * this.HEIGHT;
}

var rectangle = new Widget({width: 50, height: 100})
rectangle.area() // 5000

try {
  rectangle.HEIGHT = 200
} catch (err) {
  // Throws
}
# Or in CoffeeScript, for extra awesome sauce:

class Widget extends Crystal
  constructor: (spec) ->
    @define({
      WIDTH: spec.width
      HEIGHT: spec.height
    })

  area: -> @WIDTH * @HEIGHT

rectangle = new Widget {width: 50, height: 100}

Why?

There isn't really any way to define constants in JavaScript like there are in other languages. However, we can achieve the same thing in JavaScript with Object.defineProperty and Object.freeze. However, these techniques have a major flaw, demonstrated with this Node.js console session:

> var x = Object.create(null)
undefined
> x
{}
> Object.defineProperty(x, 'FOO', {value: 99, enumerable: true})
{ FOO: 99 }
> x.FOO
99
> x.FOO = 100
100
> x.FOO
99
> Object.freeze(x)
{ FOO: 99 }
> x.FOO = 100
100
> x.FOO
99

Bummer. Instead of throwing an error, V8 (the JavaScript engine in Node.js) just let us assign values to x.FOO even though it had already been defined and frozen. The value of x.FOO didn't change, but V8 didn't tell us that the assignments failed. That's not very useful if you need constants and expect them to behave like constants.

Testing

To run the tests, just do

./manage test

You should see the test results output.

Copyright and License

Copyright (c) 2013 by Kris Walker [email protected] (http://www.kixx.name).

Unless otherwise indicated, all source code is licensed under the MIT license. See LICENSE for details.