Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tomeraberbach/svkc
0️⃣ JavaScript's keyed collections (Map & Set) with SameValue semantics!
https://github.com/tomeraberbach/svkc
es6-map es6-set map samevalue samevaluezero set
Last synced: about 2 months ago
JSON representation
0️⃣ JavaScript's keyed collections (Map & Set) with SameValue semantics!
- Host: GitHub
- URL: https://github.com/tomeraberbach/svkc
- Owner: TomerAberbach
- License: apache-2.0
- Created: 2021-07-22T04:09:55.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-06-23T02:56:40.000Z (6 months ago)
- Last Synced: 2024-10-12T01:23:30.022Z (2 months ago)
- Topics: es6-map, es6-set, map, samevalue, samevaluezero, set
- Language: TypeScript
- Homepage: https://npm.im/svkc
- Size: 242 KB
- Stars: 6
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- Contributing: contributing.md
- License: license
Awesome Lists containing this project
README
svkc## 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
```sh
$ 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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness#same-value-zero_equality)
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](https://www.johndcook.com/blog/2010/06/15/why-computers-have-signed-zero)
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`!
```js
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))
//=> zeroconsole.log(sameValueMap.get(-0))
//=> negative zeroconsole.log([...sameValueMap])
//=> [ [ 1, 'one' ], [ 0, 'zero' ], [ -0, 'negative zero' ], [ -1, 'negative one' ] ]sameValueMap.delete(0)
console.log(sameValueMap.has(0))
//=> falseconsole.log(sameValueMap.has(-0))
//=> trueconst sameValueSet = new SameValueSet()
sameValueSet.add(1)
sameValueSet.add(0)
sameValueSet.add(-0)
sameValueSet.add(-1)console.log(sameValueSet.has(0))
//=> trueconsole.log(sameValueSet.has(-0))
//=> trueconsole.log([...sameValueSet])
//=> [ 1, 0, -0, -1 ]sameValueSet.delete(0)
console.log(sameValueSet.has(0))
//=> falseconsole.log(sameValueSet.has(-0))
//=> true
```## Contributing
Stars are always welcome!
For bugs and feature requests,
[please create an issue](https://github.com/TomerAberbach/svkc/issues/new).For pull requests, please read the
[contributing guidelines](https://github.com/TomerAberbach/svkc/blob/main/contributing.md).## License
[Apache License 2.0](https://github.com/TomerAberbach/svkc/blob/main/license)
This is not an official Google product.