Skip to content

Commit

Permalink
feat: add .prettierrc
Browse files Browse the repository at this point in the history
  • Loading branch information
cudr committed Feb 6, 2022
1 parent 1d5aaee commit 8c7133a
Show file tree
Hide file tree
Showing 28 changed files with 378 additions and 372 deletions.
7 changes: 2 additions & 5 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
{
"presets": [
"@babel/preset-env",
"@babel/typescript"
],
"presets": ["@babel/preset-env", "@babel/typescript"],
"plugins": [
"@babel/proposal-class-properties",
"@babel/proposal-object-rest-spread"
]
}
}
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"printWidth": 80,
"semi": false,
"singleQuote": true,
"arrowParens": "avoid"
}
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ install:

cache:
directories:
- node_modules
- node_modules
27 changes: 16 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# scto

Text diff compare to OP (CRDT Operation model)

[![Build Status](https://travis-ci.org/cudr/scto.svg?branch=master)](https://travis-ci.org/cudr/scto)
Expand All @@ -18,6 +19,7 @@ yarn add scto
### Example

Text compare:

```ts
import { stringDiffToOps, applyOps } from 'scto'

Expand All @@ -40,6 +42,7 @@ console.log(applyed === modifyed) // true
```

Array compare:

```ts
import { collateDiffToOps, applyOps } from 'scto'

Expand All @@ -60,33 +63,35 @@ const applyed = applyOps(origin, operations)
/* ['abc', 'foo', 'baz'] */
```


### Possible operations:

Replace:

```ts
export interface Replace {
type: "replace";
offset: number;
shift: number;
data: Collate;
type: 'replace'
offset: number
shift: number
data: Collate
}
```

Drop:

```ts
export interface Drop {
type: "drop";
offset: number;
shift: number;
type: 'drop'
offset: number
shift: number
}
```

Insert:

```ts
export interface Insert {
type: "insert";
offset: number;
data: Collate;
type: 'insert'
offset: number
data: Collate
}
```
50 changes: 25 additions & 25 deletions benchmark/benchmark-utils.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
import faker from "faker";
import { insert, drop } from "../src/utils";
import faker from 'faker'
import { insert, drop } from '../src/utils'

export const genSentences = (iterations: number = 100): string => {
let text = "",
i = 0;
let text = '',
i = 0

for (i; i < iterations; i++) {
text += faker.lorem.sentences();
text += faker.lorem.sentences()
}

return text;
};
return text
}

let simbols =
"!@#$%^&*()_+~`|  }{[]:;?><,./-=0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
'!@#$%^&*()_+~`|  }{[]:;?><,./-=0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

export const getRandSimbol = (): string => {
return simbols[Math.floor(Math.random() * simbols.length)];
};
return simbols[Math.floor(Math.random() * simbols.length)]
}

export const randomizeString = (source: string) => {
const rand = Math.random();
const rand = Math.random()

if (rand < 0.6) return source;
if (rand < 0.6) return source

const index = Math.floor(Math.random() * source.length);
const index = Math.floor(Math.random() * source.length)

if (rand > 0.8) return insert(source, getRandSimbol(), index) as string;
if (rand > 0.8) return insert(source, getRandSimbol(), index) as string

return drop(source, index, 1) as string;
};
return drop(source, index, 1) as string
}

export const randomizeText = (text: string = ""): string => {
const arr = text.split(" ");
export const randomizeText = (text: string = ''): string => {
const arr = text.split(' ')

const randomArr = arr.reduce((acc: string[], el: string) => {
const rand = Math.random();
const rand = Math.random()

if (rand < 0.3) return acc;
if (rand > 0.7) return [...acc, faker.random.words()];
if (rand < 0.3) return acc
if (rand > 0.7) return [...acc, faker.random.words()]

return [...acc, randomizeString(el)];
}, []);
return [...acc, randomizeString(el)]
}, [])

return randomArr.join(" ");
};
return randomArr.join(' ')
}
60 changes: 30 additions & 30 deletions benchmark/benchmark.spec.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
import { diffWordsWithSpace } from "diff";
import { diff_match_patch } from "diff-match-patch";
import { stringDiffToOps } from "../src/compare/string-diff-to-operations";
import { applyOps } from "../src/operations";
import { genSentences, randomizeText } from "./benchmark-utils";
import { diffWordsWithSpace } from 'diff'
import { diff_match_patch } from 'diff-match-patch'
import { stringDiffToOps } from '../src/compare/string-diff-to-operations'
import { applyOps } from '../src/operations'
import { genSentences, randomizeText } from './benchmark-utils'

