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

arraysearch

v1.2.0

Published

search arrays or maps for objects with desired properties

Downloads

96

Readme

ArraySearch


JavaScript utility that allows for robust deep searching of arrays of complex objects. Includes the ability to search by any number of properties within an object, or to find objects based on elements contained in within an array of that object.

See my blog post http://blog.thunderlab.net/arraysearch-and-natural-language-functions for an explanation of how it works and some of the reasoning in my development.

Installation

npm install arraysearch NPM

bower install ArraySearch Bower

In node.js, require ArraySearch:

var find = require('arraysearch').Finder

To use a bower component or just in the browser, simply include the script and initialize the finder:

var find = new Finder();

Usage

Calling the finder:

result = find.[one|all].in(collection).with(filter)

accepts a filter to compare against objects in the collection (either a array or a map)

result = find.[one|all].in(collection).having(searchPath).with(filter)

accepts a filter and a the target path of a array within the object to search in

Return Type:

  • one: returns the first object found meeting the search filter
  • all: returns an array or map (depends on original collection) of all objects found meeting the search filter

If no objects meeting the filter are found, either undefined or an empty collection will be returned, depending on the return type.

Parameters:

  • collection: an array or map of objects (with a similar structure)
  • filter: properties to search for within each object, should be an object (see examples below)
  • searchPath: an object indicating the location of any array within each object (see examples below)

With v1.1, a map of objects can be provided. Searching for a single result will return a single object, while searching for all will return a map of results. This will temporarily modify each object in the map to preserve its key.

Examples

var find = Finder(),
people = [
	{name: 'Joe', age: 21, hair: 'brown'},
	{name: 'Larry', age: 22},
	{name: 'Bob', age: 18, gender: 'M'}
],
buildings = [
	{
		id: 59,
		stories: 2,
		roof: {
			color: 'black',
			material: 'clay'
		},
		exitSigns : [
			{id: 0, floor: 1},
			{id: 1, floor: 1}
		]
	},
	{
		id: 62,
		stories: 3,
		address: '123 Main St',
		roof: {
			color: 'red',
			material: 'clay'
		},
		exitSigns : [
			{id: 0, floor: 1, color: 'red'},
			{id: 1, floor: 1},
			{id: 2, floor: 2}
		]
	}
]

//basic properties search
//Returns {name: 'Larry', age: 22}
find.one.in(people).with({name: 'Larry'})
//Returns [{name: 'Bob', age: 18, gender: 'M'}]
find.all.in(people).with({age: 18})

//nested properties search
//Returns {id: 62, ...}
find.one.in(buildings).with({roof: {color: 'red'}, stories: 3})

//nested array search
//Returns [{id: 59, ...}, {id: 62, ...}]
find.all.in(buildings).having('exitSigns').with({floor: 1})
find.all.in(buildings).having(['exitSigns']).with({floor: 1})
find.all.in(buildings).having({exitSigns: []}).with({floor: 1})

//keys search
//returns [{name: 'Bob', age: 18, gender: 'M'}]
find.all.in(people).with.keys('gender')
find.all.in(people).with.keys(['age','gender'])
//returns [{name: 'Joe',...},{name: 'Bob',...}]
find.all.in(people).with.any.keys(['hair','gender'])

//nested keys search
//returns [{id: 62, ...}]
find.all.in(buildings).having('exitSigns').with.keys('color')
//returns {id: 59, ...}
find.one.in(buildings).having('exitSigns').with.any.keys(['color','floor'])

Future features

  • the ability to search for for objects using relational operators (>,<,...):

    find.[all|one].in(array).having(searchPath).[above|below|at[least|most]](number)

  • extend/replace array find prototype method (maybe)