|
"use strict"; |
|
Object.defineProperty(exports, "__esModule", { value: true }); |
|
const boundaries_1 = require("./boundaries"); |
|
|
|
|
|
const NotBreak = 0; |
|
const BreakStart = 1; |
|
const Break = 2; |
|
const BreakLastRegional = 3; |
|
const BreakPenultimateRegional = 4; |
|
class GraphemerHelper { |
|
|
|
|
|
|
|
|
|
|
|
|
|
static isSurrogate(str, pos) { |
|
return (0xd800 <= str.charCodeAt(pos) && |
|
str.charCodeAt(pos) <= 0xdbff && |
|
0xdc00 <= str.charCodeAt(pos + 1) && |
|
str.charCodeAt(pos + 1) <= 0xdfff); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static codePointAt(str, idx) { |
|
if (idx === undefined) { |
|
idx = 0; |
|
} |
|
const code = str.charCodeAt(idx); |
|
|
|
if (0xd800 <= code && code <= 0xdbff && idx < str.length - 1) { |
|
const hi = code; |
|
const low = str.charCodeAt(idx + 1); |
|
if (0xdc00 <= low && low <= 0xdfff) { |
|
return (hi - 0xd800) * 0x400 + (low - 0xdc00) + 0x10000; |
|
} |
|
return hi; |
|
} |
|
|
|
if (0xdc00 <= code && code <= 0xdfff && idx >= 1) { |
|
const hi = str.charCodeAt(idx - 1); |
|
const low = code; |
|
if (0xd800 <= hi && hi <= 0xdbff) { |
|
return (hi - 0xd800) * 0x400 + (low - 0xdc00) + 0x10000; |
|
} |
|
return low; |
|
} |
|
|
|
|
|
return code; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static shouldBreak(start, mid, end, startEmoji, midEmoji, endEmoji) { |
|
const all = [start].concat(mid).concat([end]); |
|
const allEmoji = [startEmoji].concat(midEmoji).concat([endEmoji]); |
|
const previous = all[all.length - 2]; |
|
const next = end; |
|
const nextEmoji = endEmoji; |
|
|
|
|
|
|
|
const rIIndex = all.lastIndexOf(boundaries_1.CLUSTER_BREAK.REGIONAL_INDICATOR); |
|
if (rIIndex > 0 && |
|
all.slice(1, rIIndex).every(function (c) { |
|
return c === boundaries_1.CLUSTER_BREAK.REGIONAL_INDICATOR; |
|
}) && |
|
[boundaries_1.CLUSTER_BREAK.PREPEND, boundaries_1.CLUSTER_BREAK.REGIONAL_INDICATOR].indexOf(previous) === -1) { |
|
if (all.filter(function (c) { |
|
return c === boundaries_1.CLUSTER_BREAK.REGIONAL_INDICATOR; |
|
}).length % |
|
2 === |
|
1) { |
|
return BreakLastRegional; |
|
} |
|
else { |
|
return BreakPenultimateRegional; |
|
} |
|
} |
|
|
|
if (previous === boundaries_1.CLUSTER_BREAK.CR && next === boundaries_1.CLUSTER_BREAK.LF) { |
|
return NotBreak; |
|
} |
|
|
|
else if (previous === boundaries_1.CLUSTER_BREAK.CONTROL || |
|
previous === boundaries_1.CLUSTER_BREAK.CR || |
|
previous === boundaries_1.CLUSTER_BREAK.LF) { |
|
return BreakStart; |
|
} |
|
|
|
else if (next === boundaries_1.CLUSTER_BREAK.CONTROL || |
|
next === boundaries_1.CLUSTER_BREAK.CR || |
|
next === boundaries_1.CLUSTER_BREAK.LF) { |
|
return BreakStart; |
|
} |
|
|
|
else if (previous === boundaries_1.CLUSTER_BREAK.L && |
|
(next === boundaries_1.CLUSTER_BREAK.L || |
|
next === boundaries_1.CLUSTER_BREAK.V || |
|
next === boundaries_1.CLUSTER_BREAK.LV || |
|
next === boundaries_1.CLUSTER_BREAK.LVT)) { |
|
return NotBreak; |
|
} |
|
|
|
else if ((previous === boundaries_1.CLUSTER_BREAK.LV || previous === boundaries_1.CLUSTER_BREAK.V) && |
|
(next === boundaries_1.CLUSTER_BREAK.V || next === boundaries_1.CLUSTER_BREAK.T)) { |
|
return NotBreak; |
|
} |
|
|
|
else if ((previous === boundaries_1.CLUSTER_BREAK.LVT || previous === boundaries_1.CLUSTER_BREAK.T) && |
|
next === boundaries_1.CLUSTER_BREAK.T) { |
|
return NotBreak; |
|
} |
|
|
|
else if (next === boundaries_1.CLUSTER_BREAK.EXTEND || next === boundaries_1.CLUSTER_BREAK.ZWJ) { |
|
return NotBreak; |
|
} |
|
|
|
else if (next === boundaries_1.CLUSTER_BREAK.SPACINGMARK) { |
|
return NotBreak; |
|
} |
|
|
|
else if (previous === boundaries_1.CLUSTER_BREAK.PREPEND) { |
|
return NotBreak; |
|
} |
|
|
|
const previousNonExtendIndex = allEmoji |
|
.slice(0, -1) |
|
.lastIndexOf(boundaries_1.EXTENDED_PICTOGRAPHIC); |
|
if (previousNonExtendIndex !== -1 && |
|
allEmoji[previousNonExtendIndex] === boundaries_1.EXTENDED_PICTOGRAPHIC && |
|
all.slice(previousNonExtendIndex + 1, -2).every(function (c) { |
|
return c === boundaries_1.CLUSTER_BREAK.EXTEND; |
|
}) && |
|
previous === boundaries_1.CLUSTER_BREAK.ZWJ && |
|
nextEmoji === boundaries_1.EXTENDED_PICTOGRAPHIC) { |
|
return NotBreak; |
|
} |
|
|
|
|
|
if (mid.indexOf(boundaries_1.CLUSTER_BREAK.REGIONAL_INDICATOR) !== -1) { |
|
return Break; |
|
} |
|
if (previous === boundaries_1.CLUSTER_BREAK.REGIONAL_INDICATOR && |
|
next === boundaries_1.CLUSTER_BREAK.REGIONAL_INDICATOR) { |
|
return NotBreak; |
|
} |
|
|
|
return BreakStart; |
|
} |
|
} |
|
exports.default = GraphemerHelper; |
|
|