-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
55 lines (43 loc) · 1.2 KB
/
index.js
File metadata and controls
55 lines (43 loc) · 1.2 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
43
44
45
46
47
48
49
50
51
52
53
54
55
"use strict";
/*
* Full-width ASCII variants range from 0xFF01 to 0xFF5E.
* Therefore the non-inclusive upper and lower bounds for
* this range is 0xFF00 and 0xFF5F.
*
* 0x3000 is the Ideographic Space, which is the full-width
* equivalent of space (0x0020) in ASCII.
*/
const toFullWidth = (charCode) => {
if (0x0020 < charCode && charCode < 0x007F) {
return 0xFF00 + (charCode - 0x0020);
}
if (0x0020 === charCode) {
return 0x3000;
}
return charCode;
};
const toHalfWidth = (charCode) => {
if (0xFF00 < charCode && charCode < 0xFF5F) {
return 0x0020 + (charCode - 0xFF00);
}
if (0x3000 === charCode) {
return 0x0020;
}
return charCode;
};
const process = (str, charProcess) => {
let ret = [];
for (let char of str) {
ret.push(charProcess(char.charCodeAt()));
}
return String.fromCharCode.apply(String, ret);
};
const factory = (charProcessor) => {
return (str) => {
return process(str, charProcessor);
}
}
module.exports = function (options) {
this.toFullWidth = factory(toFullWidth);
this.toHalfWidth = factory(toHalfWidth);
};