@ddunigma/node 1.0.10 → 1.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.js +44 -27
- package/dist/cjs/test/test1.js +2 -2
- package/dist/index.d.ts +16 -4
- package/dist/mjs/index.js +45 -28
- package/dist/mjs/test/test1.js +2 -2
- package/package.json +1 -1
package/dist/cjs/index.js
CHANGED
|
@@ -3,7 +3,16 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.Ddu64 = void 0;
|
|
4
4
|
class Ddu64 {
|
|
5
5
|
constructor(dduChar, paddingChar) {
|
|
6
|
-
this.dduCharKr = [
|
|
6
|
+
this.dduCharKr = [
|
|
7
|
+
"뜌",
|
|
8
|
+
"땨",
|
|
9
|
+
"이",
|
|
10
|
+
"우",
|
|
11
|
+
"야",
|
|
12
|
+
"!",
|
|
13
|
+
"?",
|
|
14
|
+
".",
|
|
15
|
+
];
|
|
7
16
|
this.paddingCharKr = "뭐";
|
|
8
17
|
this.defaultEncoding = "utf-8";
|
|
9
18
|
this.dduChar = dduChar || this.dduCharKr;
|
|
@@ -14,7 +23,7 @@ class Ddu64 {
|
|
|
14
23
|
this.dduBinaryLookupKr = new Map();
|
|
15
24
|
this.paddingRegex = new Map();
|
|
16
25
|
for (let i = 0; i < 256; i++) {
|
|
17
|
-
this.binaryLookup[i] = i.toString(2).padStart(8,
|
|
26
|
+
this.binaryLookup[i] = i.toString(2).padStart(8, "0");
|
|
18
27
|
}
|
|
19
28
|
this.dduChar.forEach((char, index) => {
|
|
20
29
|
this.dduBinaryLookup.set(char, index);
|
|
@@ -22,8 +31,8 @@ class Ddu64 {
|
|
|
22
31
|
this.dduCharKr.forEach((char, index) => {
|
|
23
32
|
this.dduBinaryLookupKr.set(char, index);
|
|
24
33
|
});
|
|
25
|
-
this.paddingRegex.set(
|
|
26
|
-
this.paddingRegex.set(
|
|
34
|
+
this.paddingRegex.set("default", new RegExp(this.paddingChar, "g"));
|
|
35
|
+
this.paddingRegex.set("KR", new RegExp(this.paddingCharKr, "g"));
|
|
27
36
|
}
|
|
28
37
|
getLargestPowerOfTwo(n) {
|
|
29
38
|
let power = Math.floor(Math.log2(n));
|
|
@@ -43,15 +52,15 @@ class Ddu64 {
|
|
|
43
52
|
yield s.slice(i, Math.min(i + length, len));
|
|
44
53
|
}
|
|
45
54
|
}
|
|
46
|
-
getSelectedSets(
|
|
47
|
-
if (
|
|
55
|
+
getSelectedSets(dduSetSymbol) {
|
|
56
|
+
if (dduSetSymbol === "KR") {
|
|
48
57
|
return {
|
|
49
58
|
dduSet: this.dduCharKr,
|
|
50
59
|
padChar: this.paddingCharKr,
|
|
51
60
|
dduLength: this.dduCharKr.length,
|
|
52
61
|
bitLength: this.getBitLength(this.dduCharKr.length),
|
|
53
62
|
lookupTable: this.dduBinaryLookupKr,
|
|
54
|
-
paddingRegExp: this.paddingRegex.get(
|
|
63
|
+
paddingRegExp: this.paddingRegex.get("KR"),
|
|
55
64
|
};
|
|
56
65
|
}
|
|
57
66
|
return {
|
|
@@ -60,7 +69,7 @@ class Ddu64 {
|
|
|
60
69
|
dduLength: this.dduChar.length,
|
|
61
70
|
bitLength: this.getBitLength(this.dduChar.length),
|
|
62
71
|
lookupTable: this.dduBinaryLookup,
|
|
63
|
-
paddingRegExp: this.paddingRegex.get(
|
|
72
|
+
paddingRegExp: this.paddingRegex.get("default"),
|
|
64
73
|
};
|
|
65
74
|
}
|
|
66
75
|
getSelectedSets64(option) {
|
|
@@ -70,14 +79,14 @@ class Ddu64 {
|
|
|
70
79
|
}
|
|
71
80
|
bufferToDduBinary(input, bitLength) {
|
|
72
81
|
const bufferLength = input.length;
|
|
73
|
-
let encodedBin =
|
|
82
|
+
let encodedBin = "";
|
|
74
83
|
for (let i = 0; i < bufferLength; i++) {
|
|
75
84
|
encodedBin += this.binaryLookup[input[i]];
|
|
76
85
|
}
|
|
77
86
|
const dduBinary = Array.from(this.splitString(encodedBin, bitLength));
|
|
78
87
|
const padding = bitLength - dduBinary[dduBinary.length - 1].length;
|
|
79
88
|
if (padding > 0) {
|
|
80
|
-
dduBinary[dduBinary.length - 1] +=
|
|
89
|
+
dduBinary[dduBinary.length - 1] += "0".repeat(padding);
|
|
81
90
|
}
|
|
82
91
|
return { dduBinary, padding };
|
|
83
92
|
}
|
|
@@ -94,9 +103,11 @@ class Ddu64 {
|
|
|
94
103
|
}
|
|
95
104
|
return buffer;
|
|
96
105
|
}
|
|
97
|
-
encode(input,
|
|
98
|
-
|
|
99
|
-
|
|
106
|
+
encode(input, options = {}) {
|
|
107
|
+
options.dduSetSymbol = options.dduSetSymbol || "default";
|
|
108
|
+
options.encoding = options.encoding || this.defaultEncoding;
|
|
109
|
+
const bufferInput = typeof input === "string" ? Buffer.from(input, options.encoding) : input;
|
|
110
|
+
const { dduSet, padChar, dduLength, bitLength } = this.getSelectedSets(options.dduSetSymbol);
|
|
100
111
|
const { dduBinary, padding } = this.bufferToDduBinary(bufferInput, bitLength);
|
|
101
112
|
let resultString = "";
|
|
102
113
|
for (const char of dduBinary) {
|
|
@@ -110,9 +121,11 @@ class Ddu64 {
|
|
|
110
121
|
}
|
|
111
122
|
return resultString;
|
|
112
123
|
}
|
|
113
|
-
encode64(input,
|
|
114
|
-
|
|
115
|
-
|
|
124
|
+
encode64(input, options = {}) {
|
|
125
|
+
options.dduSetSymbol = options.dduSetSymbol || "default";
|
|
126
|
+
options.encoding = options.encoding || this.defaultEncoding;
|
|
127
|
+
const bufferInput = typeof input === "string" ? Buffer.from(input, options.encoding) : input;
|
|
128
|
+
const { dduSet, padChar, bitLength } = this.getSelectedSets64(options.dduSetSymbol);
|
|
116
129
|
const { dduBinary, padding } = this.bufferToDduBinary(bufferInput, bitLength);
|
|
117
130
|
let resultString = "";
|
|
118
131
|
for (const char of dduBinary) {
|
|
@@ -124,10 +137,12 @@ class Ddu64 {
|
|
|
124
137
|
}
|
|
125
138
|
return resultString;
|
|
126
139
|
}
|
|
127
|
-
decode(input,
|
|
128
|
-
|
|
140
|
+
decode(input, options = {}) {
|
|
141
|
+
options.dduSetSymbol = options.dduSetSymbol || "default";
|
|
142
|
+
options.encoding = options.encoding || this.defaultEncoding;
|
|
143
|
+
const { dduSet, dduLength, bitLength, lookupTable, paddingRegExp } = this.getSelectedSets(options.dduSetSymbol);
|
|
129
144
|
const paddingCount = (input.match(paddingRegExp) || []).length;
|
|
130
|
-
input = input.replace(paddingRegExp,
|
|
145
|
+
input = input.replace(paddingRegExp, "");
|
|
131
146
|
let dduBinary = "";
|
|
132
147
|
for (let i = 0; i < input.length; i += 2) {
|
|
133
148
|
const firstIndex = lookupTable.get(input[i]);
|
|
@@ -135,24 +150,26 @@ class Ddu64 {
|
|
|
135
150
|
if (firstIndex === undefined || secondIndex === undefined)
|
|
136
151
|
continue;
|
|
137
152
|
const value = firstIndex * dduLength + secondIndex;
|
|
138
|
-
dduBinary += value.toString(2).padStart(bitLength,
|
|
153
|
+
dduBinary += value.toString(2).padStart(bitLength, "0");
|
|
139
154
|
}
|
|
140
155
|
const decoded = this.dduBinaryToBuffer(dduBinary, paddingCount);
|
|
141
|
-
return Buffer.from(decoded).toString(encoding);
|
|
156
|
+
return Buffer.from(decoded).toString(options.encoding);
|
|
142
157
|
}
|
|
143
|
-
decode64(input,
|
|
144
|
-
|
|
158
|
+
decode64(input, options = {}) {
|
|
159
|
+
options.dduSetSymbol = options.dduSetSymbol || "default";
|
|
160
|
+
options.encoding = options.encoding || this.defaultEncoding;
|
|
161
|
+
const { dduSet, bitLength, lookupTable, paddingRegExp } = this.getSelectedSets64(options.dduSetSymbol);
|
|
145
162
|
const paddingCount = (input.match(paddingRegExp) || []).length;
|
|
146
|
-
input = input.replace(paddingRegExp,
|
|
147
|
-
let dduBinary =
|
|
163
|
+
input = input.replace(paddingRegExp, "");
|
|
164
|
+
let dduBinary = "";
|
|
148
165
|
for (let i = 0; i < input.length; i++) {
|
|
149
166
|
const charIndex = lookupTable.get(input[i]);
|
|
150
167
|
if (charIndex === undefined)
|
|
151
168
|
continue;
|
|
152
|
-
dduBinary += charIndex.toString(2).padStart(bitLength,
|
|
169
|
+
dduBinary += charIndex.toString(2).padStart(bitLength, "0");
|
|
153
170
|
}
|
|
154
171
|
const decoded = this.dduBinaryToBuffer(dduBinary, paddingCount);
|
|
155
|
-
return Buffer.from(decoded).toString(encoding);
|
|
172
|
+
return Buffer.from(decoded).toString(options.encoding);
|
|
156
173
|
}
|
|
157
174
|
}
|
|
158
175
|
exports.Ddu64 = Ddu64;
|
package/dist/cjs/test/test1.js
CHANGED
|
@@ -31,9 +31,9 @@ console.log(encoded);
|
|
|
31
31
|
const decoded = ddu64.decode64(encoded);
|
|
32
32
|
console.log(decoded);
|
|
33
33
|
console.log("=-==");
|
|
34
|
-
const encoded_eng = ddu64.encode(answer, "KR");
|
|
34
|
+
const encoded_eng = ddu64.encode(answer, { dduSetSymbol: "KR" });
|
|
35
35
|
console.log(encoded_eng);
|
|
36
|
-
const decoded_eng = ddu64.decode(encoded_eng, "KR");
|
|
36
|
+
const decoded_eng = ddu64.decode(encoded_eng, { dduSetSymbol: "KR" });
|
|
37
37
|
console.log(decoded_eng);
|
|
38
38
|
console.log("=-==");
|
|
39
39
|
const ddu64_other = new __1.Ddu64(koreanChars, "즁");
|
package/dist/index.d.ts
CHANGED
|
@@ -17,8 +17,20 @@ export declare class Ddu64 {
|
|
|
17
17
|
private getSelectedSets64;
|
|
18
18
|
private bufferToDduBinary;
|
|
19
19
|
private dduBinaryToBuffer;
|
|
20
|
-
encode(input: Buffer | string,
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
20
|
+
encode(input: Buffer | string, options?: {
|
|
21
|
+
dduSetSymbol?: string;
|
|
22
|
+
encoding?: BufferEncoding;
|
|
23
|
+
}): string;
|
|
24
|
+
encode64(input: Buffer | string, options?: {
|
|
25
|
+
dduSetSymbol?: string;
|
|
26
|
+
encoding?: BufferEncoding;
|
|
27
|
+
}): string;
|
|
28
|
+
decode(input: string, options?: {
|
|
29
|
+
dduSetSymbol?: string;
|
|
30
|
+
encoding?: BufferEncoding;
|
|
31
|
+
}): string;
|
|
32
|
+
decode64(input: string, options?: {
|
|
33
|
+
dduSetSymbol?: string;
|
|
34
|
+
encoding?: BufferEncoding;
|
|
35
|
+
}): string;
|
|
24
36
|
}
|
package/dist/mjs/index.js
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
1
1
|
export class Ddu64 {
|
|
2
2
|
dduChar;
|
|
3
3
|
paddingChar;
|
|
4
|
-
dduCharKr = [
|
|
4
|
+
dduCharKr = [
|
|
5
|
+
"뜌",
|
|
6
|
+
"땨",
|
|
7
|
+
"이",
|
|
8
|
+
"우",
|
|
9
|
+
"야",
|
|
10
|
+
"!",
|
|
11
|
+
"?",
|
|
12
|
+
".",
|
|
13
|
+
];
|
|
5
14
|
paddingCharKr = "뭐";
|
|
6
15
|
defaultEncoding = "utf-8";
|
|
7
16
|
bitLengthMap;
|
|
@@ -18,7 +27,7 @@ export class Ddu64 {
|
|
|
18
27
|
this.dduBinaryLookupKr = new Map();
|
|
19
28
|
this.paddingRegex = new Map();
|
|
20
29
|
for (let i = 0; i < 256; i++) {
|
|
21
|
-
this.binaryLookup[i] = i.toString(2).padStart(8,
|
|
30
|
+
this.binaryLookup[i] = i.toString(2).padStart(8, "0");
|
|
22
31
|
}
|
|
23
32
|
this.dduChar.forEach((char, index) => {
|
|
24
33
|
this.dduBinaryLookup.set(char, index);
|
|
@@ -26,8 +35,8 @@ export class Ddu64 {
|
|
|
26
35
|
this.dduCharKr.forEach((char, index) => {
|
|
27
36
|
this.dduBinaryLookupKr.set(char, index);
|
|
28
37
|
});
|
|
29
|
-
this.paddingRegex.set(
|
|
30
|
-
this.paddingRegex.set(
|
|
38
|
+
this.paddingRegex.set("default", new RegExp(this.paddingChar, "g"));
|
|
39
|
+
this.paddingRegex.set("KR", new RegExp(this.paddingCharKr, "g"));
|
|
31
40
|
}
|
|
32
41
|
getLargestPowerOfTwo(n) {
|
|
33
42
|
let power = Math.floor(Math.log2(n));
|
|
@@ -47,15 +56,15 @@ export class Ddu64 {
|
|
|
47
56
|
yield s.slice(i, Math.min(i + length, len));
|
|
48
57
|
}
|
|
49
58
|
}
|
|
50
|
-
getSelectedSets(
|
|
51
|
-
if (
|
|
59
|
+
getSelectedSets(dduSetSymbol) {
|
|
60
|
+
if (dduSetSymbol === "KR") {
|
|
52
61
|
return {
|
|
53
62
|
dduSet: this.dduCharKr,
|
|
54
63
|
padChar: this.paddingCharKr,
|
|
55
64
|
dduLength: this.dduCharKr.length,
|
|
56
65
|
bitLength: this.getBitLength(this.dduCharKr.length),
|
|
57
66
|
lookupTable: this.dduBinaryLookupKr,
|
|
58
|
-
paddingRegExp: this.paddingRegex.get(
|
|
67
|
+
paddingRegExp: this.paddingRegex.get("KR"),
|
|
59
68
|
};
|
|
60
69
|
}
|
|
61
70
|
return {
|
|
@@ -64,7 +73,7 @@ export class Ddu64 {
|
|
|
64
73
|
dduLength: this.dduChar.length,
|
|
65
74
|
bitLength: this.getBitLength(this.dduChar.length),
|
|
66
75
|
lookupTable: this.dduBinaryLookup,
|
|
67
|
-
paddingRegExp: this.paddingRegex.get(
|
|
76
|
+
paddingRegExp: this.paddingRegex.get("default"),
|
|
68
77
|
};
|
|
69
78
|
}
|
|
70
79
|
getSelectedSets64(option) {
|
|
@@ -74,19 +83,19 @@ export class Ddu64 {
|
|
|
74
83
|
...baseSet,
|
|
75
84
|
dduSet: baseSet.dduSet.slice(0, powerOfTwoLength),
|
|
76
85
|
dduLength: powerOfTwoLength,
|
|
77
|
-
bitLength: Math.log2(powerOfTwoLength)
|
|
86
|
+
bitLength: Math.log2(powerOfTwoLength),
|
|
78
87
|
};
|
|
79
88
|
}
|
|
80
89
|
bufferToDduBinary(input, bitLength) {
|
|
81
90
|
const bufferLength = input.length;
|
|
82
|
-
let encodedBin =
|
|
91
|
+
let encodedBin = "";
|
|
83
92
|
for (let i = 0; i < bufferLength; i++) {
|
|
84
93
|
encodedBin += this.binaryLookup[input[i]];
|
|
85
94
|
}
|
|
86
95
|
const dduBinary = Array.from(this.splitString(encodedBin, bitLength));
|
|
87
96
|
const padding = bitLength - dduBinary[dduBinary.length - 1].length;
|
|
88
97
|
if (padding > 0) {
|
|
89
|
-
dduBinary[dduBinary.length - 1] +=
|
|
98
|
+
dduBinary[dduBinary.length - 1] += "0".repeat(padding);
|
|
90
99
|
}
|
|
91
100
|
return { dduBinary, padding };
|
|
92
101
|
}
|
|
@@ -103,9 +112,11 @@ export class Ddu64 {
|
|
|
103
112
|
}
|
|
104
113
|
return buffer;
|
|
105
114
|
}
|
|
106
|
-
encode(input,
|
|
107
|
-
|
|
108
|
-
|
|
115
|
+
encode(input, options = {}) {
|
|
116
|
+
options.dduSetSymbol = options.dduSetSymbol || "default";
|
|
117
|
+
options.encoding = options.encoding || this.defaultEncoding;
|
|
118
|
+
const bufferInput = typeof input === "string" ? Buffer.from(input, options.encoding) : input;
|
|
119
|
+
const { dduSet, padChar, dduLength, bitLength } = this.getSelectedSets(options.dduSetSymbol);
|
|
109
120
|
const { dduBinary, padding } = this.bufferToDduBinary(bufferInput, bitLength);
|
|
110
121
|
let resultString = "";
|
|
111
122
|
for (const char of dduBinary) {
|
|
@@ -119,9 +130,11 @@ export class Ddu64 {
|
|
|
119
130
|
}
|
|
120
131
|
return resultString;
|
|
121
132
|
}
|
|
122
|
-
encode64(input,
|
|
123
|
-
|
|
124
|
-
|
|
133
|
+
encode64(input, options = {}) {
|
|
134
|
+
options.dduSetSymbol = options.dduSetSymbol || "default";
|
|
135
|
+
options.encoding = options.encoding || this.defaultEncoding;
|
|
136
|
+
const bufferInput = typeof input === "string" ? Buffer.from(input, options.encoding) : input;
|
|
137
|
+
const { dduSet, padChar, bitLength } = this.getSelectedSets64(options.dduSetSymbol);
|
|
125
138
|
const { dduBinary, padding } = this.bufferToDduBinary(bufferInput, bitLength);
|
|
126
139
|
let resultString = "";
|
|
127
140
|
for (const char of dduBinary) {
|
|
@@ -133,10 +146,12 @@ export class Ddu64 {
|
|
|
133
146
|
}
|
|
134
147
|
return resultString;
|
|
135
148
|
}
|
|
136
|
-
decode(input,
|
|
137
|
-
|
|
149
|
+
decode(input, options = {}) {
|
|
150
|
+
options.dduSetSymbol = options.dduSetSymbol || "default";
|
|
151
|
+
options.encoding = options.encoding || this.defaultEncoding;
|
|
152
|
+
const { dduSet, dduLength, bitLength, lookupTable, paddingRegExp } = this.getSelectedSets(options.dduSetSymbol);
|
|
138
153
|
const paddingCount = (input.match(paddingRegExp) || []).length;
|
|
139
|
-
input = input.replace(paddingRegExp,
|
|
154
|
+
input = input.replace(paddingRegExp, "");
|
|
140
155
|
let dduBinary = "";
|
|
141
156
|
for (let i = 0; i < input.length; i += 2) {
|
|
142
157
|
const firstIndex = lookupTable.get(input[i]);
|
|
@@ -144,23 +159,25 @@ export class Ddu64 {
|
|
|
144
159
|
if (firstIndex === undefined || secondIndex === undefined)
|
|
145
160
|
continue;
|
|
146
161
|
const value = firstIndex * dduLength + secondIndex;
|
|
147
|
-
dduBinary += value.toString(2).padStart(bitLength,
|
|
162
|
+
dduBinary += value.toString(2).padStart(bitLength, "0");
|
|
148
163
|
}
|
|
149
164
|
const decoded = this.dduBinaryToBuffer(dduBinary, paddingCount);
|
|
150
|
-
return Buffer.from(decoded).toString(encoding);
|
|
165
|
+
return Buffer.from(decoded).toString(options.encoding);
|
|
151
166
|
}
|
|
152
|
-
decode64(input,
|
|
153
|
-
|
|
167
|
+
decode64(input, options = {}) {
|
|
168
|
+
options.dduSetSymbol = options.dduSetSymbol || "default";
|
|
169
|
+
options.encoding = options.encoding || this.defaultEncoding;
|
|
170
|
+
const { dduSet, bitLength, lookupTable, paddingRegExp } = this.getSelectedSets64(options.dduSetSymbol);
|
|
154
171
|
const paddingCount = (input.match(paddingRegExp) || []).length;
|
|
155
|
-
input = input.replace(paddingRegExp,
|
|
156
|
-
let dduBinary =
|
|
172
|
+
input = input.replace(paddingRegExp, "");
|
|
173
|
+
let dduBinary = "";
|
|
157
174
|
for (let i = 0; i < input.length; i++) {
|
|
158
175
|
const charIndex = lookupTable.get(input[i]);
|
|
159
176
|
if (charIndex === undefined)
|
|
160
177
|
continue;
|
|
161
|
-
dduBinary += charIndex.toString(2).padStart(bitLength,
|
|
178
|
+
dduBinary += charIndex.toString(2).padStart(bitLength, "0");
|
|
162
179
|
}
|
|
163
180
|
const decoded = this.dduBinaryToBuffer(dduBinary, paddingCount);
|
|
164
|
-
return Buffer.from(decoded).toString(encoding);
|
|
181
|
+
return Buffer.from(decoded).toString(options.encoding);
|
|
165
182
|
}
|
|
166
183
|
}
|
package/dist/mjs/test/test1.js
CHANGED
|
@@ -29,9 +29,9 @@ console.log(encoded);
|
|
|
29
29
|
const decoded = ddu64.decode64(encoded);
|
|
30
30
|
console.log(decoded);
|
|
31
31
|
console.log("=-==");
|
|
32
|
-
const encoded_eng = ddu64.encode(answer, "KR");
|
|
32
|
+
const encoded_eng = ddu64.encode(answer, { dduSetSymbol: "KR" });
|
|
33
33
|
console.log(encoded_eng);
|
|
34
|
-
const decoded_eng = ddu64.decode(encoded_eng, "KR");
|
|
34
|
+
const decoded_eng = ddu64.decode(encoded_eng, { dduSetSymbol: "KR" });
|
|
35
35
|
console.log(decoded_eng);
|
|
36
36
|
console.log("=-==");
|
|
37
37
|
const ddu64_other = new Ddu64(koreanChars, "즁");
|