-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathto-unicode.js
More file actions
42 lines (42 loc) 路 1.14 KB
/
Copy pathto-unicode.js
File metadata and controls
42 lines (42 loc) 路 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
'use strict';
/**
* Namespace for static methods that convert characters and strings to Unicode
* @author S0AndS0
* @copyright AGPL-3.0
*/
class toUnicode {
/**
* Converts character to Hex Unicode
* @param {string} character
* @return {string}
* @author S0AndS0
* @copyright AGPL-3.0
* @example
* toUnicode.fromCharacter('馃惣');
* //> "1f43c"
*/
static fromCharacter(character) {
return character.codePointAt(undefined).toString(16);
}
/**
* Converts string to character array of Unicode(s)
* @param {string} characters
* @return {string[]}
* @author S0AndS0
* @copyright AGPL-3.0
* @example
* toUnicode.fromString('馃帀 馃憢');
* //> [ '1f389', '20', '1f44b' ]
*/
static fromString(characters, prefix = '') {
return [...characters].reduce((accumulator, character) => {
const unicode = toUnicode.fromCharacter(character);
accumulator.push(`${prefix}${unicode}`);
return accumulator;
}, []);
}
}
/* istanbul ignore next */
if (typeof module !== 'undefined') {
module.exports = toUnicode;
}