qd-set

2.0.1 • Public • Published

Quick and Dirty Set

Build Status

A tiny (10 LOC) ES6 Set implementation. Think of it as a forwards-compatible _.uniq(array).

Disclaimer: Not a complete polyfill, don't use on large data.

Usage

import { Set } from 'qd-set/esm/index';
 
const s = new Set([1, 2, 2, 3])
s.size       // => 3
s.add(3)     // => Set([1, 2, 3])
s.add(4)     // => Set([1, 2, 3, 4])
s.has(4)     // => true
s.delete(3)  // => 3
s.forEach(x => console.log(x))
// 1
// 2
// 4
s.clear()    
s.size       // => 0
s.has(1)     // => false

Need keys, values, entries?

import { Set } from 'qd-set/esm/with-iter';
 
const s = new Set([1, 2, 2, 3])
const it = s.values();
it.next().value  // => 1
it.next().value  // => 2
it.next().value  // => 3
it.next().done   // => true

Source

export function Set(a = []) {
  a = a.filter((x, i) => i === a.indexOf(x));
  a.size = a.length;
  a.has = x => a.indexOf(x) > -1;
  a.add = x => { if (!a.has(x)) { a.size++; a.push(x); } return a; };
  a.delete = x => { let t; if (= a.has(x)) { a.size--; a.splice(a.indexOf(x), 1) } return t; };
  a.clear = () => { while (a.pop()) {} a.size = 0; };
  return a;
}

Readme

Keywords

Package Sidebar

Install

npm i qd-set

Weekly Downloads

159

Version

2.0.1

License

MIT

Unpacked Size

8.91 kB

Total Files

9

Last publish

Collaborators

  • qwtel