forked from 1-liners/1-liners
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request 1-liners#77 from 1-liners/pop-shift
last + butLast
- Loading branch information
Showing
6 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* @module 1-liners/butLast | ||
* | ||
* @description | ||
* | ||
* Return a copy of `array`, without the last item. | ||
* | ||
* @example | ||
* | ||
* import butLast from '1-liners/butLast'; | ||
* | ||
* const array = [1, 2, 3]; | ||
* | ||
* butLast(array); // => [1, 2] | ||
* array; // => [1, 2, 3] | ||
* | ||
*/ | ||
export default (array) => array.slice(0, -1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* @module 1-liners/last | ||
* | ||
* @description | ||
* | ||
* Returns the last item of `array`. | ||
* | ||
* @example | ||
* | ||
* var last = require('1-liners/last'); | ||
* | ||
* last([1, 2, 3]); // => 3 | ||
* | ||
*/ | ||
export default (array) => array[array.length - 1]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { deepEqual } from 'assert'; | ||
import butLast from '../butLast'; | ||
|
||
test('#butLast', () => { | ||
const array = [1, 2, 3]; | ||
|
||
deepEqual(butLast(array) , [1, 2] ); | ||
deepEqual(array , [1, 2, 3] ); | ||
|
||
deepEqual(butLast([]) , [] ); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { equal } from 'assert'; | ||
import last from '../last'; | ||
|
||
test('#last', () => { | ||
equal(last([1, 2, 3]), 3); | ||
}); |