Skip to content

Commit

Permalink
keyBy refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Mat Taylor authored and Mat Taylor committed Jun 6, 2023
1 parent 873abbf commit 01f18c7
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -65,7 +65,7 @@ The following methods are availble to all Objects via protoype inheritence, unle
| [`contains`](docs/api.md#contains) | Check if `this` contains all entries from another object to a given depth. |
| [`eq`](docs/api.md#eq) | Compare key and value identity between `this` and other objects to a given depth |
| [`size`](docs/api.md#size) | Return number of entres in `this`. |
| [`keyBy`](docs/api.md#keyBy) | Index an array of objects into `this` using a given key |
| [`keyBy`](docs/api.md#keyBy) | Re-index values of this `this` using a given key path |
| [`memo`](docs/api.md#memo) | Memoize `this` as a function with configurable result cache expiration |
| [`bind`](docs/api.md#bind) | Assign a function as a method of `this` with optional memoization |
| [`log`](docs/api.md#log) | Conditionally write `this` to the console with an optional message |
Expand Down
2 changes: 1 addition & 1 deletion bench.js
Expand Up @@ -63,7 +63,7 @@ function report(title, ob) {
vanilla: () => { for (let [k,v] of Object.entries(ob)) if (v == 1) return k },
},
KeyBy: {
objix: () => ({}.keyBy([{a:1},{a:2},{a:3}], 'a')),
objix: () => [{a:1},{a:2},{a:3}].keyBy('a'),
lodash: () => _.keyBy([{a:1},{a:2},{a:3}], 'a'),
},
Equals: {
Expand Down
11 changes: 6 additions & 5 deletions docs/api.md
Expand Up @@ -431,16 +431,17 @@ var o = { a: 1, b: 2 }.size() // 2

<a id="keyBy"></a>

## `Object..keyBy(array, key)`
## `Object..keyBy(path)`

Index an array of objects into `this` using the given key, and return `this`.
Re-Index values of this `this` using the given key path, and return `this`.

<div data-runkit>

```javascript
o = {}
o.keyBy([{ a: 'o1' }, { a: 'o2' }, { a: 'o2', b: 1 }], 'a')
var o = [{ a: 'o1' }, { a: 'o2' }, { a: 'o2', b: 1 }].keyBy('a')
o // { o1: { a: 'o1' }, o2: [{ a: 'o2', b: 1 }, { a: 'o2' }]
var o = [{ a: { b: { c:'o1' }}}, { a: { b: { c: 'o2' }}}].keyBy('a.b.c')
o // { o1: { a: { b: { c:'o1' }}}, o2: { a: { b: { c: 'o2' }}}}
```

</div>
Expand Down Expand Up @@ -491,7 +492,7 @@ setTimeout(() => o.nowish(), 1000) // 2022-10-17T00:01:01.565Z

## `Object..log(msg, test, type='log')`

Prints a shallow clone of `this` to the console together with a minute timestamp and an optional msg.
Prints `this.$()` to the console together with a minute timestamp and an optional msg.
If a `test` function is provided then logging will only be triggered if the test function returns truthy when called with with `this` as its first argument.
Alternative console methods such as 'trace', 'info', 'error' and 'debug' may also be specified. Returns `this`.

Expand Down
10 changes: 5 additions & 5 deletions objix.js
Expand Up @@ -94,13 +94,13 @@ const
return K(this).length
},

keyBy(a, k) {
a.map(o => this[o[k]] = this[o[k]] ? [o].concat(this[o[k]]) : o)
return this
keyBy(k, r={}, v) {
this.map(o => r[v=o.at(k)] = r[v] ? [o].concat(r[v]) : o)
return r
},

at(p) {
return p.split('.').reduce((v,c) => v[c], this)
return this[p] || p.split('.').reduce((v,c) => v[c], this)
},

$(s) {
Expand All @@ -119,7 +119,7 @@ const
},

log(m='', f, c='log') {
(!f || f(this)) && console[c](Date().slice(4,24),'-',m,this.clone())
(!f || f(this)) && console[c](Date().slice(4,24),'-',m,this.$())
return this
},

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.14.2",
"version": "1.15.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 01f18c7

Please sign in to comment.