Coder Social home page Coder Social logo

tomeraberbach / svkc Goto Github PK

View Code? Open in Web Editor NEW
6.0 3.0 0.0 248 KB

0️⃣ JavaScript's keyed collections (Map & Set) with SameValue semantics!

Home Page: https://npm.im/svkc

License: Apache License 2.0

JavaScript 12.31% TypeScript 87.69%
samevalue samevaluezero set map es6-map es6-set

svkc's Introduction

svkc

JavaScript's keyed collections (Map & Set) with SameValue semantics!

Features

  • Familiar: SameValueMap and SameValueSet extend JavaScript's built-in Map and Set
  • Compliant: maintains all the invariants of Map and Set including method return values and even iteration order!
  • Tiny: less than 350 bytes minzipped!

Install

$ npm i svkc

Huh?

A key in a Map or a value in a Set can only occur once. But how is the key or value's uniqueness determined? JavaScript's Map and Set use the sameValueZero algorithm when checking if two keys or values are equal. The algorithm considers +0 and -0 to be equal, but they are actually two different values due to how IEEE floating point numbers work. This means that both new Map([[0, 'zero'], [-0, 'negative zero']]).size and new Set([0, -0]).size return 1 rather than 2.

This package provides SameValueMap and SameValueSet that behave identically to Map and Set except they consider +0 and -0 to be different values.

Usage

Just use like a normal Map or Set!

import { SameValueMap, SameValueSet } from 'svkc'

const sameValueMap = new SameValueMap()
sameValueMap.set(1, `one`)
sameValueMap.set(0, `zero`)
sameValueMap.set(-0, `negative zero`)
sameValueMap.set(-1, `negative one`)

console.log(sameValueMap.get(0))
//=> zero

console.log(sameValueMap.get(-0))
//=> negative zero

console.log([...sameValueMap])
//=> [ [ 1, 'one' ], [ 0, 'zero' ], [ -0, 'negative zero' ], [ -1, 'negative one' ] ]

sameValueMap.delete(0)

console.log(sameValueMap.has(0))
//=> false

console.log(sameValueMap.has(-0))
//=> true

const sameValueSet = new SameValueSet()
sameValueSet.add(1)
sameValueSet.add(0)
sameValueSet.add(-0)
sameValueSet.add(-1)

console.log(sameValueSet.has(0))
//=> true

console.log(sameValueSet.has(-0))
//=> true

console.log([...sameValueSet])
//=> [ 1, 0, -0, -1 ]

sameValueSet.delete(0)

console.log(sameValueSet.has(0))
//=> false

console.log(sameValueSet.has(-0))
//=> true

Contributing

Stars are always welcome!

For bugs and feature requests, please create an issue.

For pull requests, please read the contributing guidelines.

License

Apache License 2.0

This is not an official Google product.

svkc's People

Contributors

tomeraberbach avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

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.