Skip to content

Commit

Permalink
only->pick
Browse files Browse the repository at this point in the history
  • Loading branch information
Mat Taylor authored and Mat Taylor committed Jun 5, 2023
1 parent 4d44a04 commit 31b609e
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -48,7 +48,7 @@ The following methods are availble to all Objects via protoype inheritence, unle
| [`is`](docs/api.md#is) | Check type of `this` |
| [`[@@iterator]`](docs/api.md#iterator) | Iterate through values of `this` |
| [`clean`](docs/api.md#clean) | Return a copy of `this` without falsey entries |
| [`only`](docs/api.md#only) | Create a copy of `this` with only entries with specific keys or values that that match a filter function |
| [`pick`](docs/api.md#pick) | Create a copy of `this` with only entries with specific keys or values that that match a filter function |
| [`find`](docs/api.md#find) | Find keys of `this` which match a function or value |
| [`assign`](docs/api.md#assign) | Assign new properties to `this` |
| [`extend`](docs/api.md#extend) | Assign default properties to `this` |
Expand Down
10 changes: 6 additions & 4 deletions bench.js
Expand Up @@ -45,16 +45,18 @@ function report(title, ob) {
lodash: () => _.mapValues(ob, v => v+1),
vanilla: () => Object.fromEntries(Object.entries(ob).map(([k,v]) => [k, v+1])),
},
'Only (func)': {
objix: () => ob.only(v => v == 1),
Pick: {
objix: () => ob.pick(v => v == 1),
lodash: () => _.pickBy(ob, v => v == 1),
vanilla: () => Object.fromEntries(Object.entries(ob).flatMap(([k,v]) => v == 1 ? [[k,v]] : [])),
},
'Only (list)': {
objix: () => ob.only(['s1','s2','s3']),
/*
'Pick (list)': {
objix: () => ob.pick(['s1','s2','s3']),
lodash: () => _.pick(ob,['s1','s2','s3']),
vanilla: () => Object.fromEntries(Object.entries(ob).flatMap(([k,v]) => ['s1','s2','s3'].includes(k) ? [[k,v]] : [])),
},
*/
Find: {
objix: () => ob.find(v => v == 1),
lodash: () => _.findKey(ob, v => v == 1),
Expand Down
10 changes: 5 additions & 5 deletions docs/api.md
Expand Up @@ -151,7 +151,7 @@ var o = { a: 1, b: null, c: false, d: 0, e: '' }.clean() // { a: 1 }

</div>

<a id="only"></a>
<a id="pick"></a>

## `Object..filter(function||list, target={})`

Expand All @@ -161,10 +161,10 @@ If the first argument is a list, return `target` with all entries of `this` wher
<div data-runkit>

```javascript
var o = { a: 1, b: 2 }.only(['b']) // { b: 2 }
var o = { a: 1, b: 2 }.only(v => v > 1) // { b: 2 }
var o = { a: 1, b: 2 }.only((v, k) => k == 'b') // { b: 2 }
var o = { a: 1, b: 2 }.only(v => v > 2) // {}
var o = { a: 1, b: 2 }.pick(['b']) // { b: 2 }
var o = { a: 1, b: 2 }.pick(v => v > 1) // { b: 2 }
var o = { a: 1, b: 2 }.pick((v, k) => k == 'b') // { b: 2 }
var o = { a: 1, b: 2 }.pick(v => v > 2) // {}
```

</div>
Expand Down
6 changes: 3 additions & 3 deletions docs/bench.md
Expand Up @@ -23,7 +23,7 @@ For simple object objix performs insanely well, but this drops off quickly when
| (index) | objix | lodash | vanilla | % Inc | % Err |
| ------- | -------- | -------- | -------- | ------- | ----- |
| Map | 7012.38 | 4195.86 | 1050.79 | 67.13 | 14.8 |
| Filter | 38468.78 | 1126.07 | 1529.99 | 3316.2 | 18.86 |
| Pick | 38468.78 | 1126.07 | 1529.99 | 3316.2 | 18.86 |
| Find | 74188.6 | 20799.43 | 15938.13 | 256.69 | 22.95 |
| KeyBy | 9383.75 | 6485.31 | | 44.69 | 19.94 |
| Equals | 2297.18 | 1340.82 | 1240.04 | 71.33 | 11.84 |
Expand All @@ -38,7 +38,7 @@ For simple object objix performs insanely well, but this drops off quickly when
| (index) | objix | lodash | vanilla | % Inc | % Err |
| ------- | -------- | -------- | -------- | ------- | ----- |
| Map | 4259.97 | 2944.87 | 866.31 | 44.66 | 12.15 |
| Filter | 7833.53 | 1042.81 | 1282.9 | 651.19 | 10.03 |
| Pick | 7833.53 | 1042.81 | 1282.9 | 651.19 | 10.03 |
| Find | 76332.7 | 20380.14 | 15779.81 | 274.54 | 23.19 |
| KeyBy | 9069.12 | 6443.92 | | 40.74 | 21.64 |
| Equals | 2071.18 | 1223.36 | 1161.06 | 69.3 | 10.07 |
Expand All @@ -53,7 +53,7 @@ For simple object objix performs insanely well, but this drops off quickly when
| (index) | objix | lodash | vanilla | % Inc | % Err |
| ------- | ------- | ------- | ------- | ------ | ----- |
| Map | 656.5 | 680.92 | 216.47 | -3.59 | 6.55 |
| Filter | 795.29 | 433.24 | 275.08 | 83.57 | 4.57 |
| Pick | 795.29 | 433.24 | 275.08 | 83.57 | 4.57 |
| Find | 3741.3 | 3714.32 | 530.39 | 0.73 | 7.26 |
| KeyBy | 9374.13 | 6412.73 | | 46.18 | 20.06 |
| Equals | 471.47 | 432.91 | 439.48 | 8.91 | 4.88 |
Expand Down
8 changes: 4 additions & 4 deletions objix.js
Expand Up @@ -21,7 +21,7 @@ const
return r
},

only(f, r={}) {
pick(f, r={}) {
for (let k in this) if (f.call ? f(this[k],k) : f.includes(k)) r[k] = this[k]
return r
},
Expand All @@ -31,7 +31,7 @@ const
},

clean() {
return this.only(v => v)
return this.pick(v => v)
},

is(t, i) {
Expand Down Expand Up @@ -71,11 +71,11 @@ const
},

same(o) {
return this.only((v,k) => v.eq(o[k]))
return this.pick((v,k) => v.eq(o[k]))
},

diff(o) {
return this.only((v,k) => !v.eq(o[k]))
return this.pick((v,k) => !v.eq(o[k]))
},

contains(o, d) {
Expand Down
2 changes: 1 addition & 1 deletion objix.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "objix",
"version": "1.13.0",
"version": "1.14.0",
"description": "A dangerously convienient, high performance and super lightweight utility (2.7kb) that injects methods into the Object prototype to sugar for many common use cases working with Javascript objects.",
"main": "objix.js",
"scripts": {
Expand Down

0 comments on commit 31b609e

Please sign in to comment.