Skip to content

Commit f8ea902

Browse files
author
Joshua Horton
committed
refactor(web): refactor _kmwCharAt
1 parent 70c1920 commit f8ea902

File tree

4 files changed

+45
-59
lines changed

4 files changed

+45
-59
lines changed

web/src/engine/common/web-utils/src/kmwstring.ts

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,27 @@ export function codeUnitToCodePoint(s: string, codeUnitIndex: number) {
384384
}
385385
}
386386

387+
/**
388+
* Returns the character at a the code point index passed
389+
*
390+
* @param {string} s The string
391+
* @param {number} index A code point index in the string
392+
* @return {string} The corresponding character
393+
*/
394+
export function charAt(s: string, index: number) { // charAtPoint
395+
const str = s;
396+
397+
if(!EXTENSION_ENABLED) {
398+
return str.charAt(index);
399+
}
400+
401+
if(index >= 0) {
402+
return substr(str, index, 1);
403+
} else {
404+
return ''
405+
};
406+
}
407+
387408
/*
388409
* TODO: Remove this file as part of addressing https://github.com/keymanapp/keyman/issues/2492.
389410
*/
@@ -392,53 +413,18 @@ declare global {
392413
interface StringConstructor {
393414
kmwEnableSupplementaryPlane(bEnable: boolean): void
394415
}
395-
396-
interface String {
397-
kmwCharAt(codePointIndex: number) : string,
398-
_kmwCharAt(codePointIndex: number) : string,
399-
}
400416
}
401417

402418
export default function extendString() {
403-
/*
404-
Helper functions
405-
*/
406-
407-
/**
408-
* Returns the character at a the code point index passed
409-
*
410-
* @param {number} codePointIndex A code point index in the string
411-
* @return {string} The corresponding character
412-
*/
413-
String.prototype.kmwCharAt = function(codePointIndex) {
414-
var str = String(this);
415-
416-
if(codePointIndex >= 0) return substr(str, codePointIndex,1); else return '';
417-
}
418-
419-
/**
420-
* String prototype library extensions for basic plane characters,
421-
* to simplify enabling or disabling supplementary plane functionality (I3319)
422-
*/
423-
424-
425419
/**
426420
* Enable or disable supplementary plane string handling
427421
*
428422
* @param {boolean} bEnable
429423
*/
430424
String.kmwEnableSupplementaryPlane = function(bEnable)
431425
{
432-
var p=String.prototype;
433-
p._kmwCharAt = bEnable ? p.kmwCharAt : p.charAt;
434-
435426
enableSupplementaryPlane(bEnable);
436427
}
437-
438-
// Ensure that _all_ String extensions are established, even if disabled by default.
439-
if(!""._kmwCharAt) {
440-
String.kmwEnableSupplementaryPlane(false);
441-
}
442428
}
443429

444430
// For side-effect imports:

web/src/engine/common/web-utils/src/tests/stringHandling.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ describe('Unicode string handling', () => {
88
String.kmwEnableSupplementaryPlane(false);
99
});
1010

11-
it('_kmwCharAt', () => {
11+
it('charAt', () => {
1212
const str = "string";
13-
assert.equal(str._kmwCharAt(-1), '');
14-
assert.equal(str._kmwCharAt(6), '');
15-
assert.equal(str._kmwCharAt(1000), '');
16-
assert.equal(str._kmwCharAt(0), "s");
17-
assert.equal(str._kmwCharAt(2), "r");
18-
assert.equal(str._kmwCharAt(5), "g");
13+
assert.equal(KMWString.charAt(str, -1), '');
14+
assert.equal(KMWString.charAt(str, 6), '');
15+
assert.equal(KMWString.charAt(str, 1000), '');
16+
assert.equal(KMWString.charAt(str, 0), "s");
17+
assert.equal(KMWString.charAt(str, 2), "r");
18+
assert.equal(KMWString.charAt(str, 5), "g");
1919
});
2020

2121
it('charCodeAt', () => {
@@ -121,14 +121,14 @@ describe('Unicode string handling', () => {
121121
String.kmwEnableSupplementaryPlane(true);
122122
});
123123

124-
it('_kmwCharAt', () => {
124+
it('charAt', () => {
125125
const str = "string";
126-
assert.equal(str._kmwCharAt(-1), '');
127-
assert.equal(str._kmwCharAt(6), '');
128-
assert.equal(str._kmwCharAt(1000), '');
129-
assert.equal(str._kmwCharAt(0), "s");
130-
assert.equal(str._kmwCharAt(2), "r");
131-
assert.equal(str._kmwCharAt(5), "g");
126+
assert.equal(KMWString.charAt(str, -1), '');
127+
assert.equal(KMWString.charAt(str, 6), '');
128+
assert.equal(KMWString.charAt(str, 1000), '');
129+
assert.equal(KMWString.charAt(str, 0), "s");
130+
assert.equal(KMWString.charAt(str, 2), "r");
131+
assert.equal(KMWString.charAt(str, 5), "g");
132132
});
133133

134134
it('charCodeAt', () => {
@@ -240,17 +240,17 @@ describe('Unicode string handling', () => {
240240
String.kmwEnableSupplementaryPlane(true);
241241
});
242242

243-
it('_kmwCharAt', () => {
243+
it('charAt', () => {
244244
// 0x1d5c9: MATHEMATICAL SANS-SERIF SMALL p
245245
// 0x1d5be: MATHEMATICAL SANS-SERIF SMALL e
246246
const apples = 'a' + String.fromCodePoint(0x1d5c9) + 'p' + 'l' + String.fromCodePoint(0x1d5be) + 's';
247-
assert.equal(apples._kmwCharAt(-1), '');
248-
assert.equal(apples._kmwCharAt(6), '');
249-
assert.equal(apples._kmwCharAt(1000), '');
250-
assert.equal(apples._kmwCharAt(0), "a");
251-
assert.equal(apples._kmwCharAt(1), String.fromCodePoint(0x1d5c9));
252-
assert.equal(apples._kmwCharAt(2), "p");
253-
assert.equal(apples._kmwCharAt(5), "s");
247+
assert.equal(KMWString.charAt(apples, -1), '');
248+
assert.equal(KMWString.charAt(apples, 6), '');
249+
assert.equal(KMWString.charAt(apples, 1000), '');
250+
assert.equal(KMWString.charAt(apples, 0), "a");
251+
assert.equal(KMWString.charAt(apples, 1), String.fromCodePoint(0x1d5c9));
252+
assert.equal(KMWString.charAt(apples, 2), "p");
253+
assert.equal(KMWString.charAt(apples, 5), "s");
254254
});
255255

256256
it('charCodeAt', () => {

web/src/engine/js-processor/src/kbdInterface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ export default class KeyboardInterface extends KeyboardHarness {
643643
// Nope, so let's build its cache.
644644
var result: ComplexKeyboardStore = [];
645645
for(var i=0; i < KMWString.length(store); i++) {
646-
result.push(store._kmwCharAt(i));
646+
result.push(KMWString.charAt(store, i));
647647
}
648648

649649
// Cache the result for later!

web/src/engine/predictive-text/worker-thread/src/main/correction/context-tracker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function textToCharTransforms(text: string, transformId?: number) {
1818
let perCharTransforms: Transform[] = [];
1919

2020
for(let i=0; i < KMWString.length(text); i++) {
21-
let char = text.kmwCharAt(i); // is SMP-aware
21+
let char = KMWString.charAt(text, i); // is SMP-aware
2222

2323
let transform: Transform = {
2424
insert: char,

0 commit comments

Comments
 (0)