describe("benchmark diff compare", () => {
const origin = genSentences(50);
const modifyed = randomizeText(origin);
describe('benchmark diff compare', () => {
const origin = genSentences(50)
const modifyed = randomizeText(origin)

let dmp: any = new diff_match_patch();
let dmp: any = new diff_match_patch()

console.time("csto stringDiffToOps");
const operations = stringDiffToOps(origin, modifyed);
console.timeEnd("csto stringDiffToOps");
console.time('csto stringDiffToOps')
const operations = stringDiffToOps(origin, modifyed)
console.timeEnd('csto stringDiffToOps')

console.time("jsdiff diffWordsWithSpace");
const diff = diffWordsWithSpace(origin, modifyed);
console.timeEnd("jsdiff diffWordsWithSpace");
console.time('jsdiff diffWordsWithSpace')
const diff = diffWordsWithSpace(origin, modifyed)
console.timeEnd('jsdiff diffWordsWithSpace')

console.time("diff-match-patch patch_make");
const dmp_patch = dmp.patch_make(origin, modifyed);
console.timeEnd("diff-match-patch patch_make");
console.time('diff-match-patch patch_make')
const dmp_patch = dmp.patch_make(origin, modifyed)
console.timeEnd('diff-match-patch patch_make')

it("string scto equals", () => {
expect(applyOps(origin, operations)).toBe(modifyed);
});
it('string scto equals', () => {
expect(applyOps(origin, operations)).toBe(modifyed)
})

it("string jsdiff equals", () => {
it('string jsdiff equals', () => {
const result = diff.reduce(
(acc: any, el) => (el.removed ? acc : acc + el.value),
""
);
''
)

expect(result).toBe(modifyed);
});
expect(result).toBe(modifyed)
})

it("string diff-match-patch equals", () => {
expect(dmp.patch_apply(dmp_patch, origin)[0]).toBe(modifyed);
});
});
it('string diff-match-patch equals', () => {
expect(dmp.patch_apply(dmp_patch, origin)[0]).toBe(modifyed)
})
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"husky": "^3.0.0",
"jest": "^24.8.0",
"lint-staged": "^9.1.0",
"prettier": "^1.18.2",
"prettier": "^2.5.1",
"ts-jest": "^24.0.2",
"tslint": "^6.1.3",
"tslint-config-prettier": "^1.18.0",
Expand Down
30 changes: 15 additions & 15 deletions src/compare/collate-diff-to-operations.spec.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { collateDiffToOps } from "./collate-diff-to-operations";
import { applyOps } from "../operations";
import { collateDiffToOps } from './collate-diff-to-operations'
import { applyOps } from '../operations'

import { origin, modifyed } from "../../text-source";
import { origin, modifyed } from '../../text-source'

describe("source diff to operations", () => {
it("list diff equals one", () => {
const one = origin.split(" ");
const two = modifyed.split(" ");
describe('source diff to operations', () => {
it('list diff equals one', () => {
const one = origin.split(' ')
const two = modifyed.split(' ')

const applyed = applyOps(one, collateDiffToOps(one, two));
const applyed = applyOps(one, collateDiffToOps(one, two))

expect(applyed).toStrictEqual(two);
});
expect(applyed).toStrictEqual(two)
})

it("string diff equals", () => {
const applyed = applyOps(origin, collateDiffToOps(origin, modifyed));
it('string diff equals', () => {
const applyed = applyOps(origin, collateDiffToOps(origin, modifyed))

expect(applyed).toStrictEqual(modifyed);
});
});
expect(applyed).toStrictEqual(modifyed)
})
})
38 changes: 19 additions & 19 deletions src/compare/collate-diff-to-operations.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
import { shiftCompare } from "./shift-compare";
import { drop, insert } from "../utils";
import { shiftCompare } from './shift-compare'
import { drop, insert } from '../utils'

import { Operation, Collate, Replace, Insert, Drop } from "../operations";
import { Operation, Collate, Replace, Insert, Drop } from '../operations'

export const collateDiffToOps = (
origin: Collate,
modifyed: Collate
): Operation[] => {
const maxLen = Math.max(origin.length, modifyed.length),
operations = [];
operations = []

let i = 0;
let i = 0

for (i; i <= maxLen; i++) {
let operation: Operation | null =
origin[i] !== modifyed[i] ? shiftCompare(origin, modifyed, i) : null;
origin[i] !== modifyed[i] ? shiftCompare(origin, modifyed, i) : null

if (operation) {
operations.push(operation);
operations.push(operation)

const { type } = operation;
const { type } = operation

const { data } = operation as Replace | Insert;
const { shift } = operation as Replace | Drop;
const { data } = operation as Replace | Insert
const { shift } = operation as Replace | Drop

if (type === "insert") {
origin = insert(origin, data, i);
i += data.length;
} else if (type === "replace") {
origin = insert(origin, data, i, shift);
} else if (type === "drop") {
origin = drop(origin, i, shift);
if (type === 'insert') {
origin = insert(origin, data, i)
i += data.length
} else if (type === 'replace') {
origin = insert(origin, data, i, shift)
} else if (type === 'drop') {
origin = drop(origin, i, shift)
}
}
}

return operations;
};
return operations
}
Loading

0 comments on commit 8c7133a

Please sign in to comment.