Skip to content

Commit

Permalink
Merge pull request 1-liners#141 from 1-liners/drop-array
Browse files Browse the repository at this point in the history
rename drop to omit and add list/array drop
  • Loading branch information
stoeffel committed Oct 6, 2015
2 parents e975929 + 3ee3112 commit e307601
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
31 changes: 26 additions & 5 deletions documentation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
- [nor](#nor)
- [not](#not)
- [nth](#nth)
- [omit](#omit)
- [or](#or)
- [pick](#pick)
- [pipe](#pipe)
Expand Down Expand Up @@ -428,21 +429,22 @@ dec(1); // => 0

### drop

Creates a copy of the `object` without the given `props`.
Returns the tail 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'
```

<div align="right"><sup>
<a href="../tests/drop.js">Spec</a>
<a href="../module/drop.js">Source</a>: <code> (props:Array, object) =&gt; Object.keys(object).reduce((res, k) =&gt; Object.assign(res, props.includes(k) ? null : {[k]: object[k]}), {});</code>
<a href="../module/drop.js">Source</a>: <code> (n, array) =&gt; array.slice(Math.max(n, 0), Infinity);</code>
</sup></div>


Expand Down Expand Up @@ -1523,6 +1525,25 @@ nth(1, [1, 2, 3]); // => 2
</sup></div>


### 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}
```

<div align="right"><sup>
<a href="../tests/omit.js">Spec</a>
<a href="../module/omit.js">Source</a>: <code> (props:Array, object) =&gt; Object.keys(object).reduce((res, k) =&gt; Object.assign(res, props.includes(k) ? null : {[k]: object[k]}), {});</code>
</sup></div>


### or

Same as `a || b`.
Expand Down
11 changes: 6 additions & 5 deletions module/drop.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
*
* @description
*
* Creates a copy of the `object` without the given `props`.
* Returns the tail of `array` after dropping the first `n` elements
*
* @example
*
* 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'
*
*/
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);
2 changes: 2 additions & 0 deletions module/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -174,6 +175,7 @@ export {
nor,
not,
nth,
omit,
or,
pick,
pipe,
Expand Down
18 changes: 18 additions & 0 deletions module/omit.js
Original file line number Diff line number Diff line change
@@ -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}
*
*
*/
export default (props:Array, object) => Object.keys(object).reduce((res, k) => Object.assign(res, props.includes(k) ? null : {[k]: object[k]}), {});
22 changes: 5 additions & 17 deletions tests/drop.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
23 changes: 23 additions & 0 deletions tests/omit.js
Original file line number Diff line number Diff line change
@@ -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}
);
});

0 comments on commit e307601

Please sign in to comment.