diff --git a/documentation/README.md b/documentation/README.md index b78c4a0..c9cd61b 100644 --- a/documentation/README.md +++ b/documentation/README.md @@ -10,6 +10,7 @@ - [between](#between) - [bitAnd](#bitand) - [bitOr](#bitor) +- [butLast](#butlast) - [by](#by) - [compose](#compose) - [converge](#converge) @@ -44,6 +45,7 @@ - [isUndefined](#isundefined) - [isUnknown](#isunknown) - [join](#join) +- [last](#last) - [length](#length) - [lessOrEqual](#lessorequal) - [lessThan](#lessthan) @@ -181,6 +183,26 @@ bitOr(1, 1); // => 1 +### 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] +``` + +
+ + ### by Same as `a / b` @@ -883,6 +905,23 @@ join('-', [1, 'liners']); // => '1-liners' +### last + +Returns the last item of `array`. + +```js +var last = require('1-liners/last'); + +last([1, 2, 3]); // => 3 +``` + + + + ### length Returns the length of an array. diff --git a/module/butLast.js b/module/butLast.js new file mode 100644 index 0000000..0a4cd0f --- /dev/null +++ b/module/butLast.js @@ -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); diff --git a/module/index.js b/module/index.js index 40c9db8..174f4a7 100644 --- a/module/index.js +++ b/module/index.js @@ -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'; @@ -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'; @@ -81,6 +83,7 @@ export { between, bitAnd, bitOr, + butLast, by, compose, converge, @@ -115,6 +118,7 @@ export { isUndefined, isUnknown, join, + last, length, lessOrEqual, lessThan, diff --git a/module/last.js b/module/last.js new file mode 100644 index 0000000..eddd932 --- /dev/null +++ b/module/last.js @@ -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]; diff --git a/tests/butLast.js b/tests/butLast.js new file mode 100644 index 0000000..7b1d7f6 --- /dev/null +++ b/tests/butLast.js @@ -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([]) , [] ); +}); diff --git a/tests/last.js b/tests/last.js new file mode 100644 index 0000000..050a542 --- /dev/null +++ b/tests/last.js @@ -0,0 +1,6 @@ +import { equal } from 'assert'; +import last from '../last'; + +test('#last', () => { + equal(last([1, 2, 3]), 3); +});