mirror of
https://github.com/keymanapp/keyman.git
synced 2026-09-24 08:37:42 +00:00
feat(common): isDenormalized() function for checking strings that are neither NFC nor NFD
Fixes: #7394
This commit is contained in:
parent
48339ee41e
commit
8e19508a81
2 changed files with 28 additions and 1 deletions
|
|
@ -305,6 +305,15 @@ export function isPUA(ch: number) {
|
|||
(ch >= Uni_PUA_16_START && ch <= Uni_PUA_16_END));
|
||||
}
|
||||
|
||||
/** @returns true if s is NEITHER NFC nor NFD */
|
||||
export function isDenormalized(s: string) : boolean {
|
||||
if(!s) return false; // empty or null
|
||||
const nfc = s.normalize("NFC");
|
||||
const nfd = s.normalize("NFD");
|
||||
if (s !== nfc && s !== nfd) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
class BadStringMap extends Map<BadStringType, Set<number>> {
|
||||
public toString() : string {
|
||||
if (!this.size) {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import 'mocha';
|
||||
import {assert} from 'chai';
|
||||
import {unescapeString, UnescapeError, isOneChar, toOneChar, unescapeOneQuadString, BadStringAnalyzer, isValidUnicode, describeCodepoint, isPUA, BadStringType, unescapeStringToRegex, unescapeQuadString, NFDAnalyzer} from '../../src/util/util.js';
|
||||
import {unescapeString, UnescapeError, isOneChar, toOneChar, unescapeOneQuadString, BadStringAnalyzer, isValidUnicode, describeCodepoint, isPUA, BadStringType, unescapeStringToRegex, unescapeQuadString, NFDAnalyzer, isDenormalized} from '../../src/util/util.js';
|
||||
|
||||
describe('test UTF32 functions()', function() {
|
||||
it('should properly categorize strings', () => {
|
||||
|
|
@ -186,6 +186,24 @@ describe('test bad char functions', () => {
|
|||
assert.isTrue(isPUA(ch), describeCodepoint(ch));
|
||||
}
|
||||
});
|
||||
describe('test isDenormalized()', () => {
|
||||
it('should correctly categorize strings', () => {
|
||||
[
|
||||
undefined,
|
||||
null,
|
||||
'',
|
||||
'ABC',
|
||||
'fa\u1E69cinating', // NFC
|
||||
'fas\u0323\u0307cinating', // NFD
|
||||
'd\u0323\u0307', // NFD
|
||||
'\u1e0d\u0307', // NFC
|
||||
].map(s => assert.isFalse(isDenormalized(s), `for string ${s}`));
|
||||
[
|
||||
'd\u0307\u0323', // NFD but reversed marks
|
||||
'fas\u0307\u0323cinating', // not-NFD
|
||||
].map(s => assert.isTrue(isDenormalized(s), `for string ${s}`));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('test BadStringAnalyzer', () => {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue