mirror of
https://github.com/keymanapp/keyman.git
synced 2026-10-29 17:27:58 +00:00
feat(developer): move convertToUnicodeCharacter etc to local module kmcConvertUtil.ts
This commit is contained in:
parent
a09d615ed0
commit
0ee3f69366
3 changed files with 18 additions and 114 deletions
|
|
@ -207,106 +207,6 @@ export function describeCodepoint(ch: number): string {
|
|||
return `${s} (U+${Number(ch).toString(16).toUpperCase()})`;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief function to convert a Html-Entity Hex value to a unicode character e.g. c -> c; 😎 -> 😎
|
||||
* @param hexEntity the value that will converted
|
||||
* @return a unicode character or undefined if in_str is not recognized
|
||||
*/
|
||||
export function decodeHexEntity(hexEntity: string) {
|
||||
const hex = hexEntity.replace('&#x', '').replace(';', '');
|
||||
if (hexEntity.length > 9)
|
||||
return undefined;
|
||||
else
|
||||
return String.fromCodePoint(parseInt(hex, 16));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief function to convert a Html-Entity decimal value value to a unicode character e.g. c -> c; 😎 -> 😎
|
||||
* @param dec_Entity the value that will converted
|
||||
* @return a unicode character or undefined if in_str is not recognized
|
||||
*/
|
||||
export function decodeDecEntity(dec_Entity: string) {
|
||||
const dec = dec_Entity.replace('&#', '').replace(';', '');
|
||||
if (dec_Entity.length > 9)
|
||||
return undefined;
|
||||
else
|
||||
return String.fromCodePoint(parseInt(dec, 10));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief function to convert a unicode hex value to a unicode character e.g. U+0063 -> c; U+1F60E; -> 😎
|
||||
* @param Uni_Val the value that will converted
|
||||
* @return a unicode character or undefined if in_str is not recognized
|
||||
*/
|
||||
export function decodeUniHexValue(Uni_Val: string): any {
|
||||
const hexval = Uni_Val.replace('U+', '0x');
|
||||
if (Uni_Val.length > 8)
|
||||
return undefined;
|
||||
else
|
||||
return String.fromCodePoint(parseInt(hexval, 16));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief function to convert a numeric character reference or a unicode value to a unicode character e.g. c -> c; U+1F60E -> 😎
|
||||
* @param in_str the value that will converted
|
||||
* @return a unicode character like 'c', 'ሴ', '😎' or undefined if in_str is not recognized
|
||||
*/
|
||||
export function convertToUnicodeCharacter(in_str: string): string {
|
||||
|
||||
if ((in_str === null) || (in_str === undefined)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
else if (in_str.substring(0, 3) === "&#x") {
|
||||
return decodeHexEntity(in_str);
|
||||
}
|
||||
|
||||
else if (in_str.substring(0, 2) === "&#") {
|
||||
return decodeDecEntity(in_str);
|
||||
}
|
||||
|
||||
else if (in_str.substring(0, 1) === "&") {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
else if (in_str.substring(0, 2) === "U+") {
|
||||
return decodeUniHexValue(in_str);
|
||||
}
|
||||
|
||||
else if ([...new Intl.Segmenter().segment(in_str)].length <= 1) {
|
||||
return in_str;
|
||||
}
|
||||
else {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @brief function to convert a numeric character reference to a unicode Code Point e.g. ሴ -> U+1234; 􏘁 -> U+1F60E
|
||||
* @param instr the value that will converted
|
||||
* @return returns a unicode Code Point like U+0063, U+1234, U+1F60E; returns the input character if a non-numeric reference is used or returns 'undefined' if instr is not recognized
|
||||
*/
|
||||
export function convertToUnicodeCodePoint(instr: string): string {
|
||||
|
||||
if ((instr === null) || (instr === undefined)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (instr.substring(0, 3) === "&#x") {
|
||||
const num_length = instr.length - instr.indexOf("x") - 1;
|
||||
const num_str = instr.substring(instr.indexOf("x") + 1, instr.length - 1);
|
||||
return ("U+" + num_str.slice(-num_length).padStart(4, "0"));
|
||||
}
|
||||
|
||||
// if not hex: convert to hex
|
||||
if ((instr.substring(0, 2) === "&#")) {
|
||||
const num_length = instr.length - instr.indexOf("#") - 1;
|
||||
const num_str = instr.substring(instr.indexOf("#") + 1, instr.length - 1);
|
||||
return "U+" + Number(num_str.slice(-num_length)).toString(16).slice(-6).toUpperCase().padStart(4, "0");
|
||||
}
|
||||
else
|
||||
return instr;
|
||||
}
|
||||
|
||||
export enum BadStringType {
|
||||
pua = 'PUA',
|
||||
unassigned = 'Unassigned',
|
||||
|
|
|
|||
|
|
@ -9,7 +9,10 @@
|
|||
|
||||
import { CompilerCallbacks, CompilerOptions } from "@keymanapp/developer-utils";
|
||||
import { KeylayoutToKmnConverter, ProcesData, Rule } from './keylayout-to-kmn-converter.js';
|
||||
import { util } from '@keymanapp/common-types';
|
||||
//import { util } from '@keymanapp/common-types';
|
||||
import * as kmcConvertUtil from '../kmcConvertUtil.js';
|
||||
|
||||
|
||||
import { ConverterMessages } from '../converter-messages.js';
|
||||
import KEYMAN_VERSION from "@keymanapp/keyman-version";
|
||||
|
||||
|
|
@ -177,8 +180,8 @@ export class KmnFileWriter {
|
|||
const warn_text = this.reviewRules(unique_data_Rules, k);
|
||||
|
||||
const output_character = new TextDecoder().decode(unique_data_Rules[k].output);
|
||||
const output_Unicode_Character = util.convertToUnicodeCharacter(output_character);
|
||||
const output_Unicode_CodePoint = util.convertToUnicodeCodePoint(output_character);
|
||||
const output_Unicode_Character = kmcConvertUtil.convertToUnicodeCharacter(output_character);
|
||||
const output_Unicode_CodePoint = kmcConvertUtil.convertToUnicodeCodePoint(output_character);
|
||||
|
||||
if ((output_Unicode_Character !== undefined) && (output_Unicode_CodePoint !== undefined)) {
|
||||
|
||||
|
|
@ -242,8 +245,8 @@ export class KmnFileWriter {
|
|||
const warn_text = this.reviewRules(unique_data_Rules, k);
|
||||
|
||||
const output_character = new TextDecoder().decode(unique_data_Rules[k].output);
|
||||
const output_Unicode_Character = util.convertToUnicodeCharacter(output_character);
|
||||
const output_Unicode_CodePoint = util.convertToUnicodeCodePoint(output_character);
|
||||
const output_Unicode_Character = kmcConvertUtil.convertToUnicodeCharacter(output_character);
|
||||
const output_Unicode_CodePoint = kmcConvertUtil.convertToUnicodeCodePoint(output_character);
|
||||
|
||||
if ((output_Unicode_Character !== undefined) && (output_Unicode_CodePoint !== undefined)) {
|
||||
// if we are about to print a unicode codepoint instead of a single character we need to check if it is a control character
|
||||
|
|
@ -328,8 +331,8 @@ export class KmnFileWriter {
|
|||
|
||||
const warn_text = this.reviewRules(unique_data_Rules, k);
|
||||
const output_character = new TextDecoder().decode(unique_data_Rules[k].output);
|
||||
const output_Unicode_Character = util.convertToUnicodeCharacter(output_character);
|
||||
const output_Unicode_CodePoint = util.convertToUnicodeCodePoint(output_character);
|
||||
const output_Unicode_Character = kmcConvertUtil.convertToUnicodeCharacter(output_character);
|
||||
const output_Unicode_CodePoint = kmcConvertUtil.convertToUnicodeCodePoint(output_character);
|
||||
|
||||
if ((output_Unicode_Character !== undefined) && (output_Unicode_CodePoint !== undefined)) {
|
||||
// if we are about to print a unicode codepoint instead of a single character we need to check if a control character is to be used
|
||||
|
|
|
|||
|
|
@ -9,7 +9,8 @@
|
|||
|
||||
import 'mocha';
|
||||
import { assert } from 'chai';
|
||||
import { util } from '@keymanapp/common-types';
|
||||
//import { util } from '@keymanapp/common-types';
|
||||
//import * as kmcConvertUtil from '../src/kmcConvertUtil.js';
|
||||
import KEYMAN_VERSION from "@keymanapp/keyman-version";
|
||||
import { compilerTestCallbacks, compilerTestOptions, makePathToFixture } from './helpers/index.js';
|
||||
import { KeylayoutToKmnConverter, ProcesData, Rule } from '../src/keylayout-to-kmn/keylayout-to-kmn-converter.js';
|
||||
|
|
@ -202,7 +203,7 @@ describe('KmnFileWriter', function () {
|
|||
});
|
||||
});
|
||||
|
||||
describe('convertToUnicodeCodePoint ', function () {
|
||||
/*describe('convertToUnicodeCodePoint ', function () {
|
||||
[
|
||||
["􏘁", 'U+10F601'],
|
||||
["😁", 'U+1F601'],
|
||||
|
|
@ -225,13 +226,13 @@ describe('KmnFileWriter', function () {
|
|||
[' ;', ' ;']
|
||||
].forEach(function (values) {
|
||||
it(('should convert "' + values[0] + '"').padEnd(25, " ") + 'to "' + values[1] + '"', async function () {
|
||||
const result = util.convertToUnicodeCodePoint(values[0] as string);
|
||||
const result = kmcConvertUtil.convertToUnicodeCodePoint(values[0] as string);
|
||||
assert.equal(result, values[1]);
|
||||
});
|
||||
});
|
||||
});
|
||||
});*/
|
||||
|
||||
describe('convertToUnicodeCharacter ', function () {
|
||||
/*describe('convertToUnicodeCharacter ', function () {
|
||||
[
|
||||
["a", 'a'],
|
||||
["ሴ", 'ሴ'],
|
||||
|
|
@ -256,11 +257,11 @@ describe('KmnFileWriter', function () {
|
|||
[null, undefined]
|
||||
].forEach(function (values) {
|
||||
it(('should convert "' + values[0] + '"').padEnd(25, " ") + 'to "' + values[1] + '"', async function () {
|
||||
const result = util.convertToUnicodeCharacter(values[0] as string);
|
||||
const result = kmcConvertUtil.convertToUnicodeCharacter(values[0] as string);
|
||||
assert.equal(result, values[1]);
|
||||
});
|
||||
});
|
||||
});
|
||||
});*/
|
||||
|
||||
describe('reviewRules messages', function () {
|
||||
const sut_w = new KmnFileWriter(compilerTestCallbacks, compilerTestOptions);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue