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

composite-symbol

v1.0.2

Published

Map a series of values to a unique JavaScript Symbol

Downloads

50

Readme

composite-symbol logo, showing the Unicode character "Composition Symbol": the overlapping outlines of a square and a circle

Composite Symbol

Get a unique JavaScript Symbol from a series of values, inspired by Bradley Farias' Richer Keys proposal.

JavaScript Style Guide Travis AppVeyor npm

The Problem

Since ES2015, we can use arbitrary objects as dictionay keys by using Maps.

However, sometimes we need to map data to not just one object but to multiple values, for example to attach data to relationships. JavaScript unfortunately has no built-in way for this.

A Solution

This package provides a getCompositeSymbol function which takes an arbitrary (non-zero) number of values and returns a unique Symbol. Whenever the same series of values is passed to getCompositeSymbol, the same Symbol is going to be returned.

These unique Symbols allow us to construct things around them, e.g. we could use them as keys in dictionaries.

Example

We can use this feature to attach metadata to a relationship between two objects, without having to tack it onto the participating objects themselves:

const getCompositeSymbol = require('composite-symbol')

// Imaginary models
const Person = require('person')
const { Prius, Ferrari } = require('cars')

// We want to store information about how people use their cars
const carRelationships = {}

// Say hi to John!
const john = new Person()

// John owns an old Toyota Prius
john.car = new Prius()

// John needs his car to drive to work everyday
carRelationships[getCompositeSymbol(john, 'car')] = 'drives to work daily'

// How about John buying a new car? He really earned it.
john.car = new Ferrari()

// But John still needs to drive to work everyday
carRelationships[getCompositeSymbol(john, 'car')] // === "drives to work daily"

Installation

Install it from npm:

npm install --save composite-symbol

Gotchas

  • Order matters! getCompositeSymbol(1, 2, 3) will not return the same symbol as getCompositeSymbol(3, 2, 1).
  • Symbols are not unique across realms. That means, getCompositeSymbol(1, 2, 3) in browser window A will not return the same symbol as getCompositeSymbol(1, 2, 3) in browser window B.
  • Symbols, as well as Maps (which this package uses in the background) are ES2015 features. This is not a problem in Node.js anymore, but if you want to use this package in the browser, be aware that it will not work in Internet Explorer.