Skip to content

Commit

Permalink
Refactoring search() to support an Array for index & default to…
Browse files Browse the repository at this point in the history
… `this.index` when not supplied

Refactoring `search()` to support an `Array` for `index` & default to `this.index` when not supplied`
  • Loading branch information
avoidwork committed Jul 12, 2015
1 parent 2142777 commit 843fc44
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 22 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -566,12 +566,13 @@ _Promise_

Saves the DataStore to persistent storage.

**search( arg, index )**
**search( arg[, index=this.index] )**
_Tuple_

Returns a `Tuple` of double `Tuples` with the shape `[key, value]` of records found matching `arg`.
If `arg` is a `Function` a match is made if the result is `true`, if `arg` is a `RegExp` the field value must `.test()`
as `true`, else the value must be an equality match.
as `true`, else the value must be an equality match. The `index` parameter can be a `String` or `Array` of `Strings`;
if not supplied it defaults to `this.index`.

Example of searching with a predicate function:
```javascript
Expand Down
20 changes: 14 additions & 6 deletions lib/haro.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @copyright 2015
* @license BSD-3-Clause
* @link http://haro.rocks
* @version 1.4.10
* @version 1.4.11
*/
"use strict";

Expand Down Expand Up @@ -556,13 +556,21 @@ class Haro {
}

search (value, index) {
let indexes = index ? (this.index.indexOf(index) > -1 ? [index] : []) : this.index,
result = [],
let result = [],
fn = typeof value === "function",
rgex = typeof value.test === "function",
seen = new Set();
rgex = value && typeof value.test === "function",
seen = new Set(),
lindex, indexes;

if (value) {
lindex = clone(index || this.index);

if (lindex instanceof Array) {
indexes = lindex;
} else if (typeof lindex === "string") {
indexes = [lindex];
}

indexes.forEach(i => {
let idx = this.indexes.get(i);

Expand Down Expand Up @@ -877,7 +885,7 @@ function factory (data = null, config = {}, indexes = []) {
return new Haro(data, config, indexes);
}

factory.version = "1.4.10";
factory.version = "1.4.11";

// Node, AMD & window supported
if (typeof exports !== "undefined") {
Expand Down
21 changes: 15 additions & 6 deletions lib/haro.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @copyright 2015
* @license BSD-3-Clause
* @link http://haro.rocks
* @version 1.4.10
* @version 1.4.11
*/
"use strict";

Expand Down Expand Up @@ -625,13 +625,22 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
value: function search(value, index) {
var _this8 = this;

var indexes = index ? this.index.indexOf(index) > -1 ? [index] : [] : this.index,
result = [],
var result = [],
fn = typeof value === "function",
rgex = typeof value.test === "function",
seen = new Set();
rgex = value && typeof value.test === "function",
seen = new Set(),
lindex = undefined,
indexes = undefined;

if (value) {
lindex = clone(index || this.index);

if (lindex instanceof Array) {
indexes = lindex;
} else if (typeof lindex === "string") {
indexes = [lindex];
}

indexes.forEach(function (i) {
var idx = _this8.indexes.get(i);

Expand Down Expand Up @@ -993,7 +1002,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
return new Haro(data, config, indexes);
}

factory.version = "1.4.10";
factory.version = "1.4.11";

// Node, AMD & window supported
if (typeof exports !== "undefined") {
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.

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": "1.4.10",
"version": "1.4.11",
"description": "Harō is a modern immutable DataStore using Maps, Sets, Promises, & Tuples",
"main": "lib/haro.js",
"scripts": {
Expand Down
16 changes: 12 additions & 4 deletions src/haro.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,13 +390,21 @@ class Haro {
}

search (value, index) {
let indexes = index ? (this.index.indexOf(index) > -1 ? [index] : []) : this.index,
result = [],
let result = [],
fn = typeof value === "function",
rgex = typeof value.test === "function",
seen = new Set();
rgex = value && typeof value.test === "function",
seen = new Set(),
lindex, indexes;

if (value) {
lindex = clone(index || this.index);

if (lindex instanceof Array) {
indexes = lindex;
} else if (typeof lindex === "string") {
indexes = [lindex];
}

indexes.forEach(i => {
let idx = this.indexes.get(i);

Expand Down
2 changes: 1 addition & 1 deletion test/haro_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ exports["read (search - indexed)"] = {

test.expect(4);
this.store.batch(data, "set").then(function () {
var result1 = self.store.search(/.*de.*/i, 'name');
var result1 = self.store.search(/.*de.*/i);
var result2 = self.store.search(20, 'age');

test.equal(result1.length, "1", "Should be `1`");
Expand Down

0 comments on commit 843fc44

Please sign in to comment.