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
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-arrayor with Yarn:
$ yarn add super-arrayUsage
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