GithubHelp home page GithubHelp logo

azu / composite-symbol Goto Github PK

View Code? Open in Web Editor NEW

This project forked from loilo/composite-symbol

0.0 2.0 0.0 101 KB

✳️ Map a series of values to a unique JavaScript Symbol

License: MIT License

JavaScript 100.00%

composite-symbol's Introduction



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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.