JSPM

super-array

1.1.2
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 5
  • Score
    100M100P100Q23751F
  • License MIT

Simple utility for enhancing any Array with constant O(1) access for properties by unique key

Package Exports

  • super-array

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (super-array) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

super-array

Travis npm npm Coverage Status node GitHub stars GitHub forks license David

Simple utility for enhancing any JavaScript Array with constant O(1) access for properties by unique key

Just like a normal Array but enhanced with a custom .get(id) method

Both for Node and Browser

Install

$ npm install --save super-array

or with Yarn:

$ yarn add super-array

Usage

const SuperArray = require('super-array');

const myArray = new SuperArray([
  {id: 'ab1', name: 'John'},
  {id: 'ab2', name: 'Peter'},
]);

console.log(myArray.get('ab1')); // {id: 'ab1', name: 'John'}
console.log(myArray.get('ab2')); // {id: 'ab2', name: 'Peter'}

or you can use a custom identifier function: (please note that the returned identifier has to be unique)

const SuperArray = require('super-array');

const myArray = new SuperArray([
  {id: 'ab1', name: 'John'},
  {id: 'ab2', name: 'Peter'},
], function(item) { return item.name });

console.log(myArray.get('John')); // {id: 'ab1', name: 'John'}
console.log(myArray.get('Peter')); // {id: 'ab2', name: 'Peter'}

API

SuperArray(arr, identifyFn)

arr (optional):
  • Type: <Array>
  • Default: []
identifyFn (optional):
  • Type: <Function(item|object):identifier|string>
  • Default: function(item) { return item.id }

Returns SuperArray instance.

Support

SuperArray uses ES6 Proxies for object manipulation. Proxies are awesome feature of ES2015 that enables redefining some language operations. For example we can intercept every object property access with our own function.

The problem is that proper Proxy implementation requires native browser support and implementing them with ES5 is not suitable for production environments because performance impact is huge. Supported list:

  • Chrome 49+
  • Firefox 54+
  • Edge 14+
  • Safari 10.1+
  • Opera 47+
  • Node 6+

You can see the full list here.

Contributions

Open for PRs!

License

MIT © Patrik Toma