├── .npmrc ├── .gitattributes ├── .gitignore ├── .editorconfig ├── index.test-d.ts ├── .github └── workflows │ └── main.yml ├── index.d.ts ├── package.json ├── index.js ├── test.js ├── license └── readme.md /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType, expectError, expectAssignable} from 'tsd'; 2 | import negativeArray from './index.js'; 3 | 4 | const readonlyArray = ['🐴', '🎂', '🌈'] as const; 5 | const array = ['🐴', '🎂', '🌈']; 6 | 7 | expectAssignable(negativeArray(readonlyArray)); 8 | expectError(negativeArray(readonlyArray).push('🦄')); 9 | expectType(negativeArray(array)); 10 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 16 14 | steps: 15 | - uses: actions/checkout@v2 16 | - uses: actions/setup-node@v2 17 | with: 18 | node-version: ${{ matrix.node-version }} 19 | - run: npm install 20 | - run: npm test 21 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | Negative array index support `array[-1]` using [ES2015 `Proxy`](https://ponyfoo.com/articles/es6-proxies-in-depth). 3 | 4 | @example 5 | ``` 6 | import negativeArray from 'negative-array'; 7 | 8 | // Adds negative array index support to any given array 9 | const unicorn = negativeArray(['🐴', '🎂', '🌈']); 10 | 11 | // Get the last item by using a negative index 12 | console.log(unicorn[-1]); 13 | //=> '🌈' 14 | 15 | // OMG, YES! 16 | ``` 17 | */ 18 | export default function negativeArray(array: T): T; 19 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "negative-array", 3 | "version": "3.0.0", 4 | "description": "Negative array index support `array[-1]` using ES2015 Proxy", 5 | "license": "MIT", 6 | "repository": "sindresorhus/negative-array", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": "./index.js", 15 | "engines": { 16 | "node": "^12.20.0 || ^14.13.1 || >=16.0.0" 17 | }, 18 | "scripts": { 19 | "test": "xo && ava && tsd" 20 | }, 21 | "files": [ 22 | "index.js", 23 | "index.d.ts" 24 | ], 25 | "keywords": [ 26 | "array", 27 | "negative", 28 | "index", 29 | "indice", 30 | "reverse", 31 | "proxy", 32 | "proxies", 33 | "es2015" 34 | ], 35 | "devDependencies": { 36 | "ava": "^3.15.0", 37 | "tsd": "^0.17.0", 38 | "xo": "^0.44.0" 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | export default function negativeArray(array) { 2 | if (!Array.isArray(array)) { 3 | throw new TypeError('Expected an array'); 4 | } 5 | 6 | return new Proxy(array, { 7 | get(target, name, receiver) { 8 | if (typeof name !== 'string') { 9 | return Reflect.get(target, name, receiver); 10 | } 11 | 12 | const index = Number(name); 13 | 14 | if (Number.isNaN(index)) { 15 | return Reflect.get(target, name, receiver); 16 | } 17 | 18 | return target[index < 0 ? target.length + index : index]; 19 | }, 20 | set(target, name, value, receiver) { 21 | if (typeof name !== 'string') { 22 | return Reflect.set(target, name, value, receiver); 23 | } 24 | 25 | const index = Number(name); 26 | 27 | if (Number.isNaN(index)) { 28 | return Reflect.set(target, name, value, receiver); 29 | } 30 | 31 | target[index < 0 ? target.length + index : index] = value; 32 | 33 | return true; 34 | }, 35 | }); 36 | } 37 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import negativeArray from './index.js'; 3 | 4 | test('quacks like an array', t => { 5 | const fixture = negativeArray(['foo', 'bar', 'baz']); 6 | t.is(fixture.length, 3); 7 | t.is(fixture.toString(), 'foo,bar,baz'); 8 | }); 9 | 10 | test('get values', t => { 11 | const fixture = negativeArray(['foo', 'bar', 'baz']); 12 | t.is(fixture[0], 'foo'); 13 | t.is(fixture[1], 'bar'); 14 | t.is(fixture[-1], 'baz'); 15 | t.is(fixture[-2], 'bar'); 16 | }); 17 | 18 | test('set values', t => { 19 | const fixture = negativeArray(['foo', 'bar', 'baz']); 20 | fixture[0] = 0; 21 | t.deepEqual(fixture, [0, 'bar', 'baz']); 22 | fixture[1] = 1; 23 | t.deepEqual(fixture, [0, 1, 'baz']); 24 | fixture[-1] = -1; 25 | t.deepEqual(fixture, [0, 1, -1]); 26 | fixture[-2] = -2; 27 | t.deepEqual(fixture, [0, -2, -1]); 28 | }); 29 | 30 | test('only accepts arrays', t => { 31 | t.throws(() => { 32 | negativeArray({}); 33 | }); 34 | }); 35 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # negative-array 2 | 3 | > Negative array index support `array[-1]` using [ES2015 `Proxy`](https://ponyfoo.com/articles/es6-proxies-in-depth) 4 | 5 | JavaScript doesn't natively support the use of a negative index to get items backwards from the end, but with [ES2015 Proxy](http://soft.vub.ac.be/~tvcutsem/proxies/) it's possible. Take a look at the [source](index.js) to see how simple it is to implement and read this [short article](http://dailyjs.com/2013/11/15/negative-array/) about it. 6 | 7 | **Note:** With [Node.js 16.6.0](https://nodejs.org/en/blog/release/v16.6.0/), you can now use `Array#at()` instead of this package. 8 | 9 | ## Install 10 | 11 | ``` 12 | $ npm install negative-array 13 | ``` 14 | 15 | ## Usage 16 | 17 | ```js 18 | import negativeArray from 'negative-array'; 19 | 20 | // Adds negative array index support to any given array 21 | const unicorn = negativeArray(['🐴', '🎂', '🌈']); 22 | 23 | // Get the last item by using a negative index 24 | console.log(unicorn[-1]); 25 | //=> '🌈' 26 | 27 | // OMG, YES! 28 | ``` 29 | 30 | ## Related 31 | 32 | - [on-change](https://github.com/sindresorhus/on-change) - Watch an object or array for changes (Uses `Proxy` too) 33 | - [known](https://github.com/sindresorhus/known) - Allow only access to known object properties (Uses `Proxy` too) 34 | --------------------------------------------------------------------------------