Infinite Object (Io) - Version control tree using plain JavaScript and prototypical inheritance
Infinite Object minimal version: minimal.js
Minimal proof of concept api:
(function() {
"use strict";
function Io() {
this.tree = [];
}
Io.prototype.up = function up() {
var up = Object.create(this);
this.tree.push(up);
up.tree = [];
return up;
};
var root = new Io();
root.key = "value";
root.key2 = { objects: "work" };
var branch = root.up();
console.log(Object.getPrototypeOf(branch) === root);
console.log(root.isPrototypeOf(branch));
branch.key = "value2";
console.log(root.tree[0] === branch);
console.log(branch.key2 === root.key2);
console.log(branch.key !== root.key);
})();
Infinite Object small version: small_polyfilled.js
Still small but slightly more usable api with polyfill to work in ES3
var root = io();
root.branch({ testProp: "testValue4", anotherProp: "ECMAScript ftw" });
Infinite Object UMD (Universal Module Definition) / main (Node.js) / browser version: main.umd.js
Proper api, requires ES5
(function() {
var root = io();
var infiniteWhiteSpaceCollection = [root];
var branch1 = root.branch("testProp", "testValue");
var branch2 = root.branch("testProp", "testValue2");
var branch21 = branch2.branch({
testProp: "testValue4",
anotherProp: "ECMAScript ftw"
});
console.log(root.branches[0] === branch1);
console.log(branch1.version === 0);
console.log(branch1.version + 1 === branch2.version);
console.log(root.currentBranch === branch21.currentBranch);
console.log(root.currentBranch === branch21);
var branch11 = branch1.branch("testProp", "testValue3");
var branch211 = branch21.branch({ newProp: ":D" });
console.log(root.currentBranch === branch211);
console.log(root.root === root);
console.log(root === branch21.root);
function logger(branch) {
console.log(branch);
}
var off = root.on(logger);
root.currentBranch = Object.getPrototypeOf(branch211);
root.off(logger);
off();
var view = root.view;
var key;
for (key in view) {
console.log(key in branch21 && view[key] === branch21[key]);
}
for (key in branch21) {
console.log(
root.hasOwnProperty(key) || (key in view && view[key] === branch21[key])
);
}
console.log(branch11.date <= branch211.date);
var jsonIo = JSON.stringify(root);
var parsedIo = io.fromJSON(jsonIo);
var reStringified = JSON.stringify(parsedIo);
console.log("JSON idempotent: " + (reStringified === jsonIo));
var jsonArr = (document.getElementById("output").innerHTML = JSON.stringify(
infiniteWhiteSpaceCollection
));
var parsedArr = JSON.parse(jsonArr).map(io.fromParsedJSON);
console.log(parsedArr);
})();
Infinite Object ECMAScript 5 module / jsnext:main (deprecated package.json property, use module) version: main.es5.js
Proper api, requires ES5+
import io from 'infinite-object';
var root = io();
Infinite Object ECMAScript module es2015 version: main.js
Proper api, requires ES2015+ / ES6+ / ES latest, uses Proxy api for immutable branches/array behavior
import io from 'infinite-object';
const root = io();
const infiniteWhiteSpaceCollection = [root];
const branch1 = root.branch("testProp", "testValue");
const branch2 = root.branch("testProp", "testValue2");
const branch21 = branch2.branch({
testProp: "testValue4",
anotherProp: "ECMAScript ftw"
});
console.log(root.branches[0] === branch1);
console.log(branch1.version === 0);
console.log(branch1.version + 1 === branch2.version);
console.log(root.currentBranch === branch21.currentBranch);
console.log(root.currentBranch === branch21);
const branch11 = branch1.branch("testProp", "testValue3");
const branch211 = branch21.branch({ newProp: ":D" });
console.log(root.currentBranch === branch211);
console.log(root.root === root);
console.log(root === branch21.root);
function logger(branch) {
console.log(branch);
}
const off = root.on(logger);
root.currentBranch = Object.getPrototypeOf(branch211);
root.off(logger);
off();
const view = root.view;
let key;
for (key in view) {
console.log(key in branch21 && view[key] === branch21[key]);
}
for (key in branch21) {
console.log(
root.hasOwnProperty(key) || (key in view && view[key] === branch21[key])
);
}
console.log(branch11.date <= branch211.date);
const jsonIo = JSON.stringify(root);
const parsedIo = io.fromJSON(jsonIo);
const reStringified = JSON.stringify(parsedIo);
console.log("JSON idempotent: " + (reStringified === jsonIo));
const jsonArr = (document.getElementById("output").innerHTML = JSON.stringify(
infiniteWhiteSpaceCollection
));
const parsedArr = JSON.parse(jsonArr).map(io.fromParsedJSON);
console.log(parsedArr);