Skip to content

Commit

Permalink
Merge pull request #11732 from keymanapp/fix/web/array-from-polyfill
Browse files Browse the repository at this point in the history
fix(web): add limited Array.from polyfill for lm-worker use
  • Loading branch information
jahorton authored Jun 14, 2024
2 parents 13700e4 + 062a867 commit 020bdb4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion common/models/wordbreakers/src/default/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ export class BreakerContext {
* @param chunk a chunk of text. Starts and ends at word boundaries.
*/
function isNonSpace(chunk: string, options?: DefaultWordBreakerOptions): boolean {
return !Array.from(chunk).map((char) => property(char, options)).every(wb => (
return !chunk.split('').map((char) => property(char, options)).every(wb => (
wb === WordBreakProperty.CR ||
wb === WordBreakProperty.LF ||
wb === WordBreakProperty.Newline ||
Expand Down
3 changes: 3 additions & 0 deletions common/web/lm-worker/build-polyfiller.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ let sourceFileSet = [
// Needed for Android / Chromium browser pre-41.
loadPolyfill('../../../node_modules/string.prototype.codepointat/codepointat.js', 'src/polyfills/string.codepointat.js'),
// Needed for Android / Chromium browser pre-45.
// Not used in this codebase, but used by some compiled model defaults.
loadPolyfill('src/polyfills/array.from.js', 'src/polyfills/array.from.js'),
// Needed for Android / Chromium browser pre-45.
loadPolyfill('src/polyfills/array.fill.js', 'src/polyfills/array.fill.js'),
// Needed for Android / Chromium browser pre-45.
loadPolyfill('src/polyfills/array.findIndex.js', 'src/polyfills/array.findIndex.js'),
Expand Down
44 changes: 44 additions & 0 deletions common/web/lm-worker/src/polyfills/array.from.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
(function() {
if(!Array.from) {
function isHighSurrogate(codeUnit) {
codeUnit = codeUnit.charCodeAt(0);
return codeUnit >= 0xD800 && codeUnit <= 0xDBFF;
}

function isLowSurrogate(codeUnit) {
codeUnit = codeUnit.charCodeAt(0);
return codeUnit >= 0xDC00 && codeUnit <= 0xDFFF;
}

Array.from = function (obj) {
if(Array.isArray(obj)) {
// Simple array clone
return obj.slice();
} else if(typeof obj == 'string') {
// Array.from is surrogate-aware and will not split surrogate pairs.
// We can start with a full split and then remerge the pairs.
var simpleSplit = obj.split('');

/** @type {string[]} */
var finalSplit = [];
/** @type {number} */
var i;

while(simpleSplit.length > 0) {
// Do we have a surrogate pair?
var a = simpleSplit.shift();
if(isHighSurrogate(a) && (isLowSurrogate(simpleSplit[0] || ''))) {
// yes, so merge them before pushing.
a = a + simpleSplit.shift();
console.log(a);
} // else: 'no', so just push the current char to the array and continue

finalSplit.push(a);
}
return finalSplit;
} else {
throw "Unexpected + nonpolyfilled use of Array.from encountered; aborting";
}
}
}
}());

0 comments on commit 020bdb4

Please sign in to comment.