Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make immutable #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
226 changes: 150 additions & 76 deletions packages/herm-js-document/modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,111 +17,185 @@ function ranID(value) {
return text;
}

function indexOf(doc, element) {
return doc.keys.indexOf(element);
}

class HermDoc {
constructor(initial) {
this.keys = [];
this.content = [];
const i = ranID('');
this.keys[0] = i;
this.content[i] = '';
if (initial) {
this.push(0, initial);
}
}
function getKeys(doc) {
return doc.keys.filter(e => doc.content[e] !== null);
}

push(index, value) {
Array.from(value).forEach((e, k) => {
const i = ranID(e);
this.keys.splice(index + k, 0, i);
this.content[i] = e;
});
function duplicate(doc) {
if (doc.duplicate) {
return doc.duplicate();
}

duplicate() {
const t = new HermDoc();
t.keys = Object.assign([], this.keys);
t.content = Object.assign([], this.content);
return t;
}
return {
keys: Object.assign([], doc.keys),
content: Object.assign([], doc.content),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[]?

};
}

display() {
let r = '';
function display(doc) {
let r = '';

this.keys.forEach((key) => {
if (this.content[key]) { r += this.content[key]; }
});
doc.keys.forEach((key) => {
if (doc.content[key]) { r += doc.content[key]; }
});

return r;
}
return r;
}

displayRange(from, to) {
const keys = this.getKeys();
let r = '';
function deleteRange(doc, from, length) {
const nextDoc = duplicate(doc);
const keys = getKeys(doc);
for (let i = 0; i < length; i += 1) {
const k = keys[from + i];
nextDoc.content[k] = null;
}
return nextDoc;
}

keys.forEach((key, index) => {
if (index >= from && index < to && this.content[key]) { r += this.content[key]; }
});
function displayRange(doc, from, to) {
const keys = getKeys(doc);
let r = '';

return r;
}
keys.forEach((key, index) => {
if (index >= from && index < to && doc.content[key]) { r += doc.content[key]; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe for (let index = from; index < to; index++)

});

indexOf(key) {
return this.keys.indexOf(key);
}
return r;
}

merge(newDoc) {
const result = new HermDoc();
let oldIndex = 0;
let newIndex = 0;
function push(doc, index, value) {
const nextDoc = duplicate(doc);
Array.from(value).forEach((e, k) => {
const i = ranID(e);
nextDoc.keys.splice(index + k, 0, i);
nextDoc.content[i] = e;
});
return nextDoc;
}

while (this.keys[oldIndex] || newDoc.keys[newIndex]) {
if (this.keys[oldIndex] === newDoc.keys[newIndex]) {
result.keys.push(this.keys[oldIndex]);
function merge(doc, newDoc) {
const result = {
keys: [],
content: [],
};
let oldIndex = 0;
let newIndex = 0;

while (doc.keys[oldIndex] || newDoc.keys[newIndex]) {
if (doc.keys[oldIndex] === newDoc.keys[newIndex]) {
result.keys.push(doc.keys[oldIndex]);
oldIndex += 1;
newIndex += 1;
} else {
while (doc.keys[oldIndex] && newDoc.keys.indexOf(doc.keys[oldIndex]) === -1) {
result.keys.push(doc.keys[oldIndex]);
oldIndex += 1;
}
while (newDoc.keys[newIndex] && doc.keys.indexOf(newDoc.keys[newIndex]) === -1) {
result.keys.push(newDoc.keys[newIndex]);
newIndex += 1;
} else {
while (this.keys[oldIndex] && newDoc.keys.indexOf(this.keys[oldIndex]) === -1) {
result.keys.push(this.keys[oldIndex]);
oldIndex += 1;
}
while (newDoc.keys[newIndex] && this.keys.indexOf(newDoc.keys[newIndex]) === -1) {
result.keys.push(newDoc.keys[newIndex]);
newIndex += 1;
}
}
}

if (!this.keys[oldIndex]) {
result.keys = result.keys.concat(newDoc.keys.slice(newIndex));
break;
}
if (!doc.keys[oldIndex]) {
result.keys = result.keys.concat(newDoc.keys.slice(newIndex));
break;
}

if (!newDoc.keys[newIndex]) {
result.keys = result.keys.concat(this.keys.slice(oldIndex));
break;
}
if (!newDoc.keys[newIndex]) {
result.keys = result.keys.concat(doc.keys.slice(oldIndex));
break;
}
}

result.content = Object.assign([], this.content);
result.content = Object.assign([], doc.content);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is Object.assign([])? You can copy array with .slice(), like [].slice().


newDoc.keys.forEach((e) => {
if (result.content[e] !== null) { result.content[e] = newDoc.content[e]; }
});
newDoc.keys.forEach((e) => {
if (result.content[e] !== null) { result.content[e] = newDoc.content[e]; }
});

return result;
return result;
}

function newHermDoc(initial) {
let result = {
keys: [],
content: [],
};

const i = ranID('');
result.keys[0] = i;
result.content[i] = '';
if (typeof initial === 'string') {
result = push(result, 0, initial);
} else if (typeof initial === 'object' && initial.keys && initial.content) {
result.keys = Object.assign([], initial.keys);
result.content = Object.assign([], initial.content);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are killing me, is this what people use nowadays?

}

return result;
}

class HermDoc {
constructor(initial) {
this.set(newHermDoc(initial));
}

set(fromDoc) {
this.keys = Object.assign([], fromDoc.keys);
this.content = Object.assign([], fromDoc.content);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...

}

duplicate() {
const t = new HermDoc();
t.keys = Object.assign([], this.keys);
t.content = Object.assign([], this.content);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls stop

return t;
}

display() {
return display(this);
}

displayRange(from, to) {
return displayRange(this, from, to);
}

indexOf(element) {
return indexOf(this, element);
}

getKeys() {
return this.keys.filter(e => this.content[e] !== null);
return getKeys(this);
}

deleteRange(from, length) {
const keys = this.getKeys();
for (let i = 0; i < length; i += 1) {
const k = keys[from + i];
this.content[k] = null;
}
this.set(deleteRange(this, from, length));
}

push(index, value) {
this.set(push(this, index, value));
}

merge(newDoc) {
this.set(merge(this, newDoc));
}
}

export default HermDoc;
export {
ranID,
indexOf,
getKeys,
duplicate,
display,
deleteRange,
displayRange,
push,
merge,
newHermDoc,
};
Loading