@ddunigma/node 1.0.26 → 1.1.0
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/README.md +4 -0
- package/dist/cjs/base/BaseDdu.d.ts +38 -0
- package/dist/cjs/base/BaseDdu.js +78 -0
- package/dist/cjs/base/index.d.ts +1 -0
- package/dist/cjs/base/index.js +5 -0
- package/dist/cjs/charSets/index.d.ts +4 -0
- package/dist/cjs/charSets/index.js +4341 -0
- package/dist/cjs/encoders/Ddu64.d.ts +49 -0
- package/dist/cjs/encoders/Ddu64.js +355 -0
- package/dist/cjs/encoders/index.d.ts +1 -0
- package/dist/cjs/encoders/index.js +6 -0
- package/dist/cjs/index.d.ts +4 -58
- package/dist/cjs/index.js +10 -329
- package/dist/cjs/types/DduDefaultTypes.d.ts +3 -0
- package/dist/cjs/types/DduDefaultTypes.js +12 -0
- package/dist/cjs/types/DduEnums.d.ts +6 -0
- package/dist/cjs/types/DduEnums.js +10 -0
- package/dist/cjs/types/DduInterface.d.ts +24 -0
- package/dist/cjs/types/DduInterface.js +2 -0
- package/dist/cjs/types/index.d.ts +3 -0
- package/dist/cjs/types/index.js +19 -0
- package/dist/mjs/base/BaseDdu.d.ts +38 -0
- package/dist/mjs/base/BaseDdu.js +72 -0
- package/dist/mjs/base/index.d.ts +1 -0
- package/dist/mjs/base/index.js +1 -0
- package/dist/mjs/charSets/index.d.ts +4 -0
- package/dist/mjs/charSets/index.js +4336 -0
- package/dist/mjs/encoders/Ddu64.d.ts +49 -0
- package/dist/mjs/encoders/Ddu64.js +358 -0
- package/dist/mjs/encoders/index.d.ts +1 -0
- package/dist/mjs/encoders/index.js +2 -0
- package/dist/mjs/index.d.ts +4 -58
- package/dist/mjs/index.js +6 -330
- package/dist/mjs/types/DduDefaultTypes.d.ts +3 -0
- package/dist/mjs/types/DduDefaultTypes.js +9 -0
- package/dist/mjs/types/DduEnums.d.ts +6 -0
- package/dist/mjs/types/DduEnums.js +7 -0
- package/dist/mjs/types/DduInterface.d.ts +24 -0
- package/dist/mjs/types/index.d.ts +3 -0
- package/dist/mjs/types/index.js +3 -0
- package/package.json +3 -3
- package/dist/cjs/test/test2.js +0 -44
- package/dist/cjs/test/test3.d.ts +0 -1
- package/dist/cjs/test/test3.js +0 -20
- package/dist/cjs/test/tester2.d.ts +0 -1
- package/dist/cjs/test/tester2.js +0 -46
- package/dist/mjs/test/test2.d.ts +0 -1
- package/dist/mjs/test/test2.js +0 -42
- package/dist/mjs/test/test3.d.ts +0 -1
- package/dist/mjs/test/test3.js +0 -18
- package/dist/mjs/test/tester2.d.ts +0 -1
- package/dist/mjs/test/tester2.js +0 -44
- /package/dist/{cjs/test/test2.d.ts → mjs/types/DduInterface.js} +0 -0
package/dist/cjs/index.js
CHANGED
|
@@ -1,331 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
"?",
|
|
14
|
-
".",
|
|
15
|
-
];
|
|
16
|
-
this.paddingCharKr = "뭐";
|
|
17
|
-
this.defaultEncoding = "utf-8";
|
|
18
|
-
this.bitLengthMap = new Map();
|
|
19
|
-
this.binaryLookup = Array.from({ length: 256 }, (_, i) => i.toString(2).padStart(8, "0"));
|
|
20
|
-
this.dduBinaryLookup = new Map();
|
|
21
|
-
this.dduBinaryLookupKr = new Map();
|
|
22
|
-
this.paddingRegex = new Map();
|
|
23
|
-
if (typeof dduChar === "string") {
|
|
24
|
-
const removeDuplicatesString = [...new Set(dduChar.trim())].join("");
|
|
25
|
-
this.dduChar = removeDuplicatesString.trim().split("");
|
|
26
|
-
}
|
|
27
|
-
else {
|
|
28
|
-
this.dduChar = dduChar || this.dduCharKr;
|
|
29
|
-
}
|
|
30
|
-
this.paddingChar = paddingChar || this.paddingCharKr;
|
|
31
|
-
this.dduChar.forEach((char, index) => this.dduBinaryLookup.set(char, index));
|
|
32
|
-
this.dduCharKr.forEach((char, index) => this.dduBinaryLookupKr.set(char, index));
|
|
33
|
-
this.paddingRegex.set("default", new RegExp(this.paddingChar, "g"));
|
|
34
|
-
this.paddingRegex.set("KR", new RegExp(this.paddingCharKr, "g"));
|
|
35
|
-
}
|
|
36
|
-
getBitLength(setLength) {
|
|
37
|
-
let cached = this.bitLengthMap.get(setLength);
|
|
38
|
-
if (cached === undefined) {
|
|
39
|
-
cached = Math.ceil(Math.log2(setLength));
|
|
40
|
-
this.bitLengthMap.set(setLength, cached);
|
|
41
|
-
}
|
|
42
|
-
return cached;
|
|
43
|
-
}
|
|
44
|
-
getLargestPowerOfTwo(n) {
|
|
45
|
-
return Math.pow(2, Math.floor(Math.log2(n)));
|
|
46
|
-
}
|
|
47
|
-
*splitString(s, length) {
|
|
48
|
-
for (let i = 0; i < s.length; i += length) {
|
|
49
|
-
yield s.slice(i, Math.min(i + length, s.length));
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
getSelectedSets(dduSetSymbol, usePowerOfTwo) {
|
|
53
|
-
const isKR = dduSetSymbol === "KR";
|
|
54
|
-
const dduSet = isKR ? this.dduCharKr : this.dduChar;
|
|
55
|
-
const padChar = isKR ? this.paddingCharKr : this.paddingChar;
|
|
56
|
-
let dduLength = dduSet.length;
|
|
57
|
-
if (usePowerOfTwo) {
|
|
58
|
-
const powerOfTwoLength = this.getLargestPowerOfTwo(dduLength);
|
|
59
|
-
dduLength = powerOfTwoLength;
|
|
60
|
-
}
|
|
61
|
-
return {
|
|
62
|
-
dduSet: usePowerOfTwo ? dduSet.slice(0, dduLength) : dduSet,
|
|
63
|
-
padChar,
|
|
64
|
-
dduLength,
|
|
65
|
-
bitLength: this.getBitLength(dduLength),
|
|
66
|
-
lookupTable: isKR ? this.dduBinaryLookupKr : this.dduBinaryLookup,
|
|
67
|
-
paddingRegExp: this.paddingRegex.get(isKR ? "KR" : "default"),
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
bufferToDduBinary(input, bitLength) {
|
|
71
|
-
const encodedBin = input.reduce((acc, byte) => acc + this.binaryLookup[byte], "");
|
|
72
|
-
const dduBinary = Array.from(this.splitString(encodedBin, bitLength));
|
|
73
|
-
const padding = bitLength - dduBinary[dduBinary.length - 1].length;
|
|
74
|
-
if (padding > 0) {
|
|
75
|
-
dduBinary[dduBinary.length - 1] += "0".repeat(padding);
|
|
76
|
-
}
|
|
77
|
-
return { dduBinary, padding };
|
|
78
|
-
}
|
|
79
|
-
dduBinaryToBuffer(decodedBin, paddingBits) {
|
|
80
|
-
if (paddingBits > 0) {
|
|
81
|
-
decodedBin = decodedBin.slice(0, -paddingBits);
|
|
82
|
-
}
|
|
83
|
-
const buffer = [];
|
|
84
|
-
for (let i = 0; i < decodedBin.length; i += 8) {
|
|
85
|
-
buffer.push(parseInt(decodedBin.slice(i, i + 8), 2));
|
|
86
|
-
}
|
|
87
|
-
return Buffer.from(buffer);
|
|
88
|
-
}
|
|
89
|
-
encode(input, options = {}) {
|
|
90
|
-
const { dduSetSymbol = "default", encoding = this.defaultEncoding, usePowerOfTwo = true, } = options;
|
|
91
|
-
const bufferInput = typeof input === "string" ? Buffer.from(input, encoding) : input;
|
|
92
|
-
const { dduSet, padChar, dduLength, bitLength } = this.getSelectedSets(dduSetSymbol, usePowerOfTwo);
|
|
93
|
-
const { dduBinary, padding } = this.bufferToDduBinary(bufferInput, bitLength);
|
|
94
|
-
let resultString = "";
|
|
95
|
-
for (const binaryChunk of dduBinary) {
|
|
96
|
-
const charInt = parseInt(binaryChunk, 2);
|
|
97
|
-
if (usePowerOfTwo) {
|
|
98
|
-
resultString += dduSet[charInt];
|
|
99
|
-
}
|
|
100
|
-
else {
|
|
101
|
-
const quotient = Math.floor(charInt / dduLength);
|
|
102
|
-
const remainder = charInt % dduLength;
|
|
103
|
-
resultString += dduSet[quotient] + dduSet[remainder];
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
return padding > 0
|
|
107
|
-
? resultString + padChar.repeat(Math.floor(padding / 2))
|
|
108
|
-
: resultString;
|
|
109
|
-
}
|
|
110
|
-
decode(input, options = {}) {
|
|
111
|
-
const { dduSetSymbol = "default", encoding = this.defaultEncoding, usePowerOfTwo = true, } = options;
|
|
112
|
-
const { dduSet, dduLength, bitLength, lookupTable, paddingRegExp } = this.getSelectedSets(dduSetSymbol, usePowerOfTwo);
|
|
113
|
-
const paddingCount = (input.match(paddingRegExp) || []).length;
|
|
114
|
-
const paddingBits = paddingCount * 2;
|
|
115
|
-
input = input.replace(paddingRegExp, "");
|
|
116
|
-
let dduBinary = "";
|
|
117
|
-
for (let i = 0; i < input.length; i += usePowerOfTwo ? 1 : 2) {
|
|
118
|
-
const firstChar = input[i];
|
|
119
|
-
const secondChar = input[i + 1];
|
|
120
|
-
if (usePowerOfTwo) {
|
|
121
|
-
const charIndex = lookupTable.get(firstChar);
|
|
122
|
-
if (charIndex === undefined)
|
|
123
|
-
throw new Error(`Invalid character: ${firstChar}`);
|
|
124
|
-
dduBinary += charIndex.toString(2).padStart(bitLength, "0");
|
|
125
|
-
}
|
|
126
|
-
else {
|
|
127
|
-
const firstIndex = lookupTable.get(firstChar);
|
|
128
|
-
const secondIndex = lookupTable.get(secondChar);
|
|
129
|
-
if (firstIndex === undefined || secondIndex === undefined)
|
|
130
|
-
throw new Error(`Invalid character: ${firstChar} or ${secondChar}`);
|
|
131
|
-
const value = firstIndex * dduLength + secondIndex;
|
|
132
|
-
dduBinary += value.toString(2).padStart(bitLength, "0");
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
const decoded = this.dduBinaryToBuffer(dduBinary, usePowerOfTwo ? paddingBits : paddingCount * bitLength);
|
|
136
|
-
return decoded.toString(encoding);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
exports.Ddu64 = Ddu64;
|
|
140
|
-
class Custom64 {
|
|
141
|
-
constructor(dduChar, paddingChar) {
|
|
142
|
-
this.dduCharKr = [
|
|
143
|
-
"A",
|
|
144
|
-
"s",
|
|
145
|
-
"q",
|
|
146
|
-
"r",
|
|
147
|
-
"0",
|
|
148
|
-
"z",
|
|
149
|
-
"3",
|
|
150
|
-
"t",
|
|
151
|
-
"y",
|
|
152
|
-
"1",
|
|
153
|
-
"5",
|
|
154
|
-
"2",
|
|
155
|
-
"4",
|
|
156
|
-
"E",
|
|
157
|
-
"B",
|
|
158
|
-
"C",
|
|
159
|
-
"Q",
|
|
160
|
-
"F",
|
|
161
|
-
"R",
|
|
162
|
-
"T",
|
|
163
|
-
"U",
|
|
164
|
-
"W",
|
|
165
|
-
"V",
|
|
166
|
-
"X",
|
|
167
|
-
"Y",
|
|
168
|
-
"Z",
|
|
169
|
-
"b",
|
|
170
|
-
"a",
|
|
171
|
-
"c",
|
|
172
|
-
"d",
|
|
173
|
-
"D",
|
|
174
|
-
"G",
|
|
175
|
-
"L",
|
|
176
|
-
"H",
|
|
177
|
-
"I",
|
|
178
|
-
"+",
|
|
179
|
-
"J",
|
|
180
|
-
"K",
|
|
181
|
-
"M",
|
|
182
|
-
"O",
|
|
183
|
-
"N",
|
|
184
|
-
"f",
|
|
185
|
-
"e",
|
|
186
|
-
"h",
|
|
187
|
-
"g",
|
|
188
|
-
"P",
|
|
189
|
-
"i",
|
|
190
|
-
"S",
|
|
191
|
-
"k",
|
|
192
|
-
"l",
|
|
193
|
-
"m",
|
|
194
|
-
"u",
|
|
195
|
-
"j",
|
|
196
|
-
"v",
|
|
197
|
-
"n",
|
|
198
|
-
"o",
|
|
199
|
-
"p",
|
|
200
|
-
"9",
|
|
201
|
-
"w",
|
|
202
|
-
"6",
|
|
203
|
-
"7",
|
|
204
|
-
"8",
|
|
205
|
-
"x",
|
|
206
|
-
"/",
|
|
207
|
-
];
|
|
208
|
-
this.paddingCharKr = "=";
|
|
209
|
-
this.defaultEncoding = "utf-8";
|
|
210
|
-
this.bitLengthMap = new Map();
|
|
211
|
-
this.binaryLookup = Array.from({ length: 256 }, (_, i) => i.toString(2).padStart(8, "0"));
|
|
212
|
-
this.dduBinaryLookup = new Map();
|
|
213
|
-
this.dduBinaryLookupKr = new Map();
|
|
214
|
-
this.paddingRegex = new Map();
|
|
215
|
-
if (typeof dduChar === "string") {
|
|
216
|
-
const removeDuplicatesString = [...new Set(dduChar.trim())].join("");
|
|
217
|
-
this.dduChar = removeDuplicatesString.trim().split("");
|
|
218
|
-
}
|
|
219
|
-
else {
|
|
220
|
-
this.dduChar = dduChar || this.dduCharKr;
|
|
221
|
-
}
|
|
222
|
-
this.paddingChar = paddingChar || this.paddingCharKr;
|
|
223
|
-
this.dduChar.forEach((char, index) => this.dduBinaryLookup.set(char, index));
|
|
224
|
-
this.dduCharKr.forEach((char, index) => this.dduBinaryLookupKr.set(char, index));
|
|
225
|
-
this.paddingRegex.set("default", new RegExp(this.paddingChar, "g"));
|
|
226
|
-
this.paddingRegex.set("KR", new RegExp(this.paddingCharKr, "g"));
|
|
227
|
-
}
|
|
228
|
-
getBitLength(setLength) {
|
|
229
|
-
let cached = this.bitLengthMap.get(setLength);
|
|
230
|
-
if (cached === undefined) {
|
|
231
|
-
cached = Math.ceil(Math.log2(setLength));
|
|
232
|
-
this.bitLengthMap.set(setLength, cached);
|
|
233
|
-
}
|
|
234
|
-
return cached;
|
|
235
|
-
}
|
|
236
|
-
getLargestPowerOfTwo(n) {
|
|
237
|
-
return Math.pow(2, Math.floor(Math.log2(n)));
|
|
238
|
-
}
|
|
239
|
-
*splitString(s, length) {
|
|
240
|
-
for (let i = 0; i < s.length; i += length) {
|
|
241
|
-
yield s.slice(i, Math.min(i + length, s.length));
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
getSelectedSets(dduSetSymbol, usePowerOfTwo) {
|
|
245
|
-
const isKR = dduSetSymbol === "KR";
|
|
246
|
-
const dduSet = isKR ? this.dduCharKr : this.dduChar;
|
|
247
|
-
const padChar = isKR ? this.paddingCharKr : this.paddingChar;
|
|
248
|
-
let dduLength = dduSet.length;
|
|
249
|
-
if (usePowerOfTwo) {
|
|
250
|
-
const powerOfTwoLength = this.getLargestPowerOfTwo(dduLength);
|
|
251
|
-
dduLength = powerOfTwoLength;
|
|
252
|
-
}
|
|
253
|
-
return {
|
|
254
|
-
dduSet: usePowerOfTwo ? dduSet.slice(0, dduLength) : dduSet,
|
|
255
|
-
padChar,
|
|
256
|
-
dduLength,
|
|
257
|
-
bitLength: this.getBitLength(dduLength),
|
|
258
|
-
lookupTable: isKR ? this.dduBinaryLookupKr : this.dduBinaryLookup,
|
|
259
|
-
paddingRegExp: this.paddingRegex.get(isKR ? "KR" : "default"),
|
|
260
|
-
};
|
|
261
|
-
}
|
|
262
|
-
bufferToDduBinary(input, bitLength) {
|
|
263
|
-
const encodedBin = input.reduce((acc, byte) => acc + this.binaryLookup[byte], "");
|
|
264
|
-
const dduBinary = Array.from(this.splitString(encodedBin, bitLength));
|
|
265
|
-
const padding = bitLength - dduBinary[dduBinary.length - 1].length;
|
|
266
|
-
if (padding > 0) {
|
|
267
|
-
dduBinary[dduBinary.length - 1] += "0".repeat(padding);
|
|
268
|
-
}
|
|
269
|
-
return { dduBinary, padding };
|
|
270
|
-
}
|
|
271
|
-
dduBinaryToBuffer(decodedBin, paddingBits) {
|
|
272
|
-
if (paddingBits > 0) {
|
|
273
|
-
decodedBin = decodedBin.slice(0, -paddingBits);
|
|
274
|
-
}
|
|
275
|
-
const buffer = [];
|
|
276
|
-
for (let i = 0; i < decodedBin.length; i += 8) {
|
|
277
|
-
buffer.push(parseInt(decodedBin.slice(i, i + 8), 2));
|
|
278
|
-
}
|
|
279
|
-
return Buffer.from(buffer);
|
|
280
|
-
}
|
|
281
|
-
encode(input, options = {}) {
|
|
282
|
-
const { dduSetSymbol = "default", encoding = this.defaultEncoding, usePowerOfTwo = true, } = options;
|
|
283
|
-
const bufferInput = typeof input === "string" ? Buffer.from(input, encoding) : input;
|
|
284
|
-
const { dduSet, padChar, dduLength, bitLength } = this.getSelectedSets(dduSetSymbol, usePowerOfTwo);
|
|
285
|
-
const { dduBinary, padding } = this.bufferToDduBinary(bufferInput, bitLength);
|
|
286
|
-
let resultString = "";
|
|
287
|
-
for (const binaryChunk of dduBinary) {
|
|
288
|
-
const charInt = parseInt(binaryChunk, 2);
|
|
289
|
-
if (usePowerOfTwo) {
|
|
290
|
-
resultString += dduSet[charInt];
|
|
291
|
-
}
|
|
292
|
-
else {
|
|
293
|
-
const quotient = Math.floor(charInt / dduLength);
|
|
294
|
-
const remainder = charInt % dduLength;
|
|
295
|
-
resultString += dduSet[quotient] + dduSet[remainder];
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
return padding > 0
|
|
299
|
-
? resultString + padChar.repeat(Math.floor(padding / 2))
|
|
300
|
-
: resultString;
|
|
301
|
-
}
|
|
302
|
-
decode(input, options = {}) {
|
|
303
|
-
const { dduSetSymbol = "default", encoding = this.defaultEncoding, usePowerOfTwo = true, } = options;
|
|
304
|
-
const { dduSet, dduLength, bitLength, lookupTable, paddingRegExp } = this.getSelectedSets(dduSetSymbol, usePowerOfTwo);
|
|
305
|
-
const paddingCount = (input.match(paddingRegExp) || []).length;
|
|
306
|
-
const paddingBits = paddingCount * 2;
|
|
307
|
-
input = input.replace(paddingRegExp, "");
|
|
308
|
-
let dduBinary = "";
|
|
309
|
-
for (let i = 0; i < input.length; i += usePowerOfTwo ? 1 : 2) {
|
|
310
|
-
const firstChar = input[i];
|
|
311
|
-
const secondChar = input[i + 1];
|
|
312
|
-
if (usePowerOfTwo) {
|
|
313
|
-
const charIndex = lookupTable.get(firstChar);
|
|
314
|
-
if (charIndex === undefined)
|
|
315
|
-
throw new Error(`Invalid character: ${firstChar}`);
|
|
316
|
-
dduBinary += charIndex.toString(2).padStart(bitLength, "0");
|
|
317
|
-
}
|
|
318
|
-
else {
|
|
319
|
-
const firstIndex = lookupTable.get(firstChar);
|
|
320
|
-
const secondIndex = lookupTable.get(secondChar);
|
|
321
|
-
if (firstIndex === undefined || secondIndex === undefined)
|
|
322
|
-
throw new Error(`Invalid character: ${firstChar} or ${secondChar}`);
|
|
323
|
-
const value = firstIndex * dduLength + secondIndex;
|
|
324
|
-
dduBinary += value.toString(2).padStart(bitLength, "0");
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
const decoded = this.dduBinaryToBuffer(dduBinary, usePowerOfTwo ? paddingBits : paddingCount * bitLength);
|
|
328
|
-
return decoded.toString(encoding);
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
exports.Custom64 = Custom64;
|
|
3
|
+
exports.Ddu64 = exports.BaseDdu = exports.DduSetSymbol = void 0;
|
|
4
|
+
// Export enums
|
|
5
|
+
var types_1 = require("./types");
|
|
6
|
+
Object.defineProperty(exports, "DduSetSymbol", { enumerable: true, get: function () { return types_1.DduSetSymbol; } });
|
|
7
|
+
// Export base classes
|
|
8
|
+
var base_1 = require("./base");
|
|
9
|
+
Object.defineProperty(exports, "BaseDdu", { enumerable: true, get: function () { return base_1.BaseDdu; } });
|
|
10
|
+
// Export encoders
|
|
11
|
+
var encoders_1 = require("./encoders");
|
|
12
|
+
Object.defineProperty(exports, "Ddu64", { enumerable: true, get: function () { return encoders_1.Ddu64; } });
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.dduDefaultConstructorOptions = void 0;
|
|
4
|
+
const DduEnums_1 = require("./DduEnums");
|
|
5
|
+
const dduDefaultConstructorOptions = {
|
|
6
|
+
dduSetSymbol: DduEnums_1.DduSetSymbol.DDU,
|
|
7
|
+
usePowerOfTwo: true,
|
|
8
|
+
requiredLength: 8,
|
|
9
|
+
bitLength: 3,
|
|
10
|
+
useBuildErrorReturn: false,
|
|
11
|
+
};
|
|
12
|
+
exports.dduDefaultConstructorOptions = dduDefaultConstructorOptions;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DduSetSymbol = void 0;
|
|
4
|
+
var DduSetSymbol;
|
|
5
|
+
(function (DduSetSymbol) {
|
|
6
|
+
DduSetSymbol["DDU"] = "ddu";
|
|
7
|
+
DduSetSymbol["ONECHARSET"] = "oneCharSet";
|
|
8
|
+
DduSetSymbol["TWOCHARSET"] = "twoCharSet";
|
|
9
|
+
DduSetSymbol["THREECHARSET"] = "threeCharSet";
|
|
10
|
+
})(DduSetSymbol || (exports.DduSetSymbol = DduSetSymbol = {}));
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { DduSetSymbol } from "./DduEnums";
|
|
2
|
+
export interface CharSetConfig {
|
|
3
|
+
symbol: DduSetSymbol;
|
|
4
|
+
charSet: string[];
|
|
5
|
+
maxRequiredLength: number;
|
|
6
|
+
bitLength: number;
|
|
7
|
+
paddingChar: string;
|
|
8
|
+
}
|
|
9
|
+
export interface DduOptions {
|
|
10
|
+
}
|
|
11
|
+
export interface DduConstructorOptions {
|
|
12
|
+
dduSetSymbol?: DduSetSymbol;
|
|
13
|
+
dduChar?: string[] | string;
|
|
14
|
+
paddingChar?: string;
|
|
15
|
+
requiredLength?: number;
|
|
16
|
+
bitLength?: number;
|
|
17
|
+
usePowerOfTwo?: boolean;
|
|
18
|
+
useBuildErrorReturn?: boolean;
|
|
19
|
+
encoding?: BufferEncoding;
|
|
20
|
+
}
|
|
21
|
+
export interface BufferToDduBinaryResult {
|
|
22
|
+
dduBinary: string[];
|
|
23
|
+
padding: number;
|
|
24
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./DduEnums"), exports);
|
|
18
|
+
__exportStar(require("./DduInterface"), exports);
|
|
19
|
+
__exportStar(require("./DduDefaultTypes"), exports);
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { DduOptions, BufferToDduBinaryResult } from "../types";
|
|
2
|
+
export declare abstract class BaseDdu {
|
|
3
|
+
protected readonly defaultEncoding: BufferEncoding;
|
|
4
|
+
/**
|
|
5
|
+
* 이진수 변환 룩업 테이블 (0-255 -> 8비트 이진 문자열)
|
|
6
|
+
*/
|
|
7
|
+
protected readonly binaryLookup: string[];
|
|
8
|
+
/**
|
|
9
|
+
* 정규표현식 특수문자 이스케이프
|
|
10
|
+
*/
|
|
11
|
+
protected escapeRegExp(str: string): string;
|
|
12
|
+
/**
|
|
13
|
+
* 문자열을 지정된 길이로 분할하는 제너레이터
|
|
14
|
+
*/
|
|
15
|
+
protected splitString(s: string, length: number): Generator<string>;
|
|
16
|
+
/**
|
|
17
|
+
* 2의 거듭제곱 길이 계산
|
|
18
|
+
*/
|
|
19
|
+
protected getLargestPowerOfTwo(n: number): number;
|
|
20
|
+
/**
|
|
21
|
+
* 2의 거듭제곱 지수 계산
|
|
22
|
+
*/
|
|
23
|
+
protected getLargestPowerOfTwoExponent(n: number): number;
|
|
24
|
+
/**
|
|
25
|
+
* 비트 길이 계산
|
|
26
|
+
*/
|
|
27
|
+
protected getBitLength(setLength: number): number;
|
|
28
|
+
/**
|
|
29
|
+
* Buffer를 DDU Binary 배열로 변환
|
|
30
|
+
*/
|
|
31
|
+
protected bufferToDduBinary(input: Buffer, bitLength: number): BufferToDduBinaryResult;
|
|
32
|
+
/**
|
|
33
|
+
* DDU Binary를 Buffer로 변환
|
|
34
|
+
*/
|
|
35
|
+
protected dduBinaryToBuffer(decodedBin: string, paddingBits: number): Buffer;
|
|
36
|
+
abstract encode(input: Buffer | string, options?: DduOptions): string;
|
|
37
|
+
abstract decode(input: string, options?: DduOptions): string;
|
|
38
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
export class BaseDdu {
|
|
2
|
+
defaultEncoding = "utf-8";
|
|
3
|
+
/**
|
|
4
|
+
* 이진수 변환 룩업 테이블 (0-255 -> 8비트 이진 문자열)
|
|
5
|
+
*/
|
|
6
|
+
binaryLookup = Array.from({ length: 256 }, (_, i) => i.toString(2).padStart(8, "0"));
|
|
7
|
+
/**
|
|
8
|
+
* 정규표현식 특수문자 이스케이프
|
|
9
|
+
*/
|
|
10
|
+
escapeRegExp(str) {
|
|
11
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* 문자열을 지정된 길이로 분할하는 제너레이터
|
|
15
|
+
*/
|
|
16
|
+
*splitString(s, length) {
|
|
17
|
+
for (let i = 0; i < s.length; i += length) {
|
|
18
|
+
yield s.slice(i, Math.min(i + length, s.length));
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* 2의 거듭제곱 길이 계산
|
|
23
|
+
*/
|
|
24
|
+
getLargestPowerOfTwo(n) {
|
|
25
|
+
return 2 ** Math.floor(Math.log2(n));
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* 2의 거듭제곱 지수 계산
|
|
29
|
+
*/
|
|
30
|
+
getLargestPowerOfTwoExponent(n) {
|
|
31
|
+
return Math.floor(Math.log2(n));
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* 비트 길이 계산
|
|
35
|
+
*/
|
|
36
|
+
getBitLength(setLength) {
|
|
37
|
+
return Math.ceil(Math.log2(setLength));
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Buffer를 DDU Binary 배열로 변환
|
|
41
|
+
*/
|
|
42
|
+
bufferToDduBinary(input, bitLength) {
|
|
43
|
+
// 성능 최적화: reduce 대신 for loop + join 사용
|
|
44
|
+
if (input.length === 0) {
|
|
45
|
+
return { dduBinary: [], padding: 0 };
|
|
46
|
+
}
|
|
47
|
+
const binaryParts = new Array(input.length);
|
|
48
|
+
for (let i = 0; i < input.length; i++) {
|
|
49
|
+
binaryParts[i] = this.binaryLookup[input[i]];
|
|
50
|
+
}
|
|
51
|
+
const encodedBin = binaryParts.join("");
|
|
52
|
+
const dduBinary = Array.from(this.splitString(encodedBin, bitLength));
|
|
53
|
+
const padding = bitLength - dduBinary[dduBinary.length - 1].length;
|
|
54
|
+
if (padding > 0) {
|
|
55
|
+
dduBinary[dduBinary.length - 1] += "0".repeat(padding);
|
|
56
|
+
}
|
|
57
|
+
return { dduBinary, padding };
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* DDU Binary를 Buffer로 변환
|
|
61
|
+
*/
|
|
62
|
+
dduBinaryToBuffer(decodedBin, paddingBits) {
|
|
63
|
+
if (paddingBits > 0) {
|
|
64
|
+
decodedBin = decodedBin.slice(0, -paddingBits);
|
|
65
|
+
}
|
|
66
|
+
const buffer = [];
|
|
67
|
+
for (let i = 0; i < decodedBin.length; i += 8) {
|
|
68
|
+
buffer.push(parseInt(decodedBin.slice(i, i + 8), 2));
|
|
69
|
+
}
|
|
70
|
+
return Buffer.from(buffer);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { BaseDdu } from "./BaseDdu";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { BaseDdu } from "./BaseDdu";
|