GithubHelp home page GithubHelp logo

emgyrz / collossus Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 207 KB

Collection of elements of the same type, has everything that we are so lacking in JS Arrays and a little more

Home Page: https://emgyrz.github.io/collossus/

License: MIT License

Shell 1.75% JavaScript 2.87% TypeScript 95.37%

collossus's Introduction

collossus

Collection of elements of the same type, has everything that we are so lacking in JS Arrays and a little more.

npm docs

Benefits and specialities:

  • It is strongly typed: written in TypeScript and has Flow declarations
  • No more boilerplate code when working with arrays
  • Unbelievable helps when your data has identifiers
  • Ready to use with MobX. See this block
  • No built-in objects prototype modifications
  • Unlike some utility libraries (lodash etc.) which are generally very helpful, you may code in OOP style not by calling multiple unrelated functions
  • There is no unnecessary methods, focus only on working with arrays

What it has

Exports two main classes:

  • Collection - Just a wrapper around the Array, adds methods that make life easier (docs).
  • IdCollection - Identified collection. This collection is inherited from the previous one. Designed to work with data that has an id: number | string (docs).

Example:

Some of Collection class
import { Collection } from 'collossus'

const names = new Collection( [ 'Max' ] ) // [ ' Max' ]
names.push( [ 'Yan', 'Li' ] ) // [ ' Max', 'Yan', 'Li' ]
names.last() // -> 'Li'
names.swap( 1, 2 ) // [ ' Max', 'Li', 'Yan' ]
names.pushUniq( 'Max' ) // [ ' Max', 'Li', 'Yan' ]
names.rfindIndexBy( n => n === 'Yan' ) // -> 2
names.clear() // []
Some of IdCollection class
import { IdCollection } from 'collossus'

type User = { id: number, name: string }

const users = new IdCollection<User>( [ { id: 33, name: 'Max' } ] ) // [{ id: 33, name: 'Max' }]
users.push( [ { id: 55, name: 'Yan' } ] ) // [{ id: 33, name: 'Max' },{ id: 55, name: 'Yan' }]
users.findById( 33 ) // -> { id: 33, name: 'Max' }
users.pushUniqById( { id: 55, name: 'Li' } ) // [{ id: 33, name: 'Max' },{ id: 55, name: 'Yan' }]
users.removeById( 55 ) // [ { id: 33, name: 'Max' } ]
users.first() // { id: 33, name: 'Max' }
users.clear() // []

Methods

Collection methods
  • chunks - ( chunkSize: number ) => Array<Array<T>>
  • clear - () => void
  • drainFilterBy - ( predicate: CallbackFuncType<T, boolean> ) => Array<T>
  • filterBy - ( predicate: CallbackFuncType<T, boolean> ) => Collection<T>
  • findBy - ( predicate: CallbackFuncType<T, boolean>, startIndex?: number ) => null | T
  • findIndexBy - ( predicate: CallbackFuncType<T, boolean>, startIndex?: number ) => number
  • first - () => null | T
  • forEach - ( closure: CallbackFuncType<T, void> ) => void
  • get - ( index: number ) => null | T
  • getInnerRef - () => Array<T>
  • has - ( it: T ) => boolean
  • isEmpty - () => boolean
  • last - () => null | T
  • lastIndex - () => number
  • map - <R>( closure: CallbackFuncType<T, R> ) => Collection<R>
  • mapArr - <R>( closure: CallbackFuncType<T, R> ) => Array<R>
  • pop - () => null | T
  • push - ( it: T | Array<T> ) => void
  • pushUniq - ( it: T | Array<T> ) => void
  • pushUniqBy - ( it: T | Array<T>, compare: CompareFuncType<T> ) => void
  • reduce - <A>( callback: ( acc: A, it: T ) => A, initValue: A ) => A
  • remove - ( index: number ) => null | T
  • removeBy - ( predicate: CallbackFuncType<T, boolean> ) => null | T
  • repeat - ( num: number ) => Collection<T>
  • reset - ( data: null | Array<T> ) => void
  • retainBy - ( predicate: CallbackFuncType<T, boolean> ) => Array<T>
  • rfindBy - ( predicate: CallbackFuncType<T, boolean>, startIndex?: number ) => null | T
  • rfindIndexBy - ( predicate: CallbackFuncType<T, boolean>, startIndex?: number ) => number
  • set - ( index: number, it: T ) => boolean
  • shift - () => null | T
  • shuffle - () => void
  • swap - ( indexA: number, indexB: number ) => boolean
  • toArray - () => Array<T>
  • toJSON - () => null | string
  • truncate - ( len: number ) => boolean
IdCollection methods

Using with Mobx

Library exports two classes that prepared to be observable - ObservableCollection and ObservableIdCollection. But since it does not have Mobx in its dependencies you should patch it with your version of Mobx. You need to do it just once in your project before creating instances of observable collection.

import * as mobx from 'mobx'
import { patchObservableCollections, ObservableCollection } from 'collossus'

// call this somewhere on init
patchObservableCollections( mobx )

// now ObservableCollection is **really** observable
const oc = new ObservableCollection( [ 1, 2, 3 ] )

// this is `computed`
oc.length
// this is `action`
oc.push( 4 )
// and this too
oc.remove( 0 )

Everything else is identical to Collection and IdCollection

License

This module is MIT licensed.

Enjoy using!

collossus's People

Contributors

emgyrz avatar

Stargazers

 avatar  avatar  avatar

Watchers

 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.