Skip to content

Commit

Permalink
Merge pull request #373 from Endremgs/master
Browse files Browse the repository at this point in the history
head, first & last rules
  • Loading branch information
ODudek committed Jul 18, 2023
2 parents 32ea411 + 24d03a0 commit 423a057
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()`
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
11 changes: 9 additions & 2 deletions lib/rules/rules.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -312,5 +314,10 @@
"isArrayBuffer": {
"compatible": false,
"alternative": "value instanceof ArrayBuffer"
},
"head": {
"compatible": true,
"alternative": "Array.prototype.at(0)",
"ES13": true
}
}
20 changes: 16 additions & 4 deletions tests/lib/rules/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()']
}]
});

Expand Down Expand Up @@ -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)']
}]
});

19 changes: 19 additions & 0 deletions tests/unit/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
})
});

0 comments on commit 423a057

Please sign in to comment.