From 24d03a02261b2284ddb564091f0f0bd10e8afd19 Mon Sep 17 00:00:00 2001 From: endremgs Date: Mon, 10 Jul 2023 15:46:13 +0200 Subject: [PATCH] head, first & last rules Add rules for head, first & last with Array.prototype.at() given as a native alternative --- README.md | 23 +++++++++++++++++++++++ lib/rules/rules.json | 11 +++++++++-- tests/lib/rules/all.js | 20 ++++++++++++++++---- tests/unit/all.js | 19 +++++++++++++++++++ 4 files changed, 67 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 8f491d8..33a1896 100644 --- a/README.md +++ b/README.md @@ -522,6 +522,13 @@ Returns the first element of an array. Passing n will return the first n element [1, 2, 3, 4, 5].slice(0, 2); // => [1, 2] + + // Native with ES13 + [1, 2, 3, 4, 5].at(0) + // => 1 + //or + [].at(0) + // => undefined ``` #### Browser Support for `Array.prototype.slice()` @@ -530,6 +537,12 @@ Returns the first element of an array. Passing n will return the first n element :-: | :-: | :-: | :-: | :-: | :-: | 1.0 ✔ | ✔ | 1.0 ✔ | ✔ | ✔ | ✔ | +#### Browser Support for `Array.prototype.at()` + +![Chrome][chrome-image] | ![Edge][edge-image] | ![Firefox][firefox-image] | ![IE][ie-image] | ![Opera][opera-image] | ![Safari][safari-image] +:-: | :-: | :-: | :-: | :-: | :-: | + 92 ✔ | 92 ✔ | 90 ✔ | ✖ | 78 ✔ | 15.4 ✔ | + **[⬆ back to top](#quick-links)** ### _.flatten @@ -693,6 +706,10 @@ Gets the first element or all but the first element. // output: 1 console.log(tail) // output [2, 3] + + // Native replacement for _.head in ES13 + array.at(0) + // output: 1 ``` #### Browser Support for Spread in array literals @@ -701,6 +718,12 @@ Gets the first element or all but the first element. :-: | :-: | :-: | :-: | :-: | :-: | 46.0 ✔ | 12.0 ✔ | 16.0 ✔ | ✖ | 37.0 ✔ | 8.0 ✔ | +#### Browser Support for `Array.prototype.at()` + +![Chrome][chrome-image] | ![Edge][edge-image] | ![Firefox][firefox-image] | ![IE][ie-image] | ![Opera][opera-image] | ![Safari][safari-image] +:-: | :-: | :-: | :-: | :-: | :-: | + 92 ✔ | 92 ✔ | 90 ✔ | ✖ | 78 ✔ | 15.4 ✔ | + **[⬆ back to top](#quick-links)** ### _.indexOf diff --git a/lib/rules/rules.json b/lib/rules/rules.json index 0f1afee..9620f4c 100644 --- a/lib/rules/rules.json +++ b/lib/rules/rules.json @@ -21,7 +21,8 @@ }, "last": { "compatible": true, - "alternative": "Array.prototype.slice()" + "alternative": "Array.prototype.at(-1) or Array.prototype.slice()", + "ES13": true }, "lastIndexOf": { "compatible": true, @@ -48,7 +49,8 @@ }, "first": { "compatible": true, - "alternative": "Array.prototype.slice() or arr[0]" + "alternative": "Array.prototype.at(0) or Array.prototype.slice()", + "ES13": true }, "findIndex": { "compatible": false, @@ -312,5 +314,10 @@ "isArrayBuffer": { "compatible": false, "alternative": "value instanceof ArrayBuffer" + }, + "head": { + "compatible": true, + "alternative": "Array.prototype.at(0)", + "ES13": true } } diff --git a/tests/lib/rules/all.js b/tests/lib/rules/all.js index 39f9242..c34227a 100644 --- a/tests/lib/rules/all.js +++ b/tests/lib/rules/all.js @@ -104,28 +104,30 @@ ruleTester.run('underscore.isNaN', rules['is-nan'], { ruleTester.run('_.first', rules['first'], { valid: [ '[0, 1, 3][0]', + '[0, 1, 3].at(0)', '[0, 1, 3].slice(0, 2)' ], invalid: [{ code: '_.first([0, 1, 3])', - errors: ['Consider using the native Array.prototype.slice() or arr[0]'] + errors: ['Consider using the native Array.prototype.at(0) or Array.prototype.slice()'] }, { code: '_.first([0, 1, 3], 2)', - errors: ['Consider using the native Array.prototype.slice() or arr[0]'] + errors: ['Consider using the native Array.prototype.at(0) or Array.prototype.slice()'] }] }); ruleTester.run('_.last', rules['last'], { valid: [ 'var numbers = [0, 1, 3]; numbers[numbers.length - 1]', + '[0, 1, 3].at(-1)', '[0, 1, 3].slice(-2)' ], invalid: [{ code: '_.last([0, 1, 3])', - errors: ['Consider using the native Array.prototype.slice()'] + errors: ['Consider using the native Array.prototype.at(-1) or Array.prototype.slice()'] }, { code: '_.last([0, 1, 3], 2)', - errors: ['Consider using the native Array.prototype.slice()'] + errors: ['Consider using the native Array.prototype.at(-1) or Array.prototype.slice()'] }] }); @@ -225,3 +227,13 @@ ruleTester.run('_.endsWith', rules['ends-with'], { }] }); +ruleTester.run('_.head', rules['head'], { + valid: [ + '[0, 1, 3].at(0)', + ], + invalid: [{ + code: '_.head([0, 1, 3])', + errors: ['Consider using the native Array.prototype.at(0)'] + }] +}); + diff --git a/tests/unit/all.js b/tests/unit/all.js index abdb8ed..d164f30 100644 --- a/tests/unit/all.js +++ b/tests/unit/all.js @@ -977,5 +977,24 @@ describe('code snippet example', () => { it('_.last([1,2,3,4,5])', () => { assert.deepEqual(_.last([1,2,3,4,5]), [1,2,3,4,5].at(-1)); }); + it('_.last([])', () => { + assert.deepEqual(_.last([]), [].at(-1)); + }); }); + describe('first', () => { + it('_.first([1,2,3,4,5])', () => { + assert.deepEqual(_.first([1,2,3,4,5]), [1,2,3,4,5].at(0)); + }); + it('_.first([])', () => { + assert.deepEqual(_.first([]), [].at(0)); + }); + }) + describe('head', () => { + it('_.head([1,2,3,4,5])', () => { + assert.deepEqual(_.head([1,2,3,4,5]), [1,2,3,4,5].at(0)); + }); + it('_.head([])', () => { + assert.deepEqual(_.head([]), [].at(0)); + }); + }) });