From 95b67068771287487bcc31da639519dcdffc74e6 Mon Sep 17 00:00:00 2001 From: David Annopolsky Date: Sun, 4 Oct 2015 20:24:26 -0400 Subject: [PATCH 1/3] rename drop to omit and add list/array drop --- README.md | 2 +- documentation/README.md | 32 +++++++++++++++++++++++++++----- module/drop.js | 13 +++++++------ module/index.js | 2 ++ module/omit.js | 18 ++++++++++++++++++ tests/drop.js | 22 +++++----------------- tests/omit.js | 23 +++++++++++++++++++++++ 7 files changed, 83 insertions(+), 29 deletions(-) create mode 100644 module/omit.js create mode 100644 tests/omit.js diff --git a/README.md b/README.md index e0b5e42..30206e3 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ **Functional tools that couldn’t be simpler.** -We’re proud to present *1-liners* – a dead simple functional utility belt. **[103 one-liner functions][docs]** (and counting). Each hand-crafted with love and attention. +We’re proud to present *1-liners* – a dead simple functional utility belt. **[104 one-liner functions][docs]** (and counting). Each hand-crafted with love and attention. [docs]: ./documentation diff --git a/documentation/README.md b/documentation/README.md index d4f6298..258115c 100644 --- a/documentation/README.md +++ b/documentation/README.md @@ -77,6 +77,7 @@ - [nor](#nor) - [not](#not) - [nth](#nth) +- [omit](#omit) - [or](#or) - [pick](#pick) - [pipe](#pipe) @@ -428,21 +429,22 @@ dec(1); // => 0 ### drop -Creates a copy of the `object` without the given `props`. +Returns the suffix of `array` after dropping the first `n` elements ```js const drop = require('1-liners/drop'); -const object = {foo: 1, bar: 2, baz: 3}; +const array = [1, 2, 3, 4, 5]; + const string = 'Hello World'; -drop(['foo', 'baz'], object); // => {bar: 2} -object; // => {foo: 1, bar: 2, baz: 3} +drop(2, array); // => [3, 4, 5] +drop(6, string); // => 'World' ```
Spec • - Source: (props:Array, object) => Object.keys(object).reduce((res, k) => Object.assign(res, props.includes(k) ? null : {[k]: object[k]}), {}); + Source: (n, array) => array.slice(Math.max(n, 0), Infinity);
@@ -1523,6 +1525,26 @@ nth(1, [1, 2, 3]); // => 2 +### omit + +Creates a copy of the `object` without the given `props`. + +```js +const omit = require('1-liners/omit'); + +const object = {foo: 1, bar: 2, baz: 3}; + +omit(['foo', 'baz'], object); // => {bar: 2} +object; // => {foo: 1, bar: 2, baz: 3} +``` + +
+ Spec + • + Source: (props:Array, object) => Object.keys(object).reduce((res, k) => Object.assign(res, props.includes(k) ? null : {[k]: object[k]}), {}); +
+ + ### or Same as `a || b`. diff --git a/module/drop.js b/module/drop.js index 4812904..8b6bfff 100644 --- a/module/drop.js +++ b/module/drop.js @@ -3,16 +3,17 @@ * * @description * - * Creates a copy of the `object` without the given `props`. + * Returns the suffix of `array` after dropping the first `n` elements * * @example * * const drop = require('1-liners/drop'); * - * const object = {foo: 1, bar: 2, baz: 3}; - * - * drop(['foo', 'baz'], object); // => {bar: 2} - * object; // => {foo: 1, bar: 2, baz: 3} + * const array = [1, 2, 3, 4, 5]; + * const string = 'Hello World'; + * + * drop(2, array); // => [3, 4, 5] + * drop(6, string); // => 'World' * */ -export default (props:Array, object) => Object.keys(object).reduce((res, k) => Object.assign(res, props.includes(k) ? null : {[k]: object[k]}), {}); +export default (n, array) => array.slice(Math.max(n, 0), Infinity); \ No newline at end of file diff --git a/module/index.js b/module/index.js index aa7dd60..66d602d 100644 --- a/module/index.js +++ b/module/index.js @@ -70,6 +70,7 @@ import noop from './noop'; import nor from './nor'; import not from './not'; import nth from './nth'; +import omit from './omit'; import or from './or'; import pick from './pick'; import pipe from './pipe'; @@ -174,6 +175,7 @@ export { nor, not, nth, + omit, or, pick, pipe, diff --git a/module/omit.js b/module/omit.js new file mode 100644 index 0000000..4c15f1c --- /dev/null +++ b/module/omit.js @@ -0,0 +1,18 @@ +/** + * @module 1-liners/omit + * + * @description + * + * Creates a copy of the `object` without the given `props`. + * + * @example + * + * const omit = require('1-liners/omit'); + * + * const object = {foo: 1, bar: 2, baz: 3}; + * + * omit(['foo', 'baz'], object); // => {bar: 2} + * object; // => {foo: 1, bar: 2, baz: 3} + * + */ +export default (props:Array, object) => Object.keys(object).reduce((res, k) => Object.assign(res, props.includes(k) ? null : {[k]: object[k]}), {}); diff --git a/tests/drop.js b/tests/drop.js index d955fad..7b8803d 100644 --- a/tests/drop.js +++ b/tests/drop.js @@ -2,22 +2,10 @@ import { deepEqual } from 'assert'; import drop from '../drop'; test('#drop', () => { - const object = {foo: 1, bar: 2, baz: 3}; + const array = [1, 2, 3, 4, 5]; + const string = 'Hello World'; - deepEqual( - drop(['foo', 'baz'], object), - {bar: 2} - ); - deepEqual( - drop([], object), - object - ); - deepEqual( - drop(['oof'], object), - object - ); - deepEqual( - object, - {foo: 1, bar: 2, baz: 3} - ); + deepEqual(drop(2, array), [3, 4, 5]); + deepEqual(drop(array.length + 1, array), []); + deepEqual(drop(6, string), 'World'); }); diff --git a/tests/omit.js b/tests/omit.js new file mode 100644 index 0000000..5be9449 --- /dev/null +++ b/tests/omit.js @@ -0,0 +1,23 @@ +import { deepEqual } from 'assert'; +import omit from '../omit'; + +test('#omit', () => { + const object = {foo: 1, bar: 2, baz: 3}; + + deepEqual( + omit(['foo', 'baz'], object), + {bar: 2} + ); + deepEqual( + omit([], object), + object + ); + deepEqual( + omit(['oof'], object), + object + ); + deepEqual( + object, + {foo: 1, bar: 2, baz: 3} + ); +}); From 789309294adea67553c9a1e015b7d0b3cc9ebfe7 Mon Sep 17 00:00:00 2001 From: David Annopolsky Date: Mon, 5 Oct 2015 09:00:47 -0400 Subject: [PATCH 2/3] address PR comments --- documentation/README.md | 2 +- module/drop.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/documentation/README.md b/documentation/README.md index 258115c..b94e414 100644 --- a/documentation/README.md +++ b/documentation/README.md @@ -429,7 +429,7 @@ dec(1); // => 0 ### drop -Returns the suffix of `array` after dropping the first `n` elements +Returns the tail of `array` after dropping the first `n` elements ```js const drop = require('1-liners/drop'); diff --git a/module/drop.js b/module/drop.js index 8b6bfff..7139abc 100644 --- a/module/drop.js +++ b/module/drop.js @@ -3,7 +3,7 @@ * * @description * - * Returns the suffix of `array` after dropping the first `n` elements + * Returns the tail of `array` after dropping the first `n` elements * * @example * @@ -11,9 +11,9 @@ * * const array = [1, 2, 3, 4, 5]; * const string = 'Hello World'; - * + * * drop(2, array); // => [3, 4, 5] * drop(6, string); // => 'World' * */ -export default (n, array) => array.slice(Math.max(n, 0), Infinity); \ No newline at end of file +export default (n, array) => array.slice(Math.max(n, 0), Infinity); From 3ee3112b21b656dea12e1f6fbf56737d04eef24f Mon Sep 17 00:00:00 2001 From: David Annopolsky Date: Tue, 6 Oct 2015 09:16:06 -0400 Subject: [PATCH 3/3] addressed PR comment for original object --- documentation/README.md | 1 - module/omit.js | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/documentation/README.md b/documentation/README.md index b94e414..be3192f 100644 --- a/documentation/README.md +++ b/documentation/README.md @@ -1535,7 +1535,6 @@ const omit = require('1-liners/omit'); const object = {foo: 1, bar: 2, baz: 3}; omit(['foo', 'baz'], object); // => {bar: 2} -object; // => {foo: 1, bar: 2, baz: 3} ```
diff --git a/module/omit.js b/module/omit.js index 4c15f1c..02276bb 100644 --- a/module/omit.js +++ b/module/omit.js @@ -12,7 +12,7 @@ * const object = {foo: 1, bar: 2, baz: 3}; * * omit(['foo', 'baz'], object); // => {bar: 2} - * object; // => {foo: 1, bar: 2, baz: 3} + * * */ export default (props:Array, object) => Object.keys(object).reduce((res, k) => Object.assign(res, props.includes(k) ? null : {[k]: object[k]}), {});