Skip to content

Commit

Permalink
Creating size property to mimic the underlying Map better & updat…
Browse files Browse the repository at this point in the history
…ing tests
  • Loading branch information
avoidwork committed Feb 22, 2018
1 parent 290534b commit 54f788c
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 39 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,11 @@ _Array_

Array representing the order of `this.data`.

**size**
_Number_

Total records in the DataStore.

**total**
_Number_

Expand Down
12 changes: 7 additions & 5 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.0.4
* @version 4.1.0
*/
"use strict";

Expand Down Expand Up @@ -339,7 +339,7 @@
this.patch = patch;
this.pattern = pattern;
this.source = source;
this.total = 0;
this.size = this.total = 0;
this.uri = "";
this.worker = null;
this.versions = new Map();
Expand Down Expand Up @@ -391,7 +391,7 @@

clear () {
this.beforeClear();
this.total = 0;
this.size = this.total = 0;
this.data.clear();
this.indexes.clear();
this.versions.clear();
Expand Down Expand Up @@ -431,6 +431,7 @@
delIndex(this.index, this.indexes, this.delimiter, key, og, this.pattern);
this.data.delete(key);
--this.total;
this.size = this.total;
}, async () => {
this.ondelete(key, batch, retry, lazyLoad);

Expand Down Expand Up @@ -663,7 +664,7 @@
this.data.clear();
this.indexes.clear();
each(data, datum => this.data.set(this.key ? datum[this.key] : uuid() || uuid(), datum));
this.total = this.data.size;
this.size = this.total = this.data.size;
} else {
throw new Error("Invalid type");
}
Expand Down Expand Up @@ -788,6 +789,7 @@

