strc 1.0.0-a

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 JustDeveloper <https://justdeveloper.is-a.dev/>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,54 @@
1
+ ## JavaScript String Compressor
2
+ #### An UMD compression algorithm for strings in JavaScript.
3
+ Advanced string compression library supporting multiple languages and character encodings.
4
+ - 🗜️ High compression ratios (up to 8:1 for numbers)
5
+ - 🌍 Multilingual support (English, Russian, Japanese, Hindi, Bengali, and more)
6
+ - 🔧 Zero dependencies
7
+ - 📦 TypeScript support
8
+
9
+ ## Installation
10
+ Install via npm
11
+ ```
12
+ npm i strc
13
+ ```
14
+
15
+ Or you can use it on your website by inserting the following HTML `script` tag.
16
+ ```html
17
+ <script type="text/javascript" src="https://jssc.js.org/script.js"></script>
18
+ ```
19
+
20
+ ## Usage
21
+ #### JavaScript
22
+ ```js
23
+ const { compress, decompress } = require('strc');
24
+
25
+ const example = compress("Hello, world!");
26
+ decompress(example);
27
+ ```
28
+
29
+ #### TypeScript
30
+ ```ts
31
+ import { compress, decompress } from 'strc';
32
+
33
+ const example = compress("Hello, world!");
34
+ decompress(example);
35
+ ```
36
+
37
+ #### Browsers/Frontend (static websites)
38
+ ```html
39
+ <script type="text/javascript" src="https://jssc.js.org/script.js"></script>
40
+ ```
41
+ ```js
42
+ const compressed = JSSC.compress("Hello, world!");
43
+ const decompressed = JSSC.decompress(compressed);
44
+ ```
45
+
46
+ ## API
47
+ #### `compress(str: string): string`
48
+ Compresses a string and returns the compressed result.
49
+
50
+ #### `decompress(str: string): string`
51
+ Decompresses a previously compressed string.
52
+
53
+ ## License
54
+ [MIT © 2025 JustDeveloper](https://github.com/JustDeveloper1/JSSC/blob/main/LICENSE)
package/index.d.ts ADDED
@@ -0,0 +1,59 @@
1
+ /*
2
+
3
+ MIT License
4
+
5
+ Copyright (c) 2025 JustDeveloper <https://justdeveloper.is-a.dev/>
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in all
15
+ copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ SOFTWARE.
24
+
25
+ */
26
+
27
+ /**
28
+ * JavaScript String Compressor - compress function
29
+ * @param str - Input string to compress
30
+ * @returns Compressed string
31
+ * @example
32
+ * compress('Hello, World!');
33
+ * @since 1.0.0
34
+ */
35
+ export function compress(str: string): string;
36
+
37
+ /**
38
+ * JavaScript String Compressor - decompress function
39
+ * @param str - Compressed string to decompress
40
+ * @returns Decompressed string
41
+ * @example
42
+ * decompress(compressedString);
43
+ * @since 1.0.0
44
+ */
45
+ export function decompress(str: string): string;
46
+
47
+ /**
48
+ * JavaScript String Compressor
49
+ * @license MIT
50
+ * @copyright (c) 2025 JustDeveloper <<https://justdeveloper.is-a.dev>>
51
+ * @since 1.0.0
52
+ */
53
+ declare const JSSC: {
54
+ compress: typeof compress;
55
+ decompress: typeof decompress;
56
+ [Symbol.toStringTag]: 'JSSC';
57
+ };
58
+
59
+ export default JSSC;
package/index.js ADDED
@@ -0,0 +1,732 @@
1
+ /*
2
+
3
+ MIT License
4
+
5
+ Copyright (c) 2025 JustDeveloper <https://justdeveloper.is-a.dev/>
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in all
15
+ copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ SOFTWARE.
24
+
25
+ */
26
+
27
+ (function (root, factory) {
28
+ if (typeof define === 'function' && define.amd) {
29
+ define([], factory); /* amd */
30
+ } else if (typeof module === 'object' && module.exports) {
31
+ module.exports = factory(); /* node */
32
+ } else {
33
+ root.JSSC = factory(); /* browsers */
34
+ Object.freeze(root.JSSC);
35
+ }
36
+ }(typeof self !== 'undefined' ? self : this, function () {
37
+
38
+ if ((String.fromCharCode(65536).charCodeAt(0) === 65536) || !(String.fromCharCode(256).charCodeAt(0) === 256)) {
39
+ throw new Error('Supported UTF-16 only!')
40
+ }
41
+
42
+ function stringCodes(str) {
43
+ let output = [];
44
+ let max = 0;
45
+ let maxCharCode = 0;
46
+ let min = Infinity;
47
+ String(str).split('').forEach(char => {
48
+ const code = char.charCodeAt();
49
+ output.push(code);
50
+ max = Math.max(max, code.toString().length);
51
+ maxCharCode = Math.max(maxCharCode, code);
52
+ min = Math.min(min, code.toString().length);
53
+ });
54
+ return {max, output, maxCharCode, min}
55
+ }
56
+
57
+ function codesString(cds) {
58
+ let output = '';
59
+ cds.forEach(code => {
60
+ output += String.fromCharCode(code);
61
+ });
62
+ return output
63
+ }
64
+
65
+ function charCode(num) {
66
+ return String.fromCharCode(num + 32);
67
+ }
68
+ function checkChar(cde) {
69
+ return cde % 65535 === cde
70
+ }
71
+
72
+ function stringChunks(str, num) {
73
+ const output = [];
74
+ for (let i = 0; i < str.length; i += num) {
75
+ output.push(str.slice(i, i + num))
76
+ }
77
+ return output
78
+ }
79
+ function chunkArray(array, num) {
80
+ const result = [];
81
+ for (let i = 0; i < array.length; i += num) {
82
+ result.push(array.slice(i, i + num));
83
+ }
84
+ return result;
85
+ }
86
+
87
+ function decToBin(num, wnum) {
88
+ return num.toString(2).padStart(wnum, '0');
89
+ }
90
+ function binToDec(str) {
91
+ return parseInt(str, 2);
92
+ }
93
+
94
+ function charsBase() {
95
+ const charsBase = {};
96
+ function addChar(i) {
97
+ charsBase[i] = String.fromCharCode(i);
98
+ }
99
+ for (let i = 0; i < 65; i++) addChar(i); /* ASCII 00 - 40 */
100
+ for (let i = 91; i < 97; i++) addChar(i); /* ASCII 5B - 60 */
101
+ for (let i = 123; i < 128; i++) addChar(i); /* ASCII 7B - 7F */
102
+ return charsBase;
103
+ }
104
+ function charsLatin() {
105
+ const charsLatin = {};
106
+ for (let i = 0; i < 128; i++) {
107
+ charsLatin[i] = String.fromCharCode(i); /* ASCII 00 - 7F */
108
+ }
109
+ return charsLatin;
110
+ }
111
+
112
+ const _JSSC = {};
113
+ _JSSC._char = (cde) => String.fromCharCode(cde);
114
+ _JSSC._IDs = {
115
+ 'BASE': 0,
116
+ 'RU': 1,
117
+ 'ENRU': 2,
118
+ 'ENKK': 3,
119
+ 'HI': 4,
120
+ 'ENHI': 5,
121
+ 'BN': 6,
122
+ 'ENBN': 7,
123
+ 'HIBN': 8,
124
+ 'JA': 9,
125
+ 'Telu': 10,
126
+ 'MR': 11,
127
+ };
128
+ _JSSC.BASE = function() { /* Base */
129
+ const chrsBase = charsBase();
130
+ const addCBase = [
131
+ 215, 247, 8722, 11800,
132
+ 174, 169, 171, 10003,
133
+ 182, 9834, 183, 10005,
134
+ 177, 181, 8960, 8211,
135
+ 8212, 8228, 8229, 8230,
136
+ 8240, 8241, 8249, 8250,
137
+ 8252, 8253, 8263, 8264,
138
+ 8267, 8270, 8274, 8451,
139
+ 8457, 8470, 8471, 8482,
140
+
141
+ 402, 1423, 1547, 65020,
142
+ 2547, 2553, 2555, 2801,
143
+ 3065, 3647, 6107, 8499,
144
+ 2546,
145
+
146
+ 8304, 185, 178, 179,
147
+ 8585, 8319, 8305,
148
+
149
+ 8709, 8730, 8734,
150
+ ];
151
+ for (let i = 161; i < 168; i++) {
152
+ addCBase.push(i);
153
+ }
154
+ for (let i = 187; i < 192; i++) {
155
+ addCBase.push(i);
156
+ }
157
+ for (let i = 8308; i < 8317; i++) {
158
+ addCBase.push(i);
159
+ }
160
+ for (let i = 8528; i < 8576; i++) {
161
+ addCBase.push(i);
162
+ }
163
+ for (let i = 8352; i < 8385; i++) {
164
+ addCBase.push(i);
165
+ }
166
+ for (let i = 8320; i < 8333; i++) {
167
+ addCBase.push(i);
168
+ }
169
+ for (let i = 8712; i < 8718; i++) {
170
+ addCBase.push(i);
171
+ }
172
+ let i = 65;
173
+ for (const cde of addCBase) {
174
+ chrsBase[i++] = _JSSC._char(cde);
175
+ if (i === 91) {
176
+ i = 97;
177
+ } else if (i === 123) {
178
+ i = 128;
179
+ }
180
+ }
181
+ return chrsBase
182
+ };
183
+ _JSSC._BASE = [
184
+ 167, 8722, 8451, 169, 8211, 215, 247,
185
+ 8457, 174, 8470, 8482, 8471, 8249,
186
+ 8250, 171, 187, 8242, 8245, 8216,
187
+ 8217, 8218, 8219, 8243, 8246, 8220,
188
+ 8221, 8222, 8223, 8226, 182, 8267,
189
+ 8270, 8240, 8241, 9834, 183, 8228,
190
+ 8229, 8230, 161, 191, 8252, 8264
191
+ ];
192
+ _JSSC._MATH = [
193
+ 8544, 8547, 8550, 8553, 8556, 8572,
194
+ 8545, 8548, 8551, 8554, 8557, 8573,
195
+ 8546, 8549, 8552, 8555, 8558, 8574,
196
+ 8560, 8563, 8566, 8569, 8559, 8575,
197
+ 8561, 8564, 8567, 8570, 8562, 8565,
198
+ 8568, 8571, 8712, 8715, 8713, 8716,
199
+ 8730, 8721, 8734, 8804, 8805
200
+ ];
201
+ _JSSC._CURR = [
202
+ 165, 3647, 8363, 8361, 1423, 8364,
203
+ 8377, 8362, 8378, 8353, 8358, 163,
204
+ 8381, 8354, 8369, 2547, 8370, 8366,
205
+ 8376, 8382, 8357, 6107, 8360, 8372,
206
+ 8373, 8365, 1547, 2801, 162, 65020,
207
+ 8355, 8383, 8380, 3065, 164, 8384,
208
+ 8379, 402, 8359, 2546, 8371, 8367,
209
+ 8356, 8375, 2553, 8368, 8352, 8499,
210
+ 8374, 2555
211
+ ];
212
+ /*
213
+ ASCII (charsLatin) // English, Spanish, Portuguese, French, German
214
+ */
215
+ _JSSC._RU = function(baseOrLatin) {
216
+ const chrsBase = baseOrLatin();
217
+ let maxI = 0;
218
+ for (let i = 1040; i < 1104; i++) {
219
+ const curI = i - 912;
220
+ chrsBase[curI] = _JSSC._char(i); /* Unicode 0410 - 044F */
221
+ maxI = Math.max(maxI, curI);
222
+ }
223
+ chrsBase[maxI + 1] = _JSSC._char(1025); /* Unicode 0401 ( Ё ) */
224
+ chrsBase[maxI + 2] = _JSSC._char(1105); /* Unicode 0451 ( ё ) */
225
+ return chrsBase;
226
+ };
227
+ _JSSC.RU = function() { /* Russian, Ukrainian, Belarusian, Kazakh */
228
+ const chrsBase = _JSSC._RU(charsBase);
229
+ let i = 65;
230
+ for (const char of _JSSC._BASE.concat(_JSSC._MATH, [
231
+ 105, 239, 1028, 1030, 1031, 1108, 1110, 1111,
232
+ 1118, 1038,
233
+ 1241, 1181, 1171, 1199, 1201, 1179, 1257, 1211, 1240, 1186, 1170, 1198, 1200, 1178, 1256, 1210,
234
+ 8381, 8364, 165, 8376, 8372
235
+ ])) {
236
+ chrsBase[i++] = _JSSC._char(char);
237
+ if (i === 91) {
238
+ i = 97;
239
+ } else if (i === 123) {
240
+ i = 193;
241
+ }
242
+ }
243
+ chrsBase[110] = 'i';
244
+ chrsBase[111] = 'I';
245
+
246
+ return chrsBase;
247
+ };
248
+ _JSSC.ENRU = function() { /* English, Russian, Ukrainian, Belarusian */
249
+ const chrsBase = _JSSC._RU(charsLatin);
250
+ let i = 194;
251
+ for (const char of _JSSC._BASE.concat([
252
+ 105, 239, 1028, 1030, 1031, 1108, 1110, 1111,
253
+ 1118, 1038,
254
+ 8381, 8364, 165, 8376, 8372, 163, 8380
255
+ ], [
256
+ 215, 247
257
+ ])) {
258
+ chrsBase[i++] = _JSSC._char(char);
259
+ }
260
+ return chrsBase;
261
+ };
262
+ _JSSC.ENKK = function() { /* English, Russian, Kazakh */
263
+ const chrsBase = _JSSC._RU(charsLatin);
264
+ let i = 194;
265
+ for (const char of _JSSC._BASE.concat([
266
+ 1241, 1181, 1171, 1199, 1201, 1179, 1257, 1211, 1240, 1186, 1170, 1198, 1200, 1178, 1256, 1210,
267
+ 8381, 163, 8376
268
+ ])) {
269
+ chrsBase[i++] = _JSSC._char(char);
270
+ }
271
+ return chrsBase;
272
+ };
273
+ _JSSC._HI = function(baseOrLatin) {
274
+ const chrsBase = baseOrLatin();
275
+ for (let i = 2304; i < 2432; i++) {
276
+ chrsBase[i - 2176] = _JSSC._char(i); /* Unicode 0900 - 097F */
277
+ }
278
+ return chrsBase;
279
+ };
280
+ _JSSC._Ind = [
281
+ 8377, 8360, 78, 2547,
282
+ 2404,
283
+ 215, 247,
284
+ ];
285
+ _JSSC.HI = function() { /* Hindi */
286
+ const chrsBase = _JSSC._HI(charsBase);
287
+ let i = 65;
288
+ for (const char of _JSSC._BASE.concat(_JSSC._Ind)) {
289
+ chrsBase[i++] = _JSSC._char(char);
290
+ if (i === 91) {
291
+ i = 97
292
+ }
293
+ }
294
+ return chrsBase
295
+ };
296
+ _JSSC.ENHI = function() { /* English, Hindi */
297
+ return _JSSC._HI(charsLatin);
298
+ };
299
+ _JSSC._BN = function(baseOrLatin) {
300
+ const chrsBase = baseOrLatin();
301
+ for (let i = 2432; i < 2559; i++) {
302
+ chrsBase[i - 2304] = _JSSC._char(i) /* Unicode 0980 - 09FF */
303
+ }
304
+ chrsBase[255] = _JSSC._char(2404);
305
+ return chrsBase;
306
+ };
307
+ _JSSC.BN = function() { /* Bengali */
308
+ const chrsBase = _JSSC._BN(charsBase);
309
+ let i = 65;
310
+ for (const char of _JSSC._BASE.concat(_JSSC._Ind)) {
311
+ chrsBase[i++] = _JSSC._char(char);
312
+ if (i === 91) {
313
+ i = 97
314
+ }
315
+ }
316
+ return chrsBase;
317
+ };
318
+ _JSSC.ENBN = function() { /* English, Bengali */
319
+ return _JSSC._BN(charsLatin);
320
+ };
321
+ _JSSC.HIBN = function() { /* Hindi, Bengali */
322
+ const chrsBase = {};
323
+ for (let i = 2304; i < 2559; i++) {
324
+ chrsBase[i - 2176 - 128] = _JSSC._char(i);
325
+ }
326
+ chrsBase[255] = ' ';
327
+ return chrsBase;
328
+ };
329
+ _JSSC._JA = [
330
+ [
331
+ 65371, 65373, 65288, 65289, 65339, 65341, 12304, 12305,
332
+ 12289, 65292,
333
+ 12290,
334
+ 12349,
335
+ 12300, 12301, 12302, 12303,
336
+ 12288,
337
+ 12316,
338
+ 65306,
339
+ 65281,
340
+ 65311,
341
+ 12445, 12541,
342
+ 183,
343
+ ],
344
+ [
345
+ 8230, 8229,
346
+ 165,
347
+ ]
348
+ ];
349
+ _JSSC.JA = function() { /* English, Hiragana (Japanese), Katakana (Japanese) */
350
+ const chrsBase = charsLatin();
351
+ let i = 128;
352
+ for (const char of _JSSC._JA[0].concat(
353
+ Array.from({ length : 46 }, (v, j) => j + 12352 ), /* Unicode 3040 - 309F */
354
+ Array.from({ length : 46 }, (v, j) => j + 12448 ), /* Unicode 30A0 - 30FF */
355
+ _JSSC._JA[1], [
356
+ 19968, 20108, 19977,
357
+ 22235, 20116, 20845,
358
+ 19971, 20843, 20061
359
+ ]
360
+ )) {
361
+ chrsBase[i++] = _JSSC._char(char);
362
+ }
363
+ chrsBase[17] = _JSSC._char(21313);
364
+ chrsBase[18] = _JSSC._char(30334);
365
+ chrsBase[19] = _JSSC._char(21315);
366
+ chrsBase[20] = _JSSC._char(19975);
367
+ return chrsBase;
368
+ };
369
+ _JSSC.Telu = function() { /* English, Telugu */
370
+ const chrsBase = charsLatin();
371
+ for (let i = 3073; i < 3184; i++) { /* Unicode 0C01 - 0C6F */
372
+ chrsBase[i - 2945] = _JSSC._char(i);
373
+ }
374
+ let i = 239;
375
+ for (const char of _JSSC._Ind.concat([
376
+ 8364, 0xA3, 0xA2, 0xA5, 0xA7, 0xA9, 0xAE, 8482, 0x2030, 0x2031
377
+ ])) {
378
+ chrsBase[i++] = _JSSC._char(char);
379
+ }
380
+ return chrsBase;
381
+ };
382
+ _JSSC.MR = function() { /* English, Marathi */
383
+ const chrsBase = charsLatin();
384
+ for (let i = 0x900; i < 0x980; i++) {
385
+ chrsBase[i - 2176] = _JSSC._char(i);
386
+ }
387
+ return chrsBase;
388
+ };
389
+ _JSSC.use = class {
390
+ constructor() {
391
+ let output = {};
392
+ for (const [name, func] of Object.entries(_JSSC)) {
393
+ if (typeof func === 'function' && !name.startsWith('_') && name != 'use') {
394
+ output['JSSC'+name] = func;
395
+ }
396
+ }
397
+ Object.freeze(output);
398
+ return output;
399
+ }
400
+ };
401
+ _JSSC._begin = [
402
+ 'https://', 'http://', 'file://', 'mailto:', 'ftp://', 'data:', 'tel:', 'sms:'
403
+ ];
404
+ Object.freeze(_JSSC.use);
405
+
406
+ function cryptCharCode(
407
+ code, get = false,
408
+ repeatBefore = false, repeatAfter = false,
409
+ beginId = -1,
410
+ ) {
411
+ if (get) {
412
+ const codeBin = decToBin(code, 16);
413
+ const codeSet = codeBin.slice(8,11).split('');
414
+ const codeDec = binToDec(codeBin.slice(11));
415
+ const begid = binToDec(codeBin.slice(5,8));
416
+ return {
417
+ code: codeDec,
418
+ repeatBefore: codeSet[0] === '1',
419
+ repeatAfter: codeSet[1] === '1',
420
+ beginId: codeSet[2] === '1' ? begid : -1,
421
+ }
422
+ } else {
423
+ const codeBin = decToBin(code, 5);
424
+ const beginBin = beginId >= 0 ? decToBin(beginId, 3) : '';
425
+ return binToDec(String(beginBin) + String(repeatBefore ? '1' : '0') + String(repeatAfter ? '1' : '0') + String(beginId >= 0 ? '1' : '0') + String(codeBin));
426
+ }
427
+ }
428
+
429
+ /**
430
+ * **JavaScript String Compressor - compress function.**
431
+ * @param {string} str string
432
+ * @returns {string} Compressed string
433
+ * @example compress('Hello, World!');
434
+ * @since 1.0.0
435
+ */
436
+ function compress(str) {
437
+ if (typeof str != 'string') throw new Error('Invalid input.');
438
+ let repeatBefore = false;
439
+ function repeatChars(txt) {
440
+ return txt.replace(/(.)\1+/g, ( a , b ) => b + a.length);
441
+ }
442
+ let beginId = -1;
443
+ for (const begin of _JSSC._begin) {
444
+ if (str.startsWith(begin)) {
445
+ beginId = _JSSC._begin.indexOf(begin);
446
+ str = str.slice(begin.length);
447
+ break;
448
+ }
449
+ }
450
+ const d = (txt) => !/\d/.test(txt);
451
+ if (d(str)) {
452
+ str = repeatChars(str);
453
+ repeatBefore = true;
454
+ }
455
+ const strdata = stringCodes(str);
456
+ const ascii = strdata.maxCharCode < 256;
457
+ let repeatAfter = false;
458
+ function checkOutput(out) {
459
+ if (d(out)) {
460
+ repeatAfter = true;
461
+ }
462
+ }
463
+ function processOutput(out) {
464
+ if (repeatAfter) {
465
+ return repeatChars(out);
466
+ } else {
467
+ return out;
468
+ }
469
+ }
470
+ if (/^\d+$/.test(str)) { /* Numbers */
471
+ /* Up to 8:1 compression ratio */
472
+ const convertNums = {
473
+ 'A': 10,
474
+ 'B': 11,
475
+ 'C': 12,
476
+ 'D': 13,
477
+ 'E': 14
478
+ };
479
+ const inputt = str
480
+ .replaceAll('10', 'A')
481
+ .replaceAll('11', 'B')
482
+ .replaceAll('12', 'C')
483
+ .replaceAll('13', 'D')
484
+ .replaceAll('14', 'E');
485
+ const binOut = [];
486
+ for (const character of inputt.split('')) {
487
+ if (/\d/.test(character)) {
488
+ binOut.push(decToBin(parseInt(character), 4));
489
+ } else {
490
+ binOut.push(decToBin(convertNums[character], 4));
491
+ }
492
+ };
493
+ let output = '';
494
+ function binPadStart(bin) {
495
+ if (bin.length < 16) {
496
+ const numm = 4 - stringChunks(bin, 4).length;
497
+ return decToBin(15, 4).repeat(numm)+bin;
498
+ } else return bin;
499
+ }
500
+ for (const character of chunkArray(binOut, 4)) {
501
+ output += String.fromCharCode(binToDec(binPadStart(character.join(''))));
502
+ }
503
+ checkOutput(output);
504
+ return charCode(cryptCharCode(3, false, false, repeatAfter, -1))+processOutput(output);
505
+ } else if (strdata.max === 2 && strdata.min === 2) {
506
+ /* Up to 3:1 compression ratio */
507
+ let chars = strdata.output;
508
+ let output = '';
509
+ function addChar(codee) {
510
+ output += String.fromCharCode(codee);
511
+ }
512
+ function sliceChars(numbr) {
513
+ chars = chars.slice(numbr);
514
+ }
515
+ while (chars.length > 0) {
516
+ if (chars.length === 1) {
517
+ addChar(chars[0]);
518
+ sliceChars(1);
519
+ } else if (chars.length < 3) {
520
+ for (const char of chars) {
521
+ addChar(char);
522
+ }
523
+ sliceChars(chars.length)
524
+ } else {
525
+ const a1 = parseInt(String(chars[0]) + String(chars[1]) + String(chars[2]));
526
+ const a2 = parseInt(String(chars[0]) + String(chars[1]));
527
+ if (checkChar(a1)) {
528
+ addChar(a1);
529
+ sliceChars(3)
530
+ } else if (checkChar(a2)) {
531
+ addChar(a2);
532
+ sliceChars(2)
533
+ } else {
534
+ addChar(chars[0]);
535
+ sliceChars(1)
536
+ }
537
+ }
538
+ }
539
+ checkOutput(output);
540
+ return charCode(cryptCharCode(1, false, repeatBefore, repeatAfter, beginId))+processOutput(output);
541
+ } else if (ascii) {
542
+ /* Up to 2:1 compression ratio */
543
+ /*
544
+ Bytes
545
+
546
+ ---
547
+
548
+ UTF16 : xxxx xxxx xxxx xxxx
549
+ ASCII : xxxx xxxx
550
+
551
+ ---
552
+
553
+ input: UTF16 => ASCII
554
+
555
+ output: ASCII => UTF16
556
+ 1 UTF16 char = 2 ASCII chars
557
+ */
558
+ const twoChars = [];
559
+ let output = '';
560
+ for (const chars_ of stringChunks(str, 2)) {
561
+ let charsCode = '';
562
+ for (const char of chars_.split('')) {
563
+ charsCode += decToBin(char.charCodeAt(0), 8);
564
+ }
565
+ twoChars.push(charsCode);
566
+ }
567
+ for (const char of twoChars) {
568
+ output += String.fromCharCode(binToDec(char));
569
+ }
570
+ checkOutput(output);
571
+ return charCode(cryptCharCode(2, false, repeatBefore, repeatAfter, beginId))+processOutput(output)
572
+ } else {
573
+ const characterEncodings = new _JSSC.use();
574
+ const stringArray = str.split('');
575
+ let useCharacterEncoding;
576
+ let charEncodingID = NaN;
577
+ for (const [characterEncodingName, characterEncoding] of Object.entries(characterEncodings)) {
578
+ const table = characterEncoding();
579
+ table.length= 256;
580
+ const arrayy= Array.from(table);
581
+ let usethisone = true;
582
+ for (const character of stringArray) {
583
+ if (!arrayy.includes(character)) {
584
+ usethisone = false;
585
+ }
586
+ }
587
+ if (usethisone) {
588
+ useCharacterEncoding = characterEncoding();
589
+ charEncodingID = _JSSC._IDs[characterEncodingName.slice(4)];
590
+ break;
591
+ }
592
+ }
593
+ if (useCharacterEncoding) {
594
+ const reverseCharacterEncoding = {};
595
+ for (const [charCode, character] of Object.entries(useCharacterEncoding)) {
596
+ reverseCharacterEncoding[character] = charCode;
597
+ }
598
+ const binaryCharCodes = [];
599
+ const convertCharCodes = [];
600
+ for (const character of stringArray) {
601
+ binaryCharCodes.push(decToBin(parseInt(reverseCharacterEncoding[character]), 8));
602
+ }
603
+ for (const binCharCodes of chunkArray(binaryCharCodes, 2)) {
604
+ convertCharCodes.push(binCharCodes.join('').padStart(16, '0'));
605
+ }
606
+ let outputStr = '';
607
+ for (const characterCode of convertCharCodes) {
608
+ outputStr += String.fromCharCode(binToDec(characterCode))
609
+ }
610
+ /* Up to 2:1 compression ratio */
611
+ checkOutput(outputStr);
612
+ return charCode(cryptCharCode(charEncodingID + 5, false, repeatBefore, repeatAfter, beginId))+processOutput(outputStr);
613
+ }
614
+
615
+ /* 1:1 compression ratio (no compression) */
616
+ checkOutput(str);
617
+ return charCode(cryptCharCode(0, false, repeatBefore, repeatAfter, beginId))+processOutput(str);
618
+ }
619
+ }
620
+
621
+ function characterEncodings(strcode, realstr) {
622
+ const strcode2charencoding = {};
623
+ for (const [name, code] of Object.entries(_JSSC._IDs)) {
624
+ strcode2charencoding[code] = name
625
+ }
626
+ const possibleCharEncoding = strcode2charencoding[strcode - 5];
627
+ if (possibleCharEncoding) {
628
+ const characterEncodings_ = new _JSSC.use();
629
+ const characterEncoding = characterEncodings_['JSSC'+possibleCharEncoding]();
630
+ let output = '';
631
+ for (const characters of realstr.split('')) {
632
+ const characterCode = characters.charCodeAt();
633
+ const binCode0 = decToBin(characterCode, 0);
634
+ function binCodeToChar(charr) {
635
+ return String(characterEncoding[String(binToDec(charr))]);
636
+ }
637
+ if (binCode0.length > 8) {
638
+ const [character1, character2] = stringChunks(decToBin(characterCode, 16), 8);
639
+ output += binCodeToChar(character1) + binCodeToChar(character2);
640
+ } else {
641
+ const character = decToBin(characterCode, 8);
642
+ output += binCodeToChar(character);
643
+ }
644
+ }
645
+ return output;
646
+ }
647
+ }
648
+
649
+ /**
650
+ * **JavaScript String Compressor - decompress function.**
651
+ * @param {string} str Compressed string
652
+ * @returns {string} Decompressed string
653
+ * @since 1.0.0
654
+ */
655
+ function decompress(str) {
656
+ if (typeof str != 'string') throw new Error('Invalid input.');
657
+ const strcodes= cryptCharCode(str.charCodeAt(0) - 32, true);
658
+ const strcode = strcodes.code;
659
+ function repeatChars(txt) {
660
+ return txt.replace(/(\D)(\d+)/g, (_, g1, g2) => g1.repeat(g2));
661
+ }
662
+ const realstr = strcodes.repeatAfter ? repeatChars(str.slice(1)) : str.slice(1);
663
+ function begin(out) {
664
+ if (strcodes.beginId >= 0) {
665
+ return _JSSC._begin[strcodes.beginId] + out;
666
+ } else return out
667
+ }
668
+ function processOutput(out) {
669
+ if (strcodes.repeatBefore) {
670
+ return repeatChars(begin(out));
671
+ } else return begin(out);
672
+ }
673
+ let output = '';
674
+ switch (strcode) {
675
+ case 0:
676
+ return processOutput(realstr);
677
+ case 1:
678
+ function addChar(cde) {
679
+ output += String.fromCharCode(cde)
680
+ }
681
+ for (const char of realstr.split('')) {
682
+ const charcde = String(char.charCodeAt(0));
683
+ if (charcde.length > 2) {
684
+ const charcds = stringChunks(charcde, 2);
685
+ for (const chrcode of charcds) {
686
+ addChar(parseInt(chrcode));
687
+ }
688
+ } else {
689
+ addChar(char.charCodeAt(0));
690
+ }
691
+ }
692
+ return processOutput(output);
693
+ case 2:
694
+ function toChar(binCode) {
695
+ return String.fromCharCode(binToDec(binCode));
696
+ }
697
+ for (const char of realstr.split('')) {
698
+ const binCode = decToBin(char.charCodeAt(0), 16);
699
+ const binCode0 = decToBin(char.charCodeAt(0), 0);
700
+ if (binCode0.length > 8) {
701
+ const [bin1, bin2] = stringChunks(binCode, 8);
702
+ output += toChar(bin1) + toChar(bin2);
703
+ } else {
704
+ const binCode8 = decToBin(char.charCodeAt(0), 8);
705
+ output += toChar(binCode8);
706
+ }
707
+ }
708
+ return processOutput(output);
709
+ case 3:
710
+ for (const char of realstr.split('')) {
711
+ const binCodes = stringChunks(decToBin(char.charCodeAt(0), 16), 4);
712
+ for (const binCode of binCodes) {
713
+ const numm = binToDec(binCode);
714
+ if (numm != 15) {
715
+ output += numm.toString(10);
716
+ }
717
+ }
718
+ }
719
+ return processOutput(output);
720
+ default:
721
+ const err = () => {throw new Error('')};
722
+ return processOutput(characterEncodings(strcode, realstr) || err());
723
+ }
724
+ }
725
+
726
+ return {compress, decompress,
727
+ get[Symbol.toStringTag]() {
728
+ return 'JSSC';
729
+ }
730
+ };
731
+
732
+ }));
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "strc",
3
+ "version": "1.0.0a",
4
+ "description": "JavaScript String Compressor",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/JustDeveloper1/JSSC.git"
12
+ },
13
+ "author": "JustDeveloper",
14
+ "license": "MIT",
15
+ "bugs": {
16
+ "url": "https://github.com/JustDeveloper1/JSSC/issues"
17
+ },
18
+ "engines": {
19
+ "node": ">=12.0.0"
20
+ },
21
+ "homepage": "https://github.com/JustDeveloper1/JSSC#readme",
22
+ "files": [
23
+ "index.js",
24
+ "index.d.ts",
25
+ "README.md",
26
+ "LICENSE"
27
+ ]
28
+ }