Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

head, first & last rules #373

Merged
merged 1 commit into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Array.prototype.slice() may not be a relevant alternative anymore depending on the version of lodash used, as versions greater than 3.10.1 do not support a second argument in .first() and .last() which I suppose is the primary use case for 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));
});
})
});