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

object-list-utils

v1.1.2

Published

Javascript utils for interacting with lists of objects

Downloads

2

Readme

What Up

Need to see if an object is in a list?

const friends = [ {name:'Bob', age:44}, {name:'Bill', age: 33}, {name:'Jane', age:22} ];
const isBillAFriend = containsByProp('name', 'Bill', friends); //=> true
const isHankAFriend = containsByProp('name', 'Hank', friends); //=> false

Need to pull an object out of a list by id?

const people = [{id:1, name:'Bob', age:44}, {id:2, name:'Bill', age:33}, {id:3, name:'Jane', age:22}];
const personOfInterest = findById(3, people); //=> { id: 3, name: 'Jane', age: 22 }

Need to find objects in a list based on nested properties?

const peopleWithPets = [{name:'Bob', pet:{ type:'dog' }}, {name:'Jane', pet:{ type:'cat'}}];
const catLover = findByPath(['pet', 'type'], 'cat', peopleWithPets); //=> { id: 3, name: 'Jane', age: 22 }

object-list-utils does these things and more. Have a look below.

Notes

Some of the utilites allow interacting with object lists using ramda lenses

All functions are curried, so you can create your own customizations by partially applying arguments.

install

npm install object-list-utils --save
# or
yarn add object-list-utils

API Overview

For lists of objects, for example:

[ {p:v1}, {p:v2}, {p:v3}, etc ]
[ {p1: {p2:v1}}, {p1: {p2:v2}}, {p1: {p2:v2}}, etc ]
idxByProp(prop, propVal, objList); // find idx of object in list by prop and val
idxByPath(path, propVal, objList); // find idx of object in list by path and val
idxByLens(lens, propVal, objList); // find idx of object in list by lens and val
containsByProp(prop, propVal, objList); // does list contain object by prop and val
containsByPath(path, propVal, objList); // does list contain object by path and val
containsByLens(lens, propVal, objList); // does list contain object by lens and val
findByProp(prop, propVal, objList); // find object in list by prop and val
findByPath(path, propVal, objList); // find object in list by path and val
findByLens(lens, propVal, objList); // find object in list by lens and val
idxById(id, objectList);       // find idx of object in list with matching id
containsById(id, objectList);  // does list contain object with matching id
findById(id, objectList);      // find object in list with matching id

API Details

Find specified object in a list

// return true if one of the objects in list satisfies view(lens, obj) === propVal,
// otherwise return false
// '' -> a -> [{}] -> {}
export const findByProp = curry((prop, propVal, objList) =>

// example
findByProp('i', 1, [{p:'1st', i:0}, {p:'2nd', i:1}, {p:'3rd', i:2}]); //=> { p:'2nd', i:1 }
// return first object in list with matching path[0].path[1].path[...][propVal]
// or undefined if no matches
// [''] -> a -> [{}] -> {}
export const findByPath = curry((path, propVal, objList) =>

// example
findByPath(['p1', 'p2'], 'c', [ {p1:{p2:'a'}}, {p1:{p2:'b'}}, {p1:{p2:'c'}} ]); //=> { p1: { p2: 'c' } }
// return first object in list with view(lens, obj) === propVal, or undefined if no matches
// lens -> a -> [{}] -> {}
export const findBylens = curry((lens, propVal, objList) =>

// example
const lens = lensPath(['p1', 'p2']);
findByLens(lens, 'a', [ {p1:{p2:'a'}}, {p1:{p2:'b'}}, {p1:{p2:'c'}} ]); //=> { p1: { p2: 'a' } }

Find object index

// return index of first object in list with matching propName[propVal], or -1 if no match
// '' -> a -> [{}] -> int
export const idxByProp = curry((prop, propVal, objList) =>

// example
idxByProp('p', 10, [ {p:5}, {p:10}, {p:15} ]); //=> 1
idxByProp('p', 20, [ {p:5}, {p:10}, {p:15} ]); //=> -1
// return index of first object in list with matching path[0].path[1].path[...][propVal],
// or -1 if no match
//[''] -> a -> [{}] -> int
export const idxByPath = curry((path, propVal, objList) =>

// example
idxByPath(['p1', 'p2'], 'c', [ {p1:{p2:'a'}}, {p1:{p2:'b'}}, {p1:{p2:'c'}} ]); //=> 2
// return index of first object in list with view(lens, obj) === propVal, or -1 if no match
// lens -> a -> [{}] -> int
export const idxByLens = curry((lens, propVal, objList) =>

// example
import { lensPath } from 'ramda';
const lens = lensPath(['p1', 'p2']);
idxByLens(lens, {v:0}, [ {p1:{p2:{v:0}}}, {p1:{p2:{v:1}}} ]); //=> 0

Does an object exist in a list

// return true if one of the objects in list has matching propName[propVal], otherwise return false
// '' -> a -> [{}] -> bool
export const containsByProp = curry((propName, propVal, objList) =>

// example
containsByProp('p', 10, [ {p:5}, {p:10}, {p:15} ]); //=> true
containsByProp('p', 20, [ {p:5}, {p:10}, {p:15} ]); //=> false
// return true if one of the objects in list has matching path[0].path[1].path[...][propVal]
// otherwise return false
//[''] -> a -> [{}] -> bool
export const containsByPath = curry((path, propVal, objList) =>

// example
containsByPath(['p1', 'p2'], 'b', [ {p1:{p2:'a'}}, {p1:{p2:'b'}}, {p1:{p2:'c'}} ]); //=> true
// return true if one of the objects in list satisfies view(lens, obj) === propVal,
// otherwise return false
// lens -> a -> [{}] -> bool
export const containsByLens = curry((lens, propVal, objList) =>

import { lensPath } from 'ramda';
const lens = lensPath(['p1', 'p2']);
containsByLens(lens, {v:1}, [ {p1:{p2:{v:0}}}, {p1:{p2:{v:1}}} ]); //=> true

Utils dealing with lists of objects that have 'id' prop

// return index of first obj in list with matching id, -1 if no match
// id -> [{}] -> int
export const idxById = idxByProp('id');
// reutrn true if one of the objects in list has a matching id, otherwise false
// id -> [{}] -> bool
export const containsById = containsByProp('id');
// return first object in list with matching id, or undefined if no matches
// id -> [{}] -> {}
export const findById = findByProp('id');