@cjser/negative-array
v3.0.0-cjser.2
Published
Negative array index support `array[-1]` using ES2015 Proxy
Downloads
67
Maintainers
Readme
negative-array
Negative array index support
array[-1]using ES2015Proxy
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.
Note: With Node.js 16.6.0, you can now use Array#at() instead of this package.
Install
$ npm install negative-arrayUsage
import negativeArray from 'negative-array';
// Adds negative array index support to any given array
const unicorn = negativeArray(['🐴', '🎂', '🌈']);
// Get the last item by using a negative index
console.log(unicorn[-1]);
//=> '🌈'
// OMG, YES!Related
- on-change - Watch an object or array for changes (Uses
Proxytoo) - known - Allow only access to known object properties (Uses
Proxytoo)
cjser
This package is a CommonJS-compatible build generated by cjser for projects that still need require() support. The source version matches the original npm package version, with a cjser prerelease suffix for this generated build.
Original repository: https://github.com/sindresorhus/negative-array
