herm-js-document
Advanced tools
Comparing version 0.2.1 to 0.2.2
@@ -1,11 +0,10 @@ | ||
class HermDoc { | ||
constructor(initial) { | ||
constructor(initial) { | ||
this.keys = []; | ||
this.content = []; | ||
let i = parseInt(Date.now() + '' + parseInt(Math.random() * 10000)) + '' + parseInt(Math.random() * 10000); | ||
const i = `${parseInt(`${Date.now()}${parseInt(Math.random() * 10000, 10)}${parseInt(Math.random() * 10000, 10)}`, 10)}}`; | ||
this.keys[0] = i; | ||
this.content[i] = ''; | ||
if(initial) { | ||
this.push(0, initial) | ||
if (initial) { | ||
this.push(0, initial); | ||
} | ||
@@ -15,7 +14,5 @@ } | ||
push(index, value) { | ||
let docIndex = this.keys[index]; | ||
Array.from(value).forEach( (e,k) => { | ||
let i = parseInt(Date.now() + '' + parseInt(Math.random() * 10000)) + '' + parseInt(Math.random() * 10000); | ||
this.keys.splice(index+k,0,i); | ||
Array.from(value).forEach((e, k) => { | ||
const i = `${parseInt(`${Date.now()}${parseInt(Math.random() * 10000, 10)}${parseInt(Math.random() * 10000, 10)}`, 10)}}`; | ||
this.keys.splice(index + k, 0, i); | ||
this.content[i] = e; | ||
@@ -26,3 +23,3 @@ }); | ||
duplicate() { | ||
let t = new HermDoc(); | ||
const t = new HermDoc(); | ||
t.keys = Object.assign([], this.keys); | ||
@@ -34,7 +31,6 @@ t.content = Object.assign([], this.content); | ||
display() { | ||
let r = ""; | ||
let r = ''; | ||
this.keys.forEach((key) => { | ||
if(this.content[key]) | ||
r += this.content[key]; | ||
if (this.content[key]) { r += this.content[key]; } | ||
}); | ||
@@ -47,7 +43,6 @@ | ||
const keys = this.getKeys(); | ||
let r = ""; | ||
let r = ''; | ||
keys.forEach((key, index) => { | ||
if(index >= from && index < to && this.content[key]) | ||
r += this.content[key]; | ||
if (index >= from && index < to && this.content[key]) { r += this.content[key]; } | ||
}); | ||
@@ -59,24 +54,23 @@ | ||
indexOf(key) { | ||
return this.keys.indexOf(key); | ||
return this.keys.indexOf(key); | ||
} | ||
merge(newDoc) { | ||
let result = new HermDoc(); | ||
const result = new HermDoc(); | ||
let oldIndex = 0; | ||
let newIndex = 0; | ||
while(this.keys[oldIndex] || newDoc.keys[newIndex]) { | ||
if(this.keys[oldIndex] === newDoc.keys[newIndex]) { | ||
while (this.keys[oldIndex] || newDoc.keys[newIndex]) { | ||
if (this.keys[oldIndex] === newDoc.keys[newIndex]) { | ||
result.keys.push(this.keys[oldIndex]); | ||
oldIndex++; | ||
newIndex++; | ||
} | ||
else { | ||
while(this.keys[oldIndex] && newDoc.keys.indexOf(this.keys[oldIndex]) === -1) { | ||
oldIndex += 1; | ||
newIndex += 1; | ||
} else { | ||
while (this.keys[oldIndex] && newDoc.keys.indexOf(this.keys[oldIndex]) === -1) { | ||
result.keys.push(this.keys[oldIndex]); | ||
oldIndex++; | ||
oldIndex += 1; | ||
} | ||
while(newDoc.keys[newIndex] && this.keys.indexOf(newDoc.keys[newIndex]) === -1) { | ||
while (newDoc.keys[newIndex] && this.keys.indexOf(newDoc.keys[newIndex]) === -1) { | ||
result.keys.push(newDoc.keys[newIndex]); | ||
newIndex++; | ||
newIndex += 1; | ||
} | ||
@@ -99,4 +93,3 @@ } | ||
newDoc.keys.forEach((e) => { | ||
if(result.content[e] !== null) | ||
result.content[e] = newDoc.content[e]; | ||
if (result.content[e] !== null) { result.content[e] = newDoc.content[e]; } | ||
}); | ||
@@ -113,3 +106,3 @@ | ||
const keys = this.getKeys(); | ||
for(let i = 0; i < length; i++) { | ||
for (let i = 0; i < length; i += 1) { | ||
const k = keys[from + i]; | ||
@@ -121,2 +114,2 @@ this.content[k] = null; | ||
export default HermDoc; | ||
export default HermDoc; |
import HermDoc from './index'; | ||
const toJsonAtt = (prop, v) => { | ||
return JSON.stringify({[prop]: v}).replace(/^{/, '').replace(/}$/, '')+','; | ||
} | ||
const toJsonAtt = (prop, v) => `${JSON.stringify({ [prop]: v }).replace(/^{/, '').replace(/}$/, '')},`; | ||
class HermJsonDoc { | ||
constructor() { | ||
return new Proxy({ | ||
_data : new HermDoc('{}'), | ||
_index : [], | ||
display : function () { | ||
return JSON.parse(this._data.display().replace(/,}$/, '}')); | ||
}, | ||
merge : function(doc) { | ||
const temp = new HermJsonDoc(); | ||
temp._data = this._data.duplicate(); | ||
temp._data = temp._data.merge(doc._data); | ||
temp._index = Object.assign([], this._index); | ||
return temp; | ||
}, | ||
duplicate : function() { | ||
const temp = new HermJsonDoc(); | ||
temp._data = this._data.duplicate(); | ||
temp._index = Object.assign([], this._index); | ||
return temp; | ||
constructor() { | ||
return new Proxy({ | ||
data: new HermDoc('{}'), | ||
index: [], | ||
display() { | ||
return JSON.parse(this.data.display().replace(/,}$/, '}')); | ||
}, | ||
merge(doc) { | ||
const temp = new HermJsonDoc(); | ||
temp.data = this.data.duplicate(); | ||
temp.data = temp.data.merge(doc.data); | ||
temp.index = Object.assign([], this.index); | ||
return temp; | ||
}, | ||
duplicate() { | ||
const temp = new HermJsonDoc(); | ||
temp.data = this.data.duplicate(); | ||
temp.index = Object.assign([], this.index); | ||
return temp; | ||
}, | ||
}, { | ||
get(obj, prop) { | ||
if (prop in obj) { return obj[prop]; } | ||
const temp = JSON.parse(`{${obj.data.displayRange(obj.data.indexOf(obj.index[prop].pos), obj.index[prop].len)}}`); | ||
return temp[prop]; | ||
}, | ||
/* eslint-disable no-param-reassign */ | ||
set(obj, prop, value) { | ||
if (prop in obj) { | ||
obj[prop] = value; | ||
return true; | ||
} | ||
}, { | ||
get: function(obj, prop) { | ||
if(prop in obj) | ||
return obj[prop] | ||
else { | ||
let temp = JSON.parse('{' + obj._data.displayRange(obj._data.indexOf(obj._index[prop].pos), obj._index[prop].len) + '}'); | ||
return temp[prop]; | ||
} | ||
}, | ||
set: function(obj, prop, value) { | ||
if(prop in obj ) { | ||
obj[prop] = value; | ||
return true; | ||
} | ||
else { | ||
value = toJsonAtt(prop, value); | ||
if(!obj._index[prop]) { | ||
obj._index[prop] = { | ||
pos: obj._data.keys.length - 2, | ||
len: value.length | ||
}; | ||
const stringValue = toJsonAtt(prop, value); | ||
obj._data.push(obj._data.keys.length - 2, value); | ||
obj._index[prop].pos = obj._data.keys[obj._index[prop].pos]; | ||
} | ||
else { | ||
const realIndex = obj._data.keys.indexOf(obj._index[prop].pos); | ||
obj._data.deleteRange(realIndex, obj._index[prop].len); | ||
obj._index[prop].len = value.length; | ||
obj._data.push(realIndex, value); | ||
obj._index[prop].pos = obj._data.keys[realIndex]; | ||
} | ||
return true; | ||
} | ||
if (!obj.index[prop]) { | ||
obj.index[prop] = { | ||
pos: obj.data.keys.length - 2, | ||
len: stringValue.length, | ||
}; | ||
obj.data.push(obj.data.keys.length - 2, stringValue); | ||
obj.index[prop].pos = obj.data.keys[obj.index[prop].pos]; | ||
} else { | ||
const realIndex = obj.data.keys.indexOf(obj.index[prop].pos); | ||
obj.data.deleteRange(realIndex, obj.index[prop].len); | ||
obj.index[prop].len = stringValue.length; | ||
obj.data.push(realIndex, stringValue); | ||
obj.index[prop].pos = obj.data.keys[realIndex]; | ||
} | ||
return true; | ||
}, | ||
/* eslint-enable no-param-reassign */ | ||
}); | ||
@@ -69,2 +64,2 @@ } | ||
export default HermJsonDoc; | ||
export default HermJsonDoc; |
{ | ||
"name": "herm-js-document", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"description": "Herm-js document system for offline sync, and auto conflict resolution.", | ||
@@ -9,2 +9,3 @@ "main": "modules/index.js", | ||
"test": "jest", | ||
"lint": "eslint --fix ./modules", | ||
"build": "webpack -p" | ||
@@ -11,0 +12,0 @@ }, |
[](https://circleci.com/gh/azukaar/herm-js/tree/master) [](https://coveralls.io/github/azukaar/herm-js?branch=master) | ||
[](https://circleci.com/gh/azukaar/herm-js/tree/master) [](https://coveralls.io/github/azukaar/herm-js?branch=master) [](https://www.npmjs.com/package/eslint-config-airbnb-base) | ||
@@ -4,0 +4,0 @@ # Herm document system |
145