Skip to content

Commit

Permalink
Creating reduce(), refactoring filter() to use reduce(), fixing…
Browse files Browse the repository at this point in the history
… a type assumption in `where()`'s meta function
  • Loading branch information
avoidwork committed Apr 3, 2018
1 parent 69a0da4 commit 24888cf
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 24 deletions.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,25 @@ store.override({'field': {'value': ['pk']}}, "indexes").then(function () {
});
```

**reduce(accumulator, value[, key, ctx=this, raw=false])**
_Array_

Runs an `Array.reduce()` inspired function against the data store (`Map`).

Example of filtering a DataStore:
```javascript
const store = haro();

// Data is added

store.reduce(function (accumulator, value, key) {
accumulator[key] = value;

return accumulator;
}, {});
```


**reindex([index])**
_Haro_

Expand Down Expand Up @@ -1013,7 +1032,7 @@ store.batch(data, 'set').then(function () {
});
```

**where(predicate)**
**where(predicate[, raw=false])**
_Array_

Ideal for when dealing with a composite index which contains an `Array` of values, which would make matching on a single value impossible when using `find()`.
Expand Down
34 changes: 24 additions & 10 deletions lib/haro.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @author Jason Mulligan <jason.mulligan@avoidwork.com>
* @copyright 2018
* @license BSD-3-Clause
* @version 4.2.6
* @version 4.3.0
*/
"use strict";

Expand Down Expand Up @@ -506,13 +506,13 @@
}

filter (fn, raw = false) {
const result = [];

this.forEach((value, key) => {
if (fn(value, key) === true) {
result.push(this.get(key, raw));
const result = this.reduce((a, v, k, ctx) => {
if (fn.call(ctx, v) === true) {
a.push(this.get(k, raw));
}
}, this);

return a;
}, []);

return raw ? result : this.list(...result);
}
Expand Down Expand Up @@ -669,6 +669,16 @@
return result;
}

reduce (fn, accumulator, raw = false) {
let a = accumulator || this.data.keys().next().value;

this.forEach((v, k) => {
a = fn(a, v, k, this, raw);
}, this);

return a;
}

register (key, fn) {
adapter[key] = fn;

Expand Down Expand Up @@ -1049,10 +1059,14 @@
return obj;
}

where (predicate) {
where (predicate, raw = false) {
const keys = this.index.filter(i => i in predicate);

return keys.length > 0 ? this.filter(new Function("a", `return (${keys.map(i => `Array.isArray(a['${i}']) ? a['${i}'].includes('${predicate[i]}') : a['${i}'] === '${predicate[i]}'`).join(") && (")});`), true) : [];
return keys.length > 0 ? this.filter(new Function("a", `return (${keys.map(i => {
const arg = typeof predicate[i] === "string" ? `'${predicate[i]}'` : predicate[i];
return `Array.isArray(a['${i}']) ? a['${i}'].includes(${arg}) : a['${i}'] === ${arg}`;
}).join(") && (")});`), raw) : [];
}
}

Expand All @@ -1071,7 +1085,7 @@
}

factory.transform = cast;
factory.version = "4.2.6";
factory.version = "4.3.0";

// Node, AMD & window supported
if (typeof exports !== "undefined") {
Expand Down
4 changes: 2 additions & 2 deletions lib/haro.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/haro.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package-lock.json

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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "haro",
"version": "4.2.6",
"version": "4.3.0",
"description": "Harō is a modern immutable DataStore",
"main": "lib/haro.js",
"scripts": {
Expand Down
30 changes: 22 additions & 8 deletions src/haro.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,13 @@
}

filter (fn, raw = false) {
const result = [];

this.forEach((value, key) => {
if (fn(value, key) === true) {
result.push(this.get(key, raw));
const result = this.reduce((a, v, k, ctx) => {
if (fn.call(ctx, v) === true) {
a.push(this.get(k, raw));
}
}, this);

return a;
}, []);

return raw ? result : this.list(...result);
}
Expand Down Expand Up @@ -354,6 +354,16 @@
return result;
}

reduce (fn, accumulator, raw = false) {
let a = accumulator || this.data.keys().next().value;

this.forEach((v, k) => {
a = fn(a, v, k, this, raw);
}, this);

return a;
}

register (key, fn) {
adapter[key] = fn;

Expand Down Expand Up @@ -734,9 +744,13 @@
return obj;
}

where (predicate) {
where (predicate, raw = false) {
const keys = this.index.filter(i => i in predicate);

return keys.length > 0 ? this.filter(new Function("a", `return (${keys.map(i => `Array.isArray(a['${i}']) ? a['${i}'].includes('${predicate[i]}') : a['${i}'] === '${predicate[i]}'`).join(") && (")});`), true) : [];
return keys.length > 0 ? this.filter(new Function("a", `return (${keys.map(i => {
const arg = typeof predicate[i] === "string" ? `'${predicate[i]}'` : predicate[i];
return `Array.isArray(a['${i}']) ? a['${i}'].includes(${arg}) : a['${i}'] === ${arg}`;
}).join(") && (")});`), raw) : [];
}
}

0 comments on commit 24888cf

Please sign in to comment.