darrenscerri / negative-array

Negative array index support `array[-1]` using ES2015 Proxy

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

negative-array Build Status

Negative array index support array[-1] using ES2015 Proxy

JavaScript doesn't natively support the use of a negative index to get items backwards from the end, but with ES2015 Proxy it's possible. Take a look at the source to see how simple it is to implement and read this short article about it.

Proxies are objects for which the programmer has to define the semantics in JavaScript. The default object semantics are implemented in the JavaScript engine, often written in lower-level languages like C++. Proxies let the programmer define most of the behavior of an object in JavaScript. They are said to provide a meta-programming API.

Usage

$ npm install negative-array
const negativeArray = require('negative-array');

// Adds negative array index support to any given array
const unicorn = negativeArray(['pony', 'cake', 'rainbow']);

// Get the last item by using a negative index
console.log(unicorn[-1]);
//=> 'rainbow'

// OMG, YES!

License

MIT © Sindre Sorhus

About

Negative array index support `array[-1]` using ES2015 Proxy

License:MIT License


Languages

Language:JavaScript 100.0%