array-miner

Search through an array of objects easily and efficiently.

Usage no npm install needed!

<script type="module">
  import arrayMiner from 'https://cdn.skypack.dev/array-miner';
</script>

README

array-miner 0.1.0

Search through an array of objects easily and efficiently.

Build Status

Quick Start

var arrayMiner = require('array-miner');

var data = [
  { id: 1, color: 'red', type: 'foo' },
  { id: 2, color: 'white', type: 'foo' },
  { id: 3, type: 'bar' }
];

arrayMiner.find(data , 'foo');
// [ { id: 1, color: 'red', type: 'foo' },
//   { id: 2, color: 'white', type: 'foo' } ]

If you plan on searching through the data repeatedly, load it with add.

arrayMiner.add(data).find('foo');
// [ { id: 1, color: 'red', type: 'foo' },
//   { id: 2, color: 'white', type: 'foo' } ]

For more options and examples, check out the unit tests in test/app.js or the API.

API

add

Add items as an array or as individual parameters.

arrayMiner.add([
  { foo: 123, bar: 1 },
  { apply: 1, pie: 1 },
  { 123: 'foo', 'some-key': 4 }
]);

arrayMiner.add({ id: 'unique' }, { foo: 4 }, { monkey: 'foo' });

clear

Clears out all items previously added.

arrayMiner.clear();

count

Returns the length of the current data. You could use .data().length, but this is faster.

arrayMiner.count();

data

Returns all the loaded data, dereferenced.

arrayMiner.data();

find

Any object in the array with a matching property value will be returned in the results. Comparison is done with ===.

arrayMiner.find('fountain of youth');

options

Set or get options. This method is chainable.

// set an option
arrayMiner.options('maxCacheLength', 100);

// get an option
arrayMiner.options('maxCacheLength');

// set multiple options
arrayMiner.options({
  maxCacheLength: 100,
  dereferenceResults: true
});

// get all options
arrayMiner.options();

Option Descriptions

dereferenceResults

Turn dereferencing on/off. When off, queries are faster, but results are returned by reference. By default, dereferencing is false.

arrayMiner.dereference(true);

cache

Set the number if query results to cache. By default, this is undefined, which caches everything (until the cache is invalidated by another operation).

arrayMiner.cache(10);

version

Check the current version.

arrayMiner.version;