Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@davestewart/collection-fns

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@davestewart/collection-fns

A set of flexible functions to query and manipulate arrays of models

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

Collection Fns

Abstract

Collection Fns is a set of flexible, type-safe functions designed to manipulate collections of models:

  • a model is defined as an object with a common identifier such as id, guid or someId
  • a collection is defined as an Array of models sharing the same id key
  • flexible functions is defined that any function can applied to any collection of arbitrary models

The project has the following goals:

  • to provide a basic set of array collection / model manipulation functions
  • to target models by arbitrary property (defaulting to id)
  • to be expressive and flexible
  • to be purely functional
  • to be TypeScript native

The end result is you use simple, safe and robust helper functions to maniulate arrays of models without ever having to resort to writing repetitive, complex, fragile or error-prone array-centric code.

Functions

Note the "keyed" column below, for functions which take an optional key parameter, allowing you to target any model schema (defaults to 'id').

  • an x means the model id is keyed
  • an o means a different property is keyed

Models

These functions manage single models within a collection:

FunctionKeyedDescriptionReturns
first Get the first model in a collectionmodel
last Get the last model in a collectionmodel
hasxTest if a collection has a modelboolean
getxGet a model from a collectionmodel
getIndexxGet the index of a model in a collectionnumber
getRandom Get a random model from a collectionmodel
addxAdd a model to a collection, or if it already exists, updatemodel
addOrMovexAdd a model to a collection, or if it already exists, move it to an indexmodel
updatexUpdate a model if it exists in a collectionmodel
movexMove a model in a collection to a specific index in the same or a different arraymodel
moveToEndxMove a model in a collection to the end of the same or a different arraymodel
moveByIndex Move a model in a collection from one index to another in the same or a different arraymodel
removexRemove a model from a collectionmodel

Collections

These functions manipulate collections, offering simple lodash-like functionality:

FunctionKeyedDescriptionReturns
forEach Iterate over a collection of models and call a function on each modelarray
map Iterate over a collection of models, call a function on each model, and return the updated arrayarray
filteroFilter a collection of models by property, including matched valuesarray
omitoFilter a collection of models by property, omitting matched valuesarray
dedupexFilter a collection of models, omitting those with duplicate idsarray
mergexGiven two arrays of models, add the models not found in the first array from the second array, and return the new arrayarray
sortoSort a collection of models by propertyarray
sortBy Utility function to return a sort() comparison functionfunction

Installation

Install via the command line:

npm i @davestewart/collection-fns

Usage

Here are some basic examples showing the flexibility and functional nature of the library:

// get the model identified by an id of 5
get(models, 5)
// update the window identified by a windowId of 10 with new data 
update(windows, 10, data, 'windowId')
// move a tab identified by a tabId 15 to the 5th index in another collection
move(left, 15, 5, right, 'tabId')
// sort a collection of users by first name
sort(users, 'firstName')

Check the example files for full code:

  • Basic – manage arrays of arbitrary models
  • Advanced – compose the functions into reusable collection classes
  • Generics – example of using and overriding generic type-safety

Generic type safety

The package's functions are generic meaning that the values you supply the function will enforce their own type checking.

Consider the following; the people array should not be able to be updated with the wrong information:

import { update } from '@davestewart/collection-fns'

const people = [
  { id: 1, name: 'tom' },
  { id: 2, name: 'dick' },
  { id: 3, name: 'harry' },
]

update(people, 2, { age: 100 }) // error! Object literal may only specify known properties, and 'age' does not exist in type 'Partial<{ id: number; name: string; }>'.

To force an update, type the payload as any:

update(people, 2, { age: 100 } as any)

You can be sure that TypeScript's got your back when shuffling models within and between collections!

Scripts

  • npm run dev - build and watch the package for changes

  • npm run build - build the package for production

  • npm run prepare - lint and fix, then build the package ready for publishing

  • npm run lint - run linting

  • npm run lint:fix - run linting and fix errors

  • npm run test - run and watch unit tests

Contributing

Adding new functionality:

  • write code
  • write tests
  • run tests / check coverage
  • update docs

Publishing:

  • update package version (minor or patch)
  • run scripts:
npm run prepare
npm publish

Keywords

FAQs

Package last updated on 16 Nov 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc