Skip to content

Commit

Permalink
Creating sortBy(index) to sort by an indexed field, and lazily crea…
Browse files Browse the repository at this point in the history
…te said index at the same time
  • Loading branch information
avoidwork committed Jun 21, 2015
1 parent 3e84092 commit b6bd15e
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 60 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ _Array_

Returns an Array of the DataStore, sorted by `callbackFn`.

**sortBy( index )**
_Array_

Returns a `Tuple` of double `Tuples` with the shape `[key, value]` of records sorted by an index.

**toArray()**
_Array_

Expand Down
71 changes: 52 additions & 19 deletions lib/haro.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,18 @@ class Haro {
return tuple.apply(tuple, result);
}

reindex () {
this.indexes.clear();
this.index.forEach(i => {
this.indexes.set(i, new Map());
});
reindex (index) {
if (!index) {
this.indexes.clear();
this.index.forEach(i => {
this.indexes.set(i, new Map());
});
} else {
this.indexes.set(index, new Map());
}

this.forEach((key, value) => {
this.setIndex(key, value);
this.forEach((data, key) => {
this.setIndex(key, data, index);
});

return this;
Expand Down Expand Up @@ -325,19 +329,24 @@ class Haro {
return defer.promise;
}

setIndex (key, data) {
this.index.forEach(i => {
let index = this.indexes.get(i),
value = this.keyIndex(i, data);
setIndex (key, data, index) {
if (!index) {
this.index.forEach(i => {
this.setIndexValue(this.indexes.get(i), this.keyIndex(i, data), key);
});
} else {
this.setIndexValue(this.indexes.get(index), this.keyIndex(index, data), key);
}

if (!index.has(value)) {
index.set(value, new Set());
}
return this;
}

index.get(value).add(key);
});
setIndexValue (index, key, value) {
if (!index.has(key)) {
index.set(key, new Set());
}

return this;
index.get(key).add(value);
}

setUri (uri) {
Expand Down Expand Up @@ -378,11 +387,35 @@ class Haro {
return this.toArray().sort(fn);
}

sortBy (index) {
let result = [],
keys = [],
lindex;

if (!this.indexes.has(index)) {
this.index.push(index);
this.reindex(index);
}

lindex = this.indexes.get(index);
lindex.forEach((idx, key) => {
keys.push(key);
});

keys.sort().forEach(i => {
lindex.get(i).forEach(key => {
result.push(this.get(key));
});
});

return tuple.apply(tuple, result);
}

toArray () {
let result = [];

this.forEach(function (value) {
result.push(clone(value));
this.forEach(function (data) {
result.push(clone(data));
});

return result;
Expand Down
77 changes: 57 additions & 20 deletions lib/haro.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,16 +292,20 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
}
}, {
key: "reindex",
value: function reindex() {
value: function reindex(index) {
var _this6 = this;

this.indexes.clear();
this.index.forEach(function (i) {
_this6.indexes.set(i, new Map());
});
if (!index) {
this.indexes.clear();
this.index.forEach(function (i) {
_this6.indexes.set(i, new Map());
});
} else {
this.indexes.set(index, new Map());
}

this.forEach(function (key, value) {
_this6.setIndex(key, value);
this.forEach(function (data, key) {
_this6.setIndex(key, data, index);
});

return this;
Expand Down Expand Up @@ -379,22 +383,28 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
}
}, {
key: "setIndex",
value: function setIndex(key, data) {
value: function setIndex(key, data, index) {
var _this8 = this;

this.index.forEach(function (i) {
var index = _this8.indexes.get(i),
value = _this8.keyIndex(i, data);

if (!index.has(value)) {
index.set(value, new Set());
}

index.get(value).add(key);
});
if (!index) {
this.index.forEach(function (i) {
_this8.setIndexValue(_this8.indexes.get(i), _this8.keyIndex(i, data), key);
});
} else {
this.setIndexValue(this.indexes.get(index), this.keyIndex(index, data), key);
}

return this;
}
}, {
key: "setIndexValue",
value: function setIndexValue(index, key, value) {
if (!index.has(key)) {
index.set(key, new Set());
}

index.get(key).add(value);
}
}, {
key: "setUri",
value: function setUri(uri) {
Expand Down Expand Up @@ -437,13 +447,40 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
value: function sort(fn) {
return this.toArray().sort(fn);
}
}, {
key: "sortBy",
value: function sortBy(index) {
var _this10 = this;

var result = [],
keys = [],
lindex = undefined;

if (!this.indexes.has(index)) {
this.index.push(index);
this.reindex(index);
}

lindex = this.indexes.get(index);
lindex.forEach(function (idx, key) {
keys.push(key);
});

keys.sort().forEach(function (i) {
lindex.get(i).forEach(function (key) {
result.push(_this10.get(key));
});
});

return tuple.apply(tuple, result);
}
}, {
key: "toArray",
value: function toArray() {
var result = [];

this.forEach(function (value) {
result.push(clone(value));
this.forEach(function (data) {
result.push(clone(data));
});

return result;
Expand Down
2 changes: 1 addition & 1 deletion lib/haro.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 lib/haro.min.js.map

Large diffs are not rendered by default.

71 changes: 52 additions & 19 deletions src/haro.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,18 @@ class Haro {
return tuple.apply(tuple, result);
}

reindex () {
this.indexes.clear();
this.index.forEach(i => {
this.indexes.set(i, new Map());
});
reindex (index) {
if (!index) {
this.indexes.clear();
this.index.forEach(i => {
this.indexes.set(i, new Map());
});
} else {
this.indexes.set(index, new Map());
}

this.forEach((key, value) => {
this.setIndex(key, value);
this.forEach((data, key) => {
this.setIndex(key, data, index);
});

return this;
Expand Down Expand Up @@ -273,19 +277,24 @@ class Haro {
return defer.promise;
}

setIndex (key, data) {
this.index.forEach(i => {
let index = this.indexes.get(i),
value = this.keyIndex(i, data);
setIndex (key, data, index) {
if (!index) {
this.index.forEach(i => {
this.setIndexValue(this.indexes.get(i), this.keyIndex(i, data), key);
});
} else {
this.setIndexValue(this.indexes.get(index), this.keyIndex(index, data), key);
}

if (!index.has(value)) {
index.set(value, new Set());
}
return this;
}

index.get(value).add(key);
});
setIndexValue (index, key, value) {
if (!index.has(key)) {
index.set(key, new Set());
}

return this;
index.get(key).add(value);
}

setUri (uri) {
Expand Down Expand Up @@ -326,11 +335,35 @@ class Haro {
return this.toArray().sort(fn);
}

sortBy (index) {
let result = [],
keys = [],
lindex;

if (!this.indexes.has(index)) {
this.index.push(index);
this.reindex(index);
}

lindex = this.indexes.get(index);
lindex.forEach((idx, key) => {
keys.push(key);
});

keys.sort().forEach(i => {
lindex.get(i).forEach(key => {
result.push(this.get(key));
});
});

return tuple.apply(tuple, result);
}

toArray () {
let result = [];

this.forEach(function (value) {
result.push(clone(value));
this.forEach(function (data) {
result.push(clone(data));
});

return result;
Expand Down
19 changes: 19 additions & 0 deletions test/haro_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,25 @@ exports["read (sort)"] = {
}
};

exports["read (sortBy)"] = {
setUp: function (done) {
this.store = haro(null);
done();
},
test: function (test) {
var self = this;

test.expect(1);
this.store.batch(data, "set").then(function() {
test.equal(self.store.sortBy('company')[0][1].company, "Coash", "Should be `Coash`");
test.done();
}, function (e) {
console.log(e.stack);
test.done();
});
}
};

exports["update"] = {
setUp: function (done) {
this.store = haro();
Expand Down

0 comments on commit b6bd15e

Please sign in to comment.