Coder Social home page Coder Social logo

supercharge / set Goto Github PK

View Code? Open in Web Editor NEW
7.0 2.0 6.0 113 KB

An Array-aligned Set class and the one JavaScript should have shipped

Home Page: https://superchargejs.com/docs/set

License: MIT License

JavaScript 47.85% TypeScript 52.15%
javascript set class supercharge hacktoberfest hacktoberfest2021

set's Introduction



Set

An Array-aligned Set class and the one JavaScript should have shipped


Installation · Docs · Usage



Latest Version Monthly downloads

Follow @marcuspoehls and @superchargejs for updates!


Introduction

The @supercharge/set package provides an improved Set implementation.

  • aligned with the Array class instead of Map
  • provides helpful methods like .map(callback), .filter(callback), .find(callback), .isEmpty(), and many more.
  • compares values for deep equality and not reference
  • it’s the Set class JavaScript should have shipped

Installation

npm i @supercharge/set

Resources

Quick Usage Overview

Using @supercharge/set is pretty straightforward. The package exports a Set class providing all methods to interact with the set.

const Set = require('@supercharge/set')

const users = new Set()

users.isEmpty() // true

users
  .add({ id: 1, name: 'Marcus' })
  .add({ id: 2, name: 'Norman' })
  .add({ id: 3, name: 'Christian' })

users.isNotEmpty() // true

const usernames = users.map(user => {
  return user.name
})
// [ 'Marcus', 'Norman', 'Christian' ]

const marcus = users.find(user => {
  return user.name === 'Marcus'
})
// { id: 1, name: 'Marcus' }

Contributing

Do you miss a function? We very much appreciate your contribution! Please send in a pull request 😊

  1. Create a fork
  2. Create your feature branch: git checkout -b my-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request 🚀

License

MIT © Supercharge


superchargejs.com  ·  GitHub @supercharge  ·  Twitter @superchargejs

set's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

set's Issues

Add "concat" method

Add a new .concat(...values) method that appends the given values to the end of the set.

Example

Set.of([1, 2, 3]).concat([4, 5]).toArray()
// [1, 2, 3, 4, 5]

Set.of([1, 2, 3]).concat(4, 5).toArray()
// [1, 2, 3, 4, 5]

Add "last" method

Add a new .last(predicate?: (value: T, set: SuperchargedSet) method returns the last item in the set. If a predicate function is provided, return the last item found by predicate.

Example

Set.of([1, 2, 3]).last()
// 3

Set.of([5, 4, 3, 2, 1]).last(value => {
  return value > 3
})
// 4

Add "count" method

Add a new .count(predicate: T => boolean) method that returns the number of items matching the given predicate

Example

Set.of([1, 2, 3, 4, 5]).count(value => {
  return value > 3
})

// 2

Add method: any

Implement an any method that returns true if at least one item in the set matches a given predicate function, otherwise false.

Requirements

Add "first" method

Add a new .first(predicate?: (value: T, set: SuperchargedSet) method returns the first item in the set. If a predicate function is provided, return the first item found by predicate. When providing a predicate function, .first(predicate) is an alias for .find(predicate).

Example

Set.of([1, 2, 3]).first()
// 1

Set.of([1, 2, 3, 4, 5]).first(value => {
  return value > 3
})
// 4

Add method: findLast

Implement a findLast method that returns the last item in the set matching the given predicate function. Returns undefined if no item matching the predicate was found in the set.

Requirements

Add "includes" method

Add a new .includes(value: T | predicate: T => boolean) method that determines whether the set includes the given value or a value identified by the predicate function.

Returns true if the value is found in the collection or the predicate function returns an item from the collection. Returns false otherwise.

Example

Set.of([1, 2, 3, 4, 5]).includes(2)
// true

Set.of([1, 2, 3, 4, 5]).includes(value => {
  return value > 3
})
// true

Set.of([1, 2, 3, 4, 5]).includes(value => {
  return value === 0
})
// false

Add "reduce" method

Add a new .reduce(operation, initialValue) method executes a reducer function on each item of the set, resulting in a single output value.

The method signature may look like this (I’m writing this freestyle, you may need to refine it when working in the code 😄 )

reduce<U>((operation: (previous: U, current: T, set: Set), initial: U): U`

Example

Set.of([1, 2, 3, 4]).reduce((sum, value) => {
  return sum + value
}, 0)

// 10

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.