npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@davestewart/collection-fns

v1.0.1

Published

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

Downloads

2

Readme

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:

| Function | Keyed | Description | Returns | | ----------- | ------ | ------------------------------------------------------------ | ------- | | first |   | Get the first model in a collection | model | | last |   | Get the last model in a collection | model | | has | x | Test if a collection has a model | boolean | | get | x | Get a model from a collection | model | | getIndex | x | Get the index of a model in a collection | number | | getRandom |   | Get a random model from a collection | model | | add | x | Add a model to a collection, or if it already exists, update | model | | addOrMove | x | Add a model to a collection, or if it already exists, move it to an index | model | | update | x | Update a model if it exists in a collection | model | | move | x | Move a model in a collection to a specific index in the same or a different array | model | | moveToEnd | x | Move a model in a collection to the end of the same or a different array | model | | moveByIndex |   | Move a model in a collection from one index to another in the same or a different array | model | | remove | x | Remove a model from a collection | model |

Collections

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

| Function | Keyed | Description | Returns | | -------- | ------ | ------------------------------------------------------------ | -------- | | forEach |   | Iterate over a collection of models and call a function on each model | array | | map |   | Iterate over a collection of models, call a function on each model, and return the updated array | array | | filter | o | Filter a collection of models by property, including matched values | array | | omit | o | Filter a collection of models by property, omitting matched values | array | | dedupe | x | Filter a collection of models, omitting those with duplicate ids | array | | merge | x | Given two arrays of models, add the models not found in the first array from the second array, and return the new array | array | | sort | o | Sort a collection of models by property | array | | sortBy |   | Utility function to return a sort() comparison function | function |

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