Skip to content

Commit

Permalink
Changing the signature of toObjekt() to add a second `frozen = true…
Browse files Browse the repository at this point in the history
…` parameter which is passed from `this.toObject()`, updating `toObject()` to insure the return is frozen, updating tests, updating `batch()` to minimize unneeded `Object.freeze()`
  • Loading branch information
avoidwork committed Oct 24, 2016
1 parent 5c90bc7 commit 78be7cc
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 59 deletions.
50 changes: 31 additions & 19 deletions lib/haro.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @copyright 2016
* @license BSD-3-Clause
* @link https://github.com/avoidwork/haro
* @version 3.0.0
* @version 3.0.1
*/
"use strict";

Expand Down Expand Up @@ -322,13 +322,23 @@ function setIndex (index, indexes, delimiter, key, data, indice, pattern) {
}
}

function toObjekt (arg) {
function toObjekt (arg, frozen = true) {
let result = {};

arg.forEach((value, key) => {
result[key] = value;
let obj = clone(value);

if (frozen) {
Object.freeze(obj);
}

result[clone(key)] = obj;
});

if (frozen) {
Object.freeze(result);
}

return result;
}

Expand Down Expand Up @@ -399,7 +409,7 @@ class Haro {

if (this.patch) {
if (del) {
data = patch(this.toArray().map(i => {
data = patch(this.limit(0, this.total, true).map(i => {
return i[this.key];
}), args, this.key, true);
} else {
Expand All @@ -414,7 +424,7 @@ class Haro {
data.push({op: "add", path: "/", value: i});
}
});
data = data.concat(patch(this.toObject(), hash, this.key, true));
data = data.concat(patch(this.toObject(undefined, false), hash, this.key, true));
}

if (data.length > 0) {
Expand Down Expand Up @@ -1191,23 +1201,25 @@ class Haro {
}

toObject (data, frozen = true) {
let func;
let result;

if (frozen) {
func = arg => {
return arg;
};
} else {
func = arg => {
return clone(arg);
};
}
result = !data ? toObjekt(this, frozen) : data.reduce((a, b) => {
let obj = clone(b[1]);

return func(!data ? toObjekt(this) : data.reduce((a, b) => {
a[b[0]] = b[1];
if (frozen) {
Object.freeze(obj);
}

a[b[0]] = obj;

return a;
}, {}));
}, {});

if (frozen) {
Object.freeze(result);
}

return result;
}

transform (input, fn) {
Expand Down Expand Up @@ -1291,7 +1303,7 @@ function factory (data = null, config = {}, indexes = []) {
}

factory.transform = cast;
factory.version = "3.0.0";
factory.version = "3.0.1";

// Node, AMD & window supported
if (typeof exports !== "undefined") {
Expand Down
50 changes: 32 additions & 18 deletions lib/haro.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @copyright 2016
* @license BSD-3-Clause
* @link https://github.com/avoidwork/haro
* @version 3.0.0
* @version 3.0.1
*/
"use strict";

Expand Down Expand Up @@ -337,12 +337,24 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
}

function toObjekt(arg) {
var frozen = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;

var result = {};

arg.forEach(function (value, key) {
result[key] = value;
var obj = clone(value);

if (frozen) {
Object.freeze(obj);
}

result[clone(key)] = obj;
});

if (frozen) {
Object.freeze(result);
}

return result;
}

Expand Down Expand Up @@ -427,7 +439,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons

if (this.patch) {
if (del) {
data = patch(this.toArray().map(function (i) {
data = patch(this.limit(0, this.total, true).map(function (i) {
return i[_this2.key];
}), args, this.key, true);
} else {
Expand All @@ -442,7 +454,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
data.push({ op: "add", path: "/", value: i });
}
});
data = data.concat(patch(this.toObject(), hash, this.key, true));
data = data.concat(patch(this.toObject(undefined, false), hash, this.key, true));
}

if (data.length > 0) {
Expand Down Expand Up @@ -1345,23 +1357,25 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
value: function toObject(data) {
var frozen = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;

var func = void 0;
var result = void 0;

if (frozen) {
func = function func(arg) {
return arg;
};
} else {
func = function func(arg) {
return clone(arg);
};
}
result = !data ? toObjekt(this, frozen) : data.reduce(function (a, b) {
var obj = clone(b[1]);

return func(!data ? toObjekt(this) : data.reduce(function (a, b) {
a[b[0]] = b[1];
if (frozen) {
Object.freeze(obj);
}

a[b[0]] = obj;

return a;
}, {}));
}, {});

if (frozen) {
Object.freeze(result);
}

return result;
}
}, {
key: "transform",
Expand Down Expand Up @@ -1451,7 +1465,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
}

factory.transform = cast;
factory.version = "3.0.0";
factory.version = "3.0.1";

// 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": "3.0.0",
"version": "3.0.1",
"description": "Harō is a modern immutable DataStore using Maps, Sets, Promises, & Tuples",
"main": "lib/haro.js",
"scripts": {
Expand Down
32 changes: 17 additions & 15 deletions src/haro.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Haro {

if (this.patch) {
if (del) {
data = patch(this.toArray().map(i => {
data = patch(this.limit(0, this.total, true).map(i => {
return i[this.key];
}), args, this.key, true);
} else {
Expand All @@ -76,7 +76,7 @@ class Haro {
data.push({op: "add", path: "/", value: i});
}
});
data = data.concat(patch(this.toObject(), hash, this.key, true));
data = data.concat(patch(this.toObject(undefined, false), hash, this.key, true));
}

if (data.length > 0) {
Expand Down Expand Up @@ -853,23 +853,25 @@ class Haro {
}

toObject (data, frozen = true) {
let func;
let result;

if (frozen) {
func = arg => {
return arg;
};
} else {
func = arg => {
return clone(arg);
};
}
result = !data ? toObjekt(this, frozen) : data.reduce((a, b) => {
let obj = clone(b[1]);

return func(!data ? toObjekt(this) : data.reduce((a, b) => {
a[b[0]] = b[1];
if (frozen) {
Object.freeze(obj);
}

a[b[0]] = obj;

return a;
}, {}));
}, {});

if (frozen) {
Object.freeze(result);
}

return result;
}

transform (input, fn) {
Expand Down
14 changes: 12 additions & 2 deletions src/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,13 +292,23 @@ function setIndex (index, indexes, delimiter, key, data, indice, pattern) {
}
}

function toObjekt (arg) {
function toObjekt (arg, frozen = true) {
let result = {};

arg.forEach((value, key) => {
result[key] = value;
let obj = clone(value);

if (frozen) {
Object.freeze(obj);
}

result[clone(key)] = obj;
});

if (frozen) {
Object.freeze(result);
}

return result;
}

Expand Down
8 changes: 6 additions & 2 deletions test/haro_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,12 @@ exports["read (toArray)"] = {
test: function (test) {
var self = this;

test.expect(2);
test.expect(4);
this.store.batch(data, "set").then(function () {
test.equal(self.store.toArray().length, 6, "Should be `6`");
test.equal(self.store.toArray(self.store.limit(0, 5)).length, 5, "Should be `5`");
test.equal(Object.isFrozen(self.store.toArray(undefined)), true, "Should be `true`");
test.equal(Object.isFrozen(self.store.toArray(undefined, false)), false, "Should be `false`");
test.done();
}, function () {
test.done();
Expand All @@ -276,10 +278,12 @@ exports["read (toObject)"] = {
test: function (test) {
var self = this;

test.expect(2);
test.expect(4);
this.store.batch(data, "set").then(function () {
test.equal(self.store.toObject()["2a30000f-92dc-405c-b1e0-7c416d766b39"].isActive, false, "Should be `false`");
test.equal(self.store.toObject(self.store.limit(0, 5))["2a30000f-92dc-405c-b1e0-7c416d766b39"].isActive, false, "Should be `false`");
test.equal(Object.isFrozen(self.store.toObject()), true, "Should be `true`");
test.equal(Object.isFrozen(self.store.toObject(undefined, false)), false, "Should be `false`");
test.done();
}, function () {
test.done();
Expand Down

0 comments on commit 78be7cc

Please sign in to comment.