if (!this.data.has(key)) {
++this.total;
this.size = this.total;
method = "post";

if (this.versioning) {
Expand Down Expand Up @@ -1066,7 +1068,7 @@
}

factory.transform = cast;
factory.version = "4.0.4";
factory.version = "4.1.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.0.4",
"version": "4.1.0",
"description": "Harō is a modern immutable DataStore",
"main": "lib/haro.js",
"scripts": {
Expand Down
8 changes: 5 additions & 3 deletions src/haro.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
this.patch = patch;
this.pattern = pattern;
this.source = source;
this.total = 0;
this.size = this.total = 0;
this.uri = "";
this.worker = null;
this.versions = new Map();
Expand Down Expand Up @@ -73,7 +73,7 @@

clear () {
this.beforeClear();
this.total = 0;
this.size = this.total = 0;
this.data.clear();
this.indexes.clear();
this.versions.clear();
Expand Down Expand Up @@ -113,6 +113,7 @@
delIndex(this.index, this.indexes, this.delimiter, key, og, this.pattern);
this.data.delete(key);
--this.total;
this.size = this.total;
}, async () => {
this.ondelete(key, batch, retry, lazyLoad);

Expand Down Expand Up @@ -345,7 +346,7 @@
this.data.clear();
this.indexes.clear();
each(data, datum => this.data.set(this.key ? datum[this.key] : uuid() || uuid(), datum));
this.total = this.data.size;
this.size = this.total = this.data.size;
} else {
throw new Error("Invalid type");
}
Expand Down Expand Up @@ -470,6 +471,7 @@

if (!this.data.has(key)) {
++this.total;
this.size = this.total;
method = "post";

if (this.versioning) {
Expand Down
31 changes: 22 additions & 9 deletions test/offline.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ exports.empty = {
done();
},
test: function (test) {
test.expect(2);
test.expect(3);
test.equal(this.store.total, 0, "Should be '0'");
test.equal(this.store.size, 0, "Should be '0'");
test.equal(this.store.data.size, 0, "Should be '0'");
test.done();
}
Expand All @@ -23,11 +24,13 @@ exports.create = {
test: function (test) {
var self = this;

test.expect(4);
test.expect(6);
test.equal(this.store.total, 0, "Should be '0'");
test.equal(this.store.size, 0, "Should be '0'");
test.equal(this.store.data.size, 0, "Should be '0'");
this.store.set(null, data[0]).then(function () {
test.equal(self.store.total, 1, "Should be '1'");
test.equal(self.store.size, 1, "Should be '1'");
test.equal(self.store.data.size, 1, "Should be '1'");
test.done();
}, function () {
Expand All @@ -44,11 +47,13 @@ exports["create (batch)"] = {
test: function (test) {
var self = this;

test.expect(11);
test.expect(13);
test.equal(this.store.total, 0, "Should be '0'");
test.equal(this.store.size, 0, "Should be '0'");
test.equal(this.store.data.size, 0, "Should be '0'");
this.store.batch(data, "set").then(function () {
test.equal(self.store.total, 6, "Should be '6'");
test.equal(self.store.size, 6, "Should be '6'");
test.equal(self.store.data.size, 6, "Should be '6'");
test.equal(self.store.registry.length, 6, "Should be '6'");
test.equal(self.store.limit(0, 2)[1][0], self.store.get(self.store.registry[1])[0], "Should be a match");
Expand Down Expand Up @@ -82,11 +87,13 @@ exports["update (batch)"] = {
test: function (test) {
var self = this;

test.expect(14);
test.expect(17);
test.equal(this.store.total, 0, "Should be '0'");
test.equal(this.store.size, 0, "Should be '0'");
test.equal(this.store.data.size, 0, "Should be '0'");
this.store.batch(data, "set").then(function () {
test.equal(self.store.total, 6, "Should be '6'");
test.equal(self.store.size, 6, "Should be '6'");
test.equal(self.store.data.size, 6, "Should be '6'");
test.equal(self.store.registry.length, 6, "Should be '6'");

Expand All @@ -95,6 +102,7 @@ exports["update (batch)"] = {
throw e;
}).then(function () {
test.equal(self.store.total, 6, "Should be '6'");
test.equal(self.store.size, 6, "Should be '6'");
test.equal(self.store.data.size, 6, "Should be '6'");
test.equal(self.store.registry.length, 6, "Should be '6'");
test.equal(self.store.limit(0, 2)[1][0], self.store.get(self.store.registry[1])[0], "Should be a match");
Expand Down Expand Up @@ -129,11 +137,12 @@ exports["read (valid)"] = {
test: function (test) {
var self = this;

test.expect(4);
test.expect(5);
this.store.set(null, data[0]).then(function (arg) {
var record = self.store.get(arg[0]);

test.equal(self.store.total, 1, "Should be '1'");
test.equal(self.store.size, 1, "Should be '1'");
test.equal(self.store.data.size, 1, "Should be '1'");
test.equal(Object.keys(record[1]).length, 19, "Should be a '19'");
test.equal(record[1].name, "Decker Merrill", "Should be a match");
Expand All @@ -152,9 +161,10 @@ exports["read (invalid)"] = {
test: function (test) {
var self = this;

test.expect(3);
test.expect(4);
this.store.set(null, data[0]).then(function () {
test.equal(self.store.total, 1, "Should be '1'");
test.equal(self.store.size, 1, "Should be '1'");
test.equal(self.store.data.size, 1, "Should be '1'");
test.equal(self.store.get("abc"), undefined, "Should be 'undefined'");
test.done();
Expand Down Expand Up @@ -466,7 +476,7 @@ exports.delete = {
test: function (test) {
var self = this;

test.expect(3);
test.expect(4);
this.store.set(null, data[0]).then(function (arg) {
test.equal(arg[1].name, "Decker Merrill", "Should be a match");

Expand All @@ -477,6 +487,7 @@ exports.delete = {
throw e;
}).then(function () {
test.equal(self.store.total, 0, "Should be '0'");
test.equal(self.store.size, 0, "Should be '0'");
test.equal(self.store.data.size, 0, "Should be '0'");
test.done();
}, function () {
Expand All @@ -493,7 +504,7 @@ exports["delete (batch)"] = {
test: function (test) {
var self = this;

test.expect(3);
test.expect(4);
this.store.batch(data, "set").then(function (arg) {
test.equal(arg[0][1].name, "Decker Merrill", "Should be a match");

Expand All @@ -504,6 +515,7 @@ exports["delete (batch)"] = {
throw e;
}).then(function () {
test.equal(self.store.total, 4, "Should be '4'");
test.equal(self.store.size, 4, "Should be '4'");
test.equal(self.store.data.size, 4, "Should be '4'");
test.done();
}, function () {
Expand Down Expand Up @@ -635,9 +647,10 @@ exports["override (records)"] = {
test: function (test) {
var self = this;

test.expect(3);
test.expect(4);
this.store.override(data, "records").then(function () {
test.equal(self.store.total, 6, "Should be a '6'");
test.equal(self.store.size, 6, "Should be a '6'");
test.equal(self.store.registry.length, 6, "Should be a '6'");
test.equal(self.store.data.size, 6, "Should be a '6'");
test.done();
Expand Down
30 changes: 13 additions & 17 deletions test/online.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const path = require("path"),
haro = require(path.join(__dirname, "..", "lib", "haro")),
data = require(path.join(__dirname, "data.json"));

process.env.NODE_NO_WARNINGS = 1;

require("tenso")({
security: {
csrf: false
Expand All @@ -11,27 +13,17 @@ require("tenso")({
},
routes: {
get: {
"/": function (req, res) {
res.send(["data"]);
},
"/data.*": function (req, res) {
res.send(data);
}
"/": ["data"],
"/data(/)?.*": (req, res) => res.send(data)
},
put: {
"/data.*": function (req, res) {
res.send(req.body);
}
"/data(/)?.*": (req, res) => res.send(req.body)
},
post: {
"/data.*": function (req, res) {
res.send(req.body, 201);
}
"/data(/)?.*": (req, res) => res.send(req.body, 201)
},
"delete": {
"/data.*": function (req, res) {
res.send({success: true});
}
"/data(/)?.*": (req, res) => res.send({success: true})
}
}
});
Expand All @@ -44,12 +36,14 @@ exports.setUri = {
test: function (test) {
var self = this;

test.expect(5);
test.expect(7);
test.equal(this.store.total, 0, "Should be '0'");
test.equal(this.store.size, 0, "Should be '0'");
test.equal(this.store.data.size, 0, "Should be '0'");
this.store.setUri("http://localhost:8000/data?page_size=10").then(function (args) {
test.equal(args.length, 6, "Should be '6'");
test.equal(self.store.total, 6, "Should be '6'");
test.equal(self.store.size, 6, "Should be '6'");
test.equal(self.store.data.size, 6, "Should be '6'");
test.done();
}, function () {
Expand Down Expand Up @@ -90,15 +84,17 @@ exports["delete (wired)"] = {
test: function (test) {
var self = this;

test.expect(3);
test.expect(5);
this.store.setUri("http://localhost:8000/data/?page_size=10").then(function (args) {
test.equal(self.store.total, 6, "Should be a match");
test.equal(self.store.size, 6, "Should be a match");

return self.store.del(args[0][0]);
}, function (e) {
throw e;
}).then(function () {
test.equal(self.store.total, self.store.data.size, "Should be a match");
test.equal(self.store.size, self.store.data.size, "Should be a match");
test.equal(self.store.data.size, 5, "Should be a match");
test.done();
}, function () {
Expand Down

0 comments on commit 54f788c

Please sign in to comment.