Skip to content

Commit

Permalink
Merge pull request #60 from kbuchanan/innerJoin
Browse files Browse the repository at this point in the history
Adding inner param in where function
  • Loading branch information
avoidwork authored Feb 27, 2019
2 parents 3ed03f4 + 7a00b8d commit 3cc843a
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,7 @@ store.batch(data, 'set').then(function () {
});
```

**where(predicate[, raw=false])**
**where(predicate[, raw=false, op="||"])**
_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
4 changes: 2 additions & 2 deletions lib/haro.js
Original file line number Diff line number Diff line change
Expand Up @@ -1069,14 +1069,14 @@
return obj;
}

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

return keys.length > 0 ? this.filter(new Function("a", `return (${keys.map(i => {
let result;
if (Array.isArray(predicate[i])) {
result = `Array.isArray(a['${i}']) ? ${predicate[i].map(arg => `a['${i}'].includes(${typeof arg === "string" ? `'${arg}'` : arg})`).join(" || ")} : a['${i}'] === '${predicate[i].join(",")}'`;
result = `Array.isArray(a['${i}']) ? ${predicate[i].map(arg => `a['${i}'].includes(${typeof arg === "string" ? `'${arg}'` : arg})`).join(` ${op} `)} : a['${i}'] === '${predicate[i].join(",")}'`;
} else if (predicate[i] instanceof RegExp) {
result = `Array.isArray(a['${i}']) ? a['${i}'].filter(i => ${predicate[i]}.test(a['${i}'])).length > 0 : ${predicate[i]}.test(a['${i}'])`;
} else {
Expand Down
2 changes: 1 addition & 1 deletion 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.

4 changes: 3 additions & 1 deletion package-lock.json

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

4 changes: 2 additions & 2 deletions src/haro.js
Original file line number Diff line number Diff line change
Expand Up @@ -745,14 +745,14 @@
return obj;
}

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

return keys.length > 0 ? this.filter(new Function("a", `return (${keys.map(i => {
let result;
if (Array.isArray(predicate[i])) {
result = `Array.isArray(a['${i}']) ? ${predicate[i].map(arg => `a['${i}'].includes(${typeof arg === "string" ? `'${arg}'` : arg})`).join(" || ")} : a['${i}'] === '${predicate[i].join(",")}'`;
result = `Array.isArray(a['${i}']) ? ${predicate[i].map(arg => `a['${i}'].includes(${typeof arg === "string" ? `'${arg}'` : arg})`).join(` ${op} `)} : a['${i}'] === '${predicate[i].join(",")}'`;
} else if (predicate[i] instanceof RegExp) {
result = `Array.isArray(a['${i}']) ? a['${i}'].filter(i => ${predicate[i]}.test(a['${i}'])).length > 0 : ${predicate[i]}.test(a['${i}'])`;
} else {
Expand Down
4 changes: 3 additions & 1 deletion test/offline.js
Original file line number Diff line number Diff line change
Expand Up @@ -668,10 +668,12 @@ exports.where = {
test: function (test) {
const self = this;

test.expect(4);
test.expect(6);
this.store.batch(data, "set").then(function () {
test.equal(self.store.where({company: "Insectus", tags: "occaecat"}).length, 1, "Should be '1'");
test.equal(self.store.where({company: "Insectus", tags: ["sunt", "aaaa"]}, false, "&&").length, 0, "Should be '0'");
test.equal(self.store.where({company: /insectus/i, tags: "occaecat"}).length, 1, "Should be '1'");
test.equal(self.store.where({tags: ["sunt", "veniam"]}, false, "&&").length, 1, "Should be '1'");
test.equal(self.store.where({company: "Insectus", tags: "aaaaa"}).length, 0, "Should be '0'");
test.equal(self.store.where({}).length, 0, "Should be '0'");
test.done();
Expand Down

0 comments on commit 3cc843a

Please sign in to comment.