Merge pull request #11733 from keymanapp/fix/web/cherry-pick/array-from-polyfill

fix(web): add limited Array.from polyfill for lm-worker use 🍒 🏠
This commit is contained in:
Joshua Horton 2024-06-17 11:25:25 +07:00 • committed by GitHub
commit 4bb80fb836
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 48 additions and 1 deletions

View file

@ -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 ||

View file

@ -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'),

View file

@ -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";
}
}
}
}());