mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-04-20 15:36:16 -04:00
Feat/changeset ts (#6594)
* Migrated changeset * Added more tests. * Fixed test scopes
This commit is contained in:
parent
3dae23a1e5
commit
28e04bdf71
37 changed files with 2540 additions and 1310 deletions
54
src/static/js/StringIterator.ts
Normal file
54
src/static/js/StringIterator.ts
Normal file
|
@ -0,0 +1,54 @@
|
|||
import {assert} from "./Changeset";
|
||||
|
||||
/**
|
||||
* A custom made String Iterator
|
||||
*
|
||||
* @typedef {object} StringIterator
|
||||
* @property {Function} newlines -
|
||||
* @property {Function} peek -
|
||||
* @property {Function} remaining -
|
||||
* @property {Function} skip -
|
||||
* @property {Function} take -
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {string} str - String to iterate over
|
||||
* @returns {StringIterator}
|
||||
*/
|
||||
export class StringIterator {
|
||||
private curIndex: number;
|
||||
private newLines: number;
|
||||
private str: String
|
||||
|
||||
constructor(str: string) {
|
||||
this.curIndex = 0;
|
||||
this.str = str
|
||||
this.newLines = str.split('\n').length - 1;
|
||||
}
|
||||
remaining = () => this.str.length - this.curIndex;
|
||||
|
||||
getnewLines = () => this.newLines;
|
||||
|
||||
assertRemaining = (n: number) => {
|
||||
assert(n <= this.remaining(), `!(${n} <= ${this.remaining()})`);
|
||||
}
|
||||
|
||||
take = (n: number) => {
|
||||
this.assertRemaining(n);
|
||||
const s = this.str.substring(this.curIndex, this.curIndex+n);
|
||||
this.newLines -= s.split('\n').length - 1;
|
||||
this.curIndex += n;
|
||||
return s;
|
||||
}
|
||||
|
||||
peek = (n: number) => {
|
||||
this.assertRemaining(n);
|
||||
return this.str.substring(this.curIndex, this.curIndex+n);
|
||||
}
|
||||
|
||||
skip = (n: number) => {
|
||||
this.assertRemaining(n);
|
||||
this.curIndex += n;
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue