Skip to content

Commit

Permalink
Merge pull request 1-liners#77 from 1-liners/pop-shift
Browse files Browse the repository at this point in the history
last + butLast
  • Loading branch information
Tomek Wiszniewski committed May 23, 2015
2 parents bc14af0 + 9775c25 commit 2c99cfc
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 0 deletions.
39 changes: 39 additions & 0 deletions documentation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- [between](#between)
- [bitAnd](#bitand)
- [bitOr](#bitor)
- [butLast](#butlast)
- [by](#by)
- [compose](#compose)
- [converge](#converge)
Expand Down Expand Up @@ -44,6 +45,7 @@
- [isUndefined](#isundefined)
- [isUnknown](#isunknown)
- [join](#join)
- [last](#last)
- [length](#length)
- [lessOrEqual](#lessorequal)
- [lessThan](#lessthan)
Expand Down Expand Up @@ -181,6 +183,26 @@ bitOr(1, 1); // => 1
</sup></div>


### butLast

Return a copy of `array`, without the last item.

```js
import butLast from '1-liners/butLast';

const array = [1, 2, 3];

butLast(array); // => [1, 2]
array; // => [1, 2, 3]
```

<div align="right"><sup>
<a href="../tests/butLast.js">Spec</a>
<a href="../module/butLast.js">Source</a>: <code> (array) =&gt; array.slice(0, -1);</code>
</sup></div>


### by

Same as `a / b`
Expand Down Expand Up @@ -883,6 +905,23 @@ join('-', [1, 'liners']); // => '1-liners'
</sup></div>


### last

Returns the last item of `array`.

```js
var last = require('1-liners/last');

last([1, 2, 3]); // => 3
```

<div align="right"><sup>
<a href="../tests/last.js">Spec</a>
<a href="../module/last.js">Source</a>: <code> (array) =&gt; array[array.length - 1];</code>
</sup></div>


### length

Returns the length of an array.
Expand Down
18 changes: 18 additions & 0 deletions module/butLast.js
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);
4 changes: 4 additions & 0 deletions module/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import and from './and';
import between from './between';
import bitAnd from './bitAnd';
import bitOr from './bitOr';
import butLast from './butLast';
import by from './by';
import compose from './compose';
import converge from './converge';
Expand Down Expand Up @@ -37,6 +38,7 @@ import isTypeOf from './isTypeOf';
import isUndefined from './isUndefined';
import isUnknown from './isUnknown';
import join from './join';
import last from './last';
import length from './length';
import lessOrEqual from './lessOrEqual';
import lessThan from './lessThan';
Expand Down Expand Up @@ -81,6 +83,7 @@ export {
between,
bitAnd,
bitOr,
butLast,
by,
compose,
converge,
Expand Down Expand Up @@ -115,6 +118,7 @@ export {
isUndefined,
isUnknown,
join,
last,
length,
lessOrEqual,
lessThan,
Expand Down
15 changes: 15 additions & 0 deletions module/last.js
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];
11 changes: 11 additions & 0 deletions tests/butLast.js
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([]) , [] );
});
6 changes: 6 additions & 0 deletions tests/last.js
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);
});

0 comments on commit 2c99cfc

Please sign in to comment.