code-point-mapping
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -5,4 +5,4 @@ export default class CodePointMapping { | ||
deleteAt(unitOff: number, unitLen: number): [number, number]; | ||
insertAt(unitOff: number, str: string): [number, string]; | ||
insertAt(unitOff: number, str: string): [number, ...string[]]; | ||
indexOfCodepoint(cpOff: number): number; | ||
} |
@@ -5,4 +5,6 @@ "use strict"; | ||
for (let x of str) { | ||
if (x.length === 1 && x.charCodeAt(0) >= 0xD800 && x.charCodeAt(0) <= 0xDFFF) { | ||
throw new Error("unpaired surrogate"); | ||
if (x.length === 1 && | ||
x.charCodeAt(0) >= 0xd800 && | ||
x.charCodeAt(0) <= 0xdfff) { | ||
throw new Error('unpaired surrogate'); | ||
} | ||
@@ -22,3 +24,3 @@ if (n.width === x.length) { | ||
} | ||
if (m && (m.width === n.width)) { | ||
if (m && m.width === n.width) { | ||
n.count += m.count; | ||
@@ -41,3 +43,3 @@ n.next = m.next; | ||
if (unitOff % n.width) { | ||
throw new Error("offset in middle of surrogate"); | ||
throw new Error('offset in middle of surrogate'); | ||
} | ||
@@ -47,3 +49,3 @@ n.next = { | ||
width: n.width, | ||
next: n.next, | ||
next: n.next | ||
}; | ||
@@ -53,3 +55,3 @@ n.count = unitOff / n.width; | ||
} | ||
throw new Error("offset beyond end of string"); | ||
throw new Error('offset beyond end of string'); | ||
} | ||
@@ -80,2 +82,3 @@ // CodePointMapping maintains a mapping between utf-16 code units | ||
// code units to the same in terms of unicode code points, | ||
// splits the string into unicode code points, | ||
// and updates the mapping appropriately. | ||
@@ -86,3 +89,3 @@ insertAt(unitOff, str) { | ||
graft(append(node, str), after); | ||
return [cpOff, str]; | ||
return [cpOff, ...str]; | ||
} | ||
@@ -102,5 +105,5 @@ // indexOfCodepoint converts the position in terms of unicode | ||
} | ||
throw new Error("cpOff outside of string"); | ||
throw new Error('cpOff outside of string'); | ||
} | ||
} | ||
exports.default = CodePointMapping; |
{ | ||
"name": "code-point-mapping", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "Map between javascript string indices and unicode code point offsets effectively", |
@@ -16,5 +16,5 @@ code-point-mapping provides a way to map between utf16 string indices | ||
import CodePointMapping from 'code-point-mapping' | ||
import * as automerge from "@automerge/automerge" | ||
import * as automerge from '@automerge/automerge' | ||
let doc1 = automerge.from({str: new automerge.Text("😀🎉✈️")}) | ||
let doc1 = automerge.from({ str: new automerge.Text('😀🎉✈️') }) | ||
let cpm = new CodePointMapping(doc1.str) | ||
@@ -26,3 +26,3 @@ | ||
d.str.deleteAt(...cpm.deleteAt(0, 2)) // d.str.deleteAt(0, 1) | ||
d.str.insertAt(...cpm.insertAt(2, "🧟♀️")) // d.str.insertAt(1, "🧟♀️") | ||
d.str.insertAt(...cpm.insertAt(2, '🧟♀️')) // d.str.insertAt(1, ..."🧟♀️") | ||
}) | ||
@@ -29,0 +29,0 @@ ``` |
5280
108