Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Set() as it should be.

License

NotificationsYou must be signed in to change notification settings

terkelg/zet

Repository files navigation

zet

versionbuild statuscodecovinstall size

JavaScript Set() as it should be.

ECMAScript 6 sets have no methods for computing the union (∪), intersection (∩) or difference (⊖).Zet is an extension of ES6Set and comes with all its functionality included with extra set logic.The API is similar to how sets work inPython.

Features

Additions to the default ECMAScript 6 set

  • Union (∪)
  • Intersection (∩)
  • Difference/subtract (-)
  • Symmetric difference (⊖)
  • Subset (⊆)
  • Superset (⊇)
  • Map
  • Filter
  • Reduce

Additionally, this module is delivered as:

  • ES Module:dist/zet.mjs
  • CommonJS:dist/zet.js
  • UMD:dist/zet.umd.js

Install

$ npm install --save zet

Usage

importZetfrom'zet';leta=newZet([1,2,3]);letb=newZet([3,4,5]);letc=newZet([2,3,4]);Zet.union(a,b);//=> [Zet] {1, 2, 3, 4, 5}a.union(b,c);//=> [Zet] {1, 2, 3, 4, 5}a.intersection(b);//=> [Zet] {3}a.symmetricDifference(c);//=> [Zet] {1, 4}a.subset(b);//=> falsea.filter(i=>i%2);//=> [Zet] {1, 3}

API

Zet([iterable])

Returns:Zet

Returns the Zet instance.

Zet extendsSet() and inherit all its functionality, likehas(),size() etc.

Zet.union(...sets) ∪

Returns:zet

Static variadic function that return a new set with elements from all othersets.

sets

Type:Zet|Set

Two or more sets of typeZet orSet.

Zet.intersection(...sets) ∩

Returns:zet

Static variadic function that return a new set with elements common tothis and all othersets.

sets

Type:Zet|Set

Two or more sets of typeZet orSet.

Zet.difference(...sets) - or \

Returns:zet

Returns the difference between two or more sets. The order of the sets matters. Sets are differentiated against the first argument/set.

sets

Type:Zet|Set

Two or more sets of typeZet orSet.

Zet.symmetricDifference(setA, setB) ⊖ or ∆

Returns:zet

Static function that return a new set with elements in either setA or setB but not both.

setA

Type:Zet|Set

Set A of typeZet orSet.

setB

Type:Zet|Set

Set B of typeZet orSet.

Zet.subset(setA, setB)

Returns:Boolean

Test whether every element insetB is insetA.

setA

Type:Zet|Set

Set of typeZet orSet.

setB

Type:Zet|Set

Set of typeZet orSet.

Zet.superset(setA, setB)

Returns:Boolean

Test whether every element insetA is insetB.

setA

Type:Zet|Set

Set of typeZet orSet.

setB

Type:Zet|Set

Set of typeZet orSet.

map(set, func)

Returns:Zet|Set

Creates a set with the results of calling the provided function on every element.

set

Type:Zet|Set

Set of typeZet orSet.

func

Type:Function

Function that produces an element of the new set.

filter(set, func)

Returns:Zet|Set

Creates a set with all elements that pass the test implemented by the provided function.

set

Type:Zet|SetIt is the set going to be examined.

func

Type:Function

It is a predicate, to test each element of the set.

reduce(set, func,initializer)

Returns:Number

Reduces the set to a single value, by executing the provided function for each element in the set (from left-to-right).

set

Type:Zet|Set

Set of typeZet orSet.

func

Type:Function

Function to be executed for each element in the set.

initializer

Type:Number

Optional. A value to be passed to the function as the initial value.

Instance Methods

union(...sets) ∪

Returns:zet

Variadic method that return a new set with elements fromthis and all othersets.

sets

Type:Zet|Set

One or more sets of typeZet orSet.

intersection(...sets) ∩

Returns:zet

Variadic method that return a new set with elements common tothis and all othersets.

sets

Type:Zet|Set

One or more sets of typeZet orSet.

difference(...sets) - or \

Returns:zet

Variadic method tht return a new set with elements inthis that are not in the othersets.

sets

Type:Zet|Set

One or more sets of typeZet orSet.

symmetricDifference(other) ⊖ or ∆

Returns:zet

Method that return a new set with elements in eitherthis orother but not both. This is also known as xor.

other

Type:Zet|Set

Set of typeZet orSet.

subset(other)

Returns:Boolean

Test whether every element in the set is inother.

other

Type:Zet|Set

Set of typeZet orSet.

superset(other)

Returns:Boolean

Test whether every element inother is in the set.

other

Type:Zet|Set

Set of typeZet orSet.

map(func)

Returns:Zet|Set

Creates a set with the results of calling the provided function on every element.

func

Type:Function

Function that produces an element of the new set.

filter(func)

Returns:Zet|Set

Creates a set with all elements that pass the test implemented by the provided function.

func

Type:Function

It is a predicate, to test each element of the set.

reduce(func,initializer)

Returns:Number

Reduces the set to a single value, by executing the provided function for each element in the set (from left-to-right).

func

Type:Function

Function to be executed for each element in the set.

initializer

Type:Number

Optional. A value to be passed to the function as the initial value.

License

MIT ©Terkel Gjervig


[8]ページ先頭

©2009-2025 Movatter.jp