iconv-tiny 1.2.0 → 1.2.2

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 CHANGED
@@ -42,7 +42,7 @@ or use CDN:
42
42
  <script type="importmap">
43
43
  {
44
44
  "imports": {
45
- "iconv-tiny": "https://unpkg.com/iconv-tiny@1.2.0/dist/iconv-tiny.mjs"
45
+ "iconv-tiny": "https://unpkg.com/iconv-tiny@1.2.2/dist/iconv-tiny.mjs"
46
46
  }
47
47
  }
48
48
  </script>
@@ -121,7 +121,7 @@ All encodings are generated automatically from http://www.unicode.org/Public/MAP
121
121
  Comparison with iconv-lite module (Core i7-7500U CPU @ 2.7GHz, Node v24.2.0). Note: your results may vary, so please always check on your hardware.
122
122
 
123
123
  ```
124
- operation iconv-lite@0.6.3 iconv-tiny@1.2.0
124
+ operation iconv-lite@0.6.3 iconv-tiny@1.2.2
125
125
  ------------------------------------------------------
126
126
  encode('win1251') ~598 Mb/s ~622 Mb/s
127
127
  decode('win1251') ~218 Mb/s ~263 Mb/s
@@ -156,8 +156,8 @@ $ npm run coverage
156
156
  ----------------|---------|----------|---------|---------|-------------------
157
157
  File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
158
158
  ----------------|---------|----------|---------|---------|-------------------
159
- All files | 100 | 100 | 98.21 | 100 |
160
- commons.mjs | 100 | 100 | 88.88 | 100 |
159
+ All files | 100 | 100 | 96.72 | 100 |
160
+ commons.mjs | 100 | 100 | 81.81 | 100 |
161
161
  iconv-tiny.mjs | 100 | 100 | 100 | 100 |
162
162
  sbcs.mjs | 100 | 100 | 100 | 100 |
163
163
  unicode.mjs | 100 | 100 | 100 | 100 |
@@ -0,0 +1,181 @@
1
+ export class IconvTiny {
2
+ /**
3
+ * @param encodings A map of encodings to support.
4
+ * @param aliases Comma-separated groups, each containing space-separated aliases for the same encoding.
5
+ */
6
+ constructor(encodings?: { [key: string]: EncodingFactory }, aliases?: string);
7
+ decode(array: Uint8Array, encoding: string, options?: OptionsAndDecoderOptions): string;
8
+ encode(content: string, encoding: string, options?: OptionsAndEncoderOptions): Uint8Array;
9
+ getEncoding(encoding: string, options?: Options): Encoding;
10
+ }
11
+
12
+ /**
13
+ * Converts an encoding name to a normalized, unique name.
14
+ * Removes non-alphanumeric characters and leading zeros.
15
+ * For more details, refer to: https://www.unicode.org/reports/tr22/tr22-8.html#Charset_Alias_Matching
16
+ * @param {string} encoding
17
+ * @returns {string}
18
+ */
19
+ export function canonicalize(encoding: string): string;
20
+
21
+ interface Encoding {
22
+ getName(): string;
23
+ decode(array: Uint8Array, options?: DecoderOptions): string;
24
+ encode(text: string, options?: EncoderOptions): Uint8Array;
25
+ newDecoder(options?: DecoderOptions): CharsetDecoder;
26
+ newEncoder(options?: EncoderOptions): CharsetEncoder;
27
+ }
28
+
29
+ interface EncodingFactory {
30
+ create(options?: Options): Encoding;
31
+ }
32
+
33
+ interface CharsetDecoder {
34
+ decode(array?: Uint8Array): string;
35
+ }
36
+
37
+ interface CharsetEncoder {
38
+ encode(text?: string): Uint8Array;
39
+ encodeInto(src: string, dst: Uint8Array): TextEncoderEncodeIntoResult;
40
+ /**
41
+ * Similar to Buffer.byteLength;
42
+ * @param src input to calculate the length of
43
+ * @returns The number of bytes of the specified string
44
+ */
45
+ byteLength(src: string): number;
46
+ }
47
+
48
+ type TextEncoderEncodeIntoResult = {
49
+ read: number;
50
+ written: number;
51
+ }
52
+
53
+ type DecoderOptions = {
54
+ /**
55
+ * Sets the replacement character used by the "decode" method for unmapped bytes (default: "�").
56
+ */
57
+ defaultCharUnicode?: string | DefaultCharUnicodeFunction;
58
+ /**
59
+ * Specifies the behavior of "decode" method (default: false)
60
+ *
61
+ * - true: use native TextDecoder whenever possible
62
+ * - false: use "software" decoding according to the mapping rules.
63
+ */
64
+ native?: boolean;
65
+ /**
66
+ * UTF-16LE, UTF-16BE, UTF-32LE, UTF-32BE: BOM is stripped by default, unless overridden by stripBOM: false
67
+ * UTF-16, UTF-32: Use BOM, fallback to LE, unless overriden by defaultEncoding: 'UTF-16BE' or 'UTF-32BE';
68
+ */
69
+ stripBOM?: boolean;
70
+ };
71
+
72
+ type EncoderOptions = {
73
+ /**
74
+ * Sets the replacement byte used by the "encode" method for unmapped Unicode symbols (default: "?").
75
+ */
76
+ defaultCharByte?: string | DefaultCharByteFunction;
77
+ /**
78
+ * UTF-16LE, UTF-16BE, UTF-32LE, UTF-32BE: No BOM added by default, unless overridden by addBOM: true
79
+ * UTF-16, UTF-32: Use LE and add BOM by default, unless overridden by addBOM: false
80
+ */
81
+ addBOM?: boolean;
82
+ }
83
+
84
+ type Options = {
85
+ /**
86
+ * Defines custom character mappings (default: undefined).
87
+ *
88
+ * Format: [<byte_1>, <character_1>, <byte_2>, <character_2>, ...].
89
+ * Example: [0x8f, "⚡"] maps the byte 0x8f to "⚡" and vice versa during encoding.
90
+ * Only symbols with code values no greater than 0xFFFF are allowed.
91
+ * Unicode encodings ignore this option
92
+ */
93
+ overrides?: Overrides;
94
+ };
95
+
96
+ type Overrides = Array<number | string>;
97
+
98
+ /**
99
+ * @param {number} input - input character code (0-65536)
100
+ * @param {number} index - index of the character
101
+ * @returns {number} default byte (0-255)
102
+ */
103
+ type DefaultCharByteFunction = (input: number, index: number) => number | null | undefined;
104
+
105
+ /**
106
+ * @param {number} input - input byte (0-255)
107
+ * @param {number} index - index of the byte
108
+ * @returns {number} default character code (0-65536)
109
+ */
110
+ type DefaultCharUnicodeFunction = (input: number, index: number) => number | null | undefined;
111
+
112
+ type OptionsAndDecoderOptions = Options & DecoderOptions;
113
+ type OptionsAndEncoderOptions = Options & EncoderOptions;
114
+
115
+ export const ISO_8859_1: EncodingFactory;
116
+ export const ISO_8859_2: EncodingFactory;
117
+ export const ISO_8859_3: EncodingFactory;
118
+ export const ISO_8859_4: EncodingFactory;
119
+ export const ISO_8859_5: EncodingFactory;
120
+ export const ISO_8859_6: EncodingFactory;
121
+ export const ISO_8859_7: EncodingFactory;
122
+ export const ISO_8859_8: EncodingFactory;
123
+ export const ISO_8859_9: EncodingFactory;
124
+ export const ISO_8859_10: EncodingFactory;
125
+ export const ISO_8859_11: EncodingFactory;
126
+ export const ISO_8859_13: EncodingFactory;
127
+ export const ISO_8859_14: EncodingFactory;
128
+ export const ISO_8859_15: EncodingFactory;
129
+ export const ISO_8859_16: EncodingFactory;
130
+ export const CP037: EncodingFactory;
131
+ export const CP500: EncodingFactory;
132
+ export const CP875: EncodingFactory;
133
+ export const CP1026: EncodingFactory;
134
+ export const CP437: EncodingFactory;
135
+ export const CP737: EncodingFactory;
136
+ export const CP775: EncodingFactory;
137
+ export const CP850: EncodingFactory;
138
+ export const CP852: EncodingFactory;
139
+ export const CP855: EncodingFactory;
140
+ export const CP857: EncodingFactory;
141
+ export const CP860: EncodingFactory;
142
+ export const CP861: EncodingFactory;
143
+ export const CP862: EncodingFactory;
144
+ export const CP863: EncodingFactory;
145
+ export const CP864: EncodingFactory;
146
+ export const CP865: EncodingFactory;
147
+ export const CP866: EncodingFactory;
148
+ export const CP869: EncodingFactory;
149
+ export const CP874: EncodingFactory;
150
+ export const CP1250: EncodingFactory;
151
+ export const CP1251: EncodingFactory;
152
+ export const CP1252: EncodingFactory;
153
+ export const CP1253: EncodingFactory;
154
+ export const CP1254: EncodingFactory;
155
+ export const CP1255: EncodingFactory;
156
+ export const CP1256: EncodingFactory;
157
+ export const CP1257: EncodingFactory;
158
+ export const CP1258: EncodingFactory;
159
+ export const MAC_CYRILLIC: EncodingFactory;
160
+ export const MAC_GREEK: EncodingFactory;
161
+ export const MAC_ICELAND: EncodingFactory;
162
+ export const MAC_LATIN2: EncodingFactory;
163
+ export const MAC_ROMAN: EncodingFactory;
164
+ export const MAC_TURKISH: EncodingFactory;
165
+ export const ATARIST: EncodingFactory;
166
+ export const CP424: EncodingFactory;
167
+ export const CP856: EncodingFactory;
168
+ export const CP1006: EncodingFactory;
169
+ export const KOI8_R: EncodingFactory;
170
+ export const KOI8_U: EncodingFactory;
171
+ export const KZ1048: EncodingFactory;
172
+ export const NEXTSTEP: EncodingFactory;
173
+ export const US_ASCII: EncodingFactory;
174
+ export const UTF8: EncodingFactory;
175
+ export const UTF16LE: EncodingFactory;
176
+ export const UTF16BE: EncodingFactory;
177
+ export const UTF32LE: EncodingFactory;
178
+ export const UTF32BE: EncodingFactory;
179
+
180
+ export const encodings: { [key: string]: EncodingFactory };
181
+ export const aliases: string;
@@ -1,19 +1,20 @@
1
1
  /**
2
- * iconv-tiny v1.2.0
2
+ * iconv-tiny v1.2.2
3
3
  * (c) 2025-present vip.delete
4
4
  * @license MIT
5
5
  **/
6
6
  let ns;
7
- const k=new TextDecoder("UTF-16LE",{fatal:!0});class l{constructor(a){this.h=a}getName(){return this.h}decode(a,b){b=this.newDecoder(b);return b.decode(a)+b.decode()}encode(a,b){return this.newEncoder(b).encode(a)}}class m{constructor(a){this.g=a}decode(a){return this.g.decode(a,{stream:!!a})}}
8
- class p{encode(a){if(!a)return new Uint8Array(0);const b=new Uint8Array(this.g(a));({written:a}=this.encodeInto(a,b));return b.subarray(0,a)}byteLength(a){let b=0;const c=new Uint8Array(4096);do{const {read:d,written:e}=this.encodeInto(a,c);a=a.slice(d);b+=e}while(a.length);return b}};class q{constructor(a,b){this.g=a;if(typeof b!=="function"){const c=b?.length?b.charCodeAt(0):65533;b=()=>c}this.h=b}decode(a){if(!a)return"";var b=this.g;const c=this.h,d=a.length,e=new Uint16Array(d);for(let g=0;g<d;g++){const h=a[g],n=b[h];e[g]=n===65533?c(h,g)??n:n}a:if(a=e.length,a<=192)var f=String.fromCharCode(...e);else{try{f=k.decode(e);break a}catch{}f=[];for(b=0;b<a;b+=1024)f.push(String.fromCharCode(...e.subarray(b,b+1024)));f=f.join("")}return f}}
9
- class r extends p{constructor(a,b){super();this.h=a;if(typeof b!=="function"){const c=b?.length?b.charCodeAt(0):63;b=()=>c}this.i=b}encodeInto(a,b){const c=this.h,d=this.i,e=Math.min(a.length,b.length);for(let f=0;f<e;f++){const g=a.charCodeAt(f),h=c[g];b[f]=h===65533?d(g,f)??63:h}return{read:e,written:e}}g(a){return a.length}byteLength(a){return this.g(a)}}
10
- class t extends l{constructor(a,b){super(a);this.i=b;this.g=null;try{new TextDecoder(this.h),this.j=!0}catch{this.j=!1}}newDecoder(a){return this.j&&(a?.native??!1)?new m(new TextDecoder(this.h)):new q(this.i,a?.defaultCharUnicode)}newEncoder(a){if(!this.g){this.g=(new Uint16Array(65536)).fill(65533);for(let b=0;b<256;b++){const c=this.i[b];c!==65533&&(this.g[c]=b)}}return new r(this.g,a?.defaultCharByte)}}
11
- class u{constructor(a,b,c){this.g=a;this.h=b;this.i=c}create(a){var b=this.h,c=this.i??"";const d=(new Uint16Array(256)).fill(65533);for(var e=0;e<256-b.length;)d[e]=e++;let f=0;for(;e<256;)d[e++]=b.charCodeAt(f++);for(b=0;b<c.length;)d[c.charCodeAt(b)]=c.charCodeAt(b+1),b+=2;a=a?.overrides??[];for(c=0;c<a.length-1;)b=a[c++],e=a[c++],d[Number(b)]=typeof e==="number"?e:e.charCodeAt(0);return new t(this.g,d)}};const v=(a,b,c,d)=>{a=a.charCodeAt(b);c[d]=a;c[d+1]=a>>8;return 0},w=(a,b,c,d)=>{a=a.charCodeAt(b);c[d]=a>>8;c[d+1]=a;return 0},x=(a,b,c,d)=>{a=a.codePointAt(b);c[d]=a;c[d+1]=a>>8;c[d+2]=a>>16;c[d+3]=a>>24;return a>65535?1:0},y=(a,b,c,d)=>{a=a.codePointAt(b);c[d]=a>>24;c[d+1]=a>>16;c[d+2]=a>>8;c[d+3]=a;return a>65535?1:0},z=(a,b)=>(a[b]|a[b+1]<<8|a[b+2]<<16|a[b+3]<<24)>>>0,A=(a,b)=>(a[b]<<24|a[b+1]<<16|a[b+2]<<8|a[b+3])>>>0,B=(a,b,c)=>{c>1114111&&(c=65533);if(c>65535){c-=65536;const d=55296|c>>10;
12
- c=56320|c&1023;a[b]=d;a[b+1]=d>>8;a[b+2]=c;a[b+3]=c>>8;return 4}a[b]=c;a[b+1]=c>>8;return 2};class C extends m{constructor(a){super(new TextDecoder("UTF-8",{ignoreBOM:a}))}}class D extends p{constructor(a){super();this.h=a;this.i=new TextEncoder}encodeInto(a,b){let c=0;if(this.h){if(b.length<3)return{read:0,written:0};b[0]=239;b[1]=187;b[2]=191;c+=3;this.h=0}const {read:d,written:e}=this.i.encodeInto(a,b.subarray(c));return{read:d,written:e+c}}g(a){return(this.h?4:0)+a.length*4}}
13
- class E extends p{constructor(a,b,c){super();this.i=a;this.h=1<<b+1;this.j=b?c?y:x:c?w:v}encodeInto(a,b){const c=this.h,d=this.j;let e=0;if(this.i){if(b.length<c)return{read:0,written:0};d("",0,b,e);e+=c;this.i=0}const f=Math.min(a.length,b.length-e&~(c-1));for(let g=0;g<f;g++,e+=c)g+=d(a,g,b,e);return{read:f,written:e}}g(a){return(this.i?this.h:0)+a.length*this.h}byteLength(a){return this.h===4?super.byteLength(a):this.g(a)}}const F=["LE","BE",""],G=[16,32,8];
7
+ const k=new TextDecoder("UTF-16LE",{fatal:!0});class l{constructor(a){this.i=a}getName(){return this.i}decode(a,b){b=this.newDecoder(b);return b.decode(a)+b.decode()}encode(a,b){return this.newEncoder(b).encode(a)}}class m{constructor(a){this.g=a}decode(a){return this.g.decode(a,{stream:!!a})}}
8
+ class p{encode(a){this.i();if(!a)return new Uint8Array(0);const b=new Uint8Array(this.g(a));({written:a}=this.encodeInto(a,b));return b.subarray(0,a)}byteLength(a){this.i();let b=0;const c=new Uint8Array(4096);do{const {read:d,written:e}=this.encodeInto(a,c);a=a.slice(d);b+=e}while(a.length);return b}};class q{constructor(a,b){this.g=a;if(typeof b!=="function"){const c=b?.length?b.charCodeAt(0):65533;b=()=>c}this.i=b}decode(a){if(!a)return"";var b=this.g,c=this.i;const d=a.length,e=new Uint16Array(d);for(let g=0;g<d;g++){const h=a[g],n=b[h];e[g]=n===65533?c(h,g)??n:n}if(e.length<=192)var f=String.fromCharCode(...e);else try{f=k.decode(e)}catch{a=e.length;b=[];for(c=0;c<a;c+=1024)b.push(String.fromCharCode(...e.subarray(c,c+1024)));f=b.join("")}return f}}
9
+ class r extends p{constructor(a,b){super();this.h=a;if(typeof b!=="function"){const c=b?.length?b.charCodeAt(0):63;b=()=>c}this.j=b}encodeInto(a,b){const c=this.h,d=this.j,e=Math.min(a.length,b.length);for(let f=0;f<e;f++){const g=a.charCodeAt(f),h=c[g];b[f]=h===65533?d(g,f)??63:h}return{read:e,written:e}}g(a){return a.length}byteLength(a){return this.g(a)}i(){}}
10
+ class t extends l{constructor(a,b){super(a);this.h=b;this.g=null;try{new TextDecoder(this.i),this.j=!0}catch{this.j=!1}}newDecoder(a){return this.j&&(a?.native??!1)?new m(new TextDecoder(this.i)):new q(this.h,a?.defaultCharUnicode)}newEncoder(a){if(!this.g){this.g=(new Uint16Array(65536)).fill(65533);for(let b=0;b<256;b++){const c=this.h[b];c!==65533&&(this.g[c]=b)}}return new r(this.g,a?.defaultCharByte)}}
11
+ class u{constructor(a,b,c){this.g=a;this.i=b;this.h=c}create(a){var b=this.i,c=this.h??"";const d=(new Uint16Array(256)).fill(65533);for(var e=0;e<256-b.length;)d[e]=e++;let f=0;for(;e<256;)d[e++]=b.charCodeAt(f++);for(b=0;b<c.length;)d[c.charCodeAt(b)]=c.charCodeAt(b+1),b+=2;a=a?.overrides??[];for(c=0;c<a.length-1;)b=a[c++],e=a[c++],d[Number(b)]=typeof e==="number"?e:e.charCodeAt(0);return new t(this.g,d)}};const v=(a,b,c,d)=>{a=a.charCodeAt(b);c[d]=a;c[d+1]=a>>8;return 0},w=(a,b,c,d)=>{a=a.charCodeAt(b);c[d]=a>>8;c[d+1]=a;return 0},x=(a,b,c,d)=>{a=a.codePointAt(b);c[d]=a;c[d+1]=a>>8;c[d+2]=a>>16;c[d+3]=a>>24;return a>65535?1:0},y=(a,b,c,d)=>{a=a.codePointAt(b);c[d]=a>>24;c[d+1]=a>>16;c[d+2]=a>>8;c[d+3]=a;return a>65535?1:0},z=(a,b)=>(a[b]|a[b+1]<<8|a[b+2]<<16|a[b+3]<<24)>>>0,A=(a,b)=>(a[b]<<24|a[b+1]<<16|a[b+2]<<8|a[b+3])>>>0,B=(a,b,c)=>{c>1114111&&(c=65533);if(c>65535){c-=65536;const d=55296|c>>10;
12
+ c=56320|c&1023;a[b]=d;a[b+1]=d>>8;a[b+2]=c;a[b+3]=c>>8;return 4}a[b]=c;a[b+1]=c>>8;return 2};class C extends m{constructor(a){super(new TextDecoder("UTF-8",{ignoreBOM:a}))}}
13
+ class D extends p{constructor(a){super();this.h=this.j=a;this.l=new TextEncoder}encodeInto(a,b){let c=0;if(this.h){if(b.length<3)return{read:0,written:0};b[0]=239;b[1]=187;b[2]=191;c+=3;this.h=!1}const {read:d,written:e}=this.l.encodeInto(a,b.subarray(c));return{read:d,written:e+c}}g(a){return(this.j?4:0)+a.length*4}i(){this.h=this.j}}
14
+ class E extends p{constructor(a,b,c){super();this.j=this.l=a;this.h=1<<b+1;this.m=b?c?y:x:c?w:v}encodeInto(a,b){const c=this.h,d=this.m;let e=0;if(this.j){if(b.length<c)return{read:0,written:0};d("",0,b,e);e+=c;this.j=!1}const f=Math.min(a.length,b.length-e&~(c-1));for(let g=0;g<f;g++,e+=c)g+=d(a,g,b,e);return{read:f,written:e}}g(a){return(this.l?this.h:0)+a.length*this.h}byteLength(a){return this.h===4?super.byteLength(a):this.g(a)}i(){this.j=this.l}}const F=["LE","BE",""],G=[16,32,8];
14
15
  class H extends m{constructor(a,b){super(new TextDecoder("UTF-16"+F[b],{ignoreBOM:a}))}}
15
- class I{constructor(a,b){this.j=a;this.h=new Uint8Array(4);this.g=0;this.i=b?A:z}decode(a){if(!a)return this.g?String.fromCharCode(65533):"";const b=new Uint8Array(a.length+4);let c=0,d=0;if(this.g){for(;this.g<4&&c<a.length;)this.h[this.g++]=a[c++];if(this.g<4)return"";d+=B(b,d,this.i(this.h,0))}const e=a.length-3;for(;c<e;c+=4)d+=B(b,d,this.i(a,c));(this.g=a.length-c)&&this.h.set(a.subarray(c));return(new TextDecoder("UTF-16",{ignoreBOM:this.j})).decode(b.subarray(0,d))}}
16
- const J=[H,I,C],K=[E,E,D];class L extends l{constructor(a,b){super("UTF-"+G[a]+F[b]);this.g=a;this.i=b}newDecoder(a){return new J[this.g](!(a?.stripBOM??1),this.i)}newEncoder(a){return new K[this.g](a?.addBOM??0,this.g,this.i)}}class M{constructor(a,b){this.h=a;this.g=b}create(){return new L(this.h,this.g)}};const N=a=>a.toLowerCase().replace(/[^a-z0-9]/gu,"").replace(/(?<!\d)0+/gu,"");
16
+ class I{constructor(a,b){this.j=a;this.i=new Uint8Array(4);this.g=0;this.h=b?A:z}decode(a){if(!a)return this.g?String.fromCharCode(65533):"";const b=new Uint8Array(a.length+4);let c=0,d=0;if(this.g){for(;this.g<4&&c<a.length;)this.i[this.g++]=a[c++];if(this.g<4)return"";d+=B(b,d,this.h(this.i,0))}const e=a.length-3;for(;c<e;c+=4)d+=B(b,d,this.h(a,c));(this.g=a.length-c)&&this.i.set(a.subarray(c));return(new TextDecoder("UTF-16",{ignoreBOM:this.j})).decode(b.subarray(0,d))}}
17
+ const J=[H,I,C],K=[E,E,D];class L extends l{constructor(a,b){super("UTF-"+G[a]+F[b]);this.g=a;this.h=b}newDecoder(a){return new J[this.g](!(a?.stripBOM??!0),this.h)}newEncoder(a){return new K[this.g](a?.addBOM??!1,this.g,this.h)}}class M{constructor(a,b){this.i=a;this.g=b}create(){return new L(this.i,this.g)}};const N=a=>a.toLowerCase().replace(/[^a-z0-9]/gu,"").replace(/(?<!\d)0+/gu,"");
17
18
  class O{constructor(a,b){a??={};this.g=new Map;this.cache=new Map;b=(b??"").split(",").map(c=>c.split(" ").map(N));for(const c of Object.keys(a)){const d=a[c];if(d?.create){const e=N(c);this.g.set(e,d);b.filter(f=>f.includes(e)).forEach(f=>f.forEach(g=>this.g.set(g,d)))}}}decode(a,b,c){return this.getEncoding(b,c).decode(a,c)}encode(a,b,c){return this.getEncoding(b,c).encode(a,c)}getEncoding(a,b){a=N(a);const c=a+(b?.overrides??"");var d=this.cache.get(c);if(!d){d=this.g.get(a);if(!d)throw Error(`Encoding "${a}" not supported`);
18
19
  d=d.create(b);this.cache.set(c,d)}return d}};ns={canonicalize:N,IconvTiny:O,SBCS:u,Unicode:M};
19
20
  export const {canonicalize,IconvTiny,SBCS,Unicode}=ns;
@@ -1,5 +1,5 @@
1
1
  /**
2
- * iconv-tiny v1.2.0
2
+ * iconv-tiny v1.2.2
3
3
  * (c) 2025-present vip.delete
4
4
  * @license MIT
5
5
  **/
@@ -78,6 +78,14 @@ var DEFAULT_NATIVE_DECODE = false;
78
78
  var STRING_SMALLSIZE = 192;
79
79
  var STRING_CHUNKSIZE = 1024;
80
80
  var UTF16 = new TextDecoder("UTF-16LE", { fatal: true });
81
+ var getStringFallback = (u16) => {
82
+ const len = u16.length;
83
+ const result = [];
84
+ for (let i = 0; i < len; i += STRING_CHUNKSIZE) {
85
+ result.push(String.fromCharCode(...u16.subarray(i, i + STRING_CHUNKSIZE)));
86
+ }
87
+ return result.join("");
88
+ };
81
89
  var getString = (u16) => {
82
90
  const len = u16.length;
83
91
  if (len <= STRING_SMALLSIZE) {
@@ -86,12 +94,8 @@ var getString = (u16) => {
86
94
  try {
87
95
  return UTF16.decode(u16);
88
96
  } catch {
97
+ return getStringFallback(u16);
89
98
  }
90
- const result = [];
91
- for (let i = 0; i < len; i += STRING_CHUNKSIZE) {
92
- result.push(String.fromCharCode(...u16.subarray(i, i + STRING_CHUNKSIZE)));
93
- }
94
- return result.join("");
95
99
  };
96
100
  var Charset = class {
97
101
  /**
@@ -156,6 +160,7 @@ var CharsetEncoderBase = class {
156
160
  */
157
161
  // @ts-expect-error
158
162
  encode(text) {
163
+ this.reset();
159
164
  if (!text) {
160
165
  return new Uint8Array(0);
161
166
  }
@@ -165,7 +170,7 @@ var CharsetEncoderBase = class {
165
170
  }
166
171
  // @ts-expect-error
167
172
  // eslint-disable-next-line jsdoc/empty-tags
168
- /** @abstract @param {string} text @returns {number} */
173
+ /** @abstract @param {string} text @returns {number} */
169
174
  // eslint-disable-next-line no-unused-vars, no-empty-function, class-methods-use-this
170
175
  byteLengthMax(text) {
171
176
  }
@@ -176,6 +181,7 @@ var CharsetEncoderBase = class {
176
181
  */
177
182
  // @ts-expect-error
178
183
  byteLength(text) {
184
+ this.reset();
179
185
  let total = 0;
180
186
  const buf = new Uint8Array(4096);
181
187
  do {
@@ -185,6 +191,10 @@ var CharsetEncoderBase = class {
185
191
  } while (text.length);
186
192
  return total;
187
193
  }
194
+ /** @abstract */
195
+ // eslint-disable-next-line no-empty-function, class-methods-use-this
196
+ reset() {
197
+ }
188
198
  };
189
199
 
190
200
  // src/sbcs.mjs
@@ -270,6 +280,12 @@ var SBCSEncoder = class extends CharsetEncoderBase {
270
280
  byteLength(text) {
271
281
  return this.byteLengthMax(text);
272
282
  }
283
+ /**
284
+ * @override
285
+ */
286
+ // eslint-disable-next-line class-methods-use-this
287
+ reset() {
288
+ }
273
289
  };
274
290
  var SBCSCharset = class extends Charset {
275
291
  /**
@@ -374,8 +390,8 @@ var SBCS = class {
374
390
 
375
391
  // src/unicode.mjs
376
392
  var BOM_CHAR = "\uFEFF";
377
- var STRIP_BOM_DEFAULT = 1;
378
- var ADD_BOM_DEFAULT = 0;
393
+ var STRIP_BOM_DEFAULT = true;
394
+ var ADD_BOM_DEFAULT = false;
379
395
  var put16LE = (src, i, dst, j) => {
380
396
  const cp = src.charCodeAt(i);
381
397
  dst[j] = cp;
@@ -440,11 +456,12 @@ var UTF8Decoder = class extends NativeCharsetDecoder {
440
456
  };
441
457
  var UTF8Encoder = class extends CharsetEncoderBase {
442
458
  /**
443
- * @param {number} doBOM
459
+ * @param {boolean} doBOM
444
460
  */
445
461
  constructor(doBOM) {
446
462
  super();
447
463
  this.doBOM = doBOM;
464
+ this.appendBOM = doBOM;
448
465
  this.encoder = new TextEncoder();
449
466
  }
450
467
  /**
@@ -455,9 +472,9 @@ var UTF8Encoder = class extends CharsetEncoderBase {
455
472
  */
456
473
  // @ts-expect-error
457
474
  encodeInto(src, dst) {
458
- const { doBOM } = this;
475
+ const { appendBOM } = this;
459
476
  let j = 0;
460
- if (doBOM) {
477
+ if (appendBOM) {
461
478
  if (dst.length < 3) {
462
479
  return { read: 0, written: 0 };
463
480
  }
@@ -465,7 +482,7 @@ var UTF8Encoder = class extends CharsetEncoderBase {
465
482
  dst[1] = 187;
466
483
  dst[2] = 191;
467
484
  j += 3;
468
- this.doBOM = 0;
485
+ this.appendBOM = false;
469
486
  }
470
487
  const { read, written } = this.encoder.encodeInto(src, dst.subarray(j));
471
488
  return { read, written: written + j };
@@ -478,16 +495,23 @@ var UTF8Encoder = class extends CharsetEncoderBase {
478
495
  byteLengthMax(src) {
479
496
  return (this.doBOM ? 4 : 0) + src.length * 4;
480
497
  }
498
+ /**
499
+ * @override
500
+ */
501
+ reset() {
502
+ this.appendBOM = this.doBOM;
503
+ }
481
504
  };
482
505
  var UnicodeEncoder = class extends CharsetEncoderBase {
483
506
  /**
484
- * @param {number} doBOM
507
+ * @param {boolean} doBOM
485
508
  * @param {number} i - 0 for UTF-16, 1 for UTF-32
486
509
  * @param {number} bo - 0 for LE, 1 for BE
487
510
  */
488
511
  constructor(doBOM, i, bo) {
489
512
  super();
490
513
  this.doBOM = doBOM;
514
+ this.appendBOM = doBOM;
491
515
  this.sz = 1 << i + 1;
492
516
  this.put = i ? bo ? put32BE : put32LE : bo ? put16BE : put16LE;
493
517
  }
@@ -499,15 +523,15 @@ var UnicodeEncoder = class extends CharsetEncoderBase {
499
523
  */
500
524
  // @ts-expect-error
501
525
  encodeInto(src, dst) {
502
- const { doBOM, sz, put } = this;
526
+ const { appendBOM, sz, put } = this;
503
527
  let j = 0;
504
- if (doBOM) {
528
+ if (appendBOM) {
505
529
  if (dst.length < sz) {
506
530
  return { read: 0, written: 0 };
507
531
  }
508
532
  put(BOM_CHAR, 0, dst, j);
509
533
  j += sz;
510
- this.doBOM = 0;
534
+ this.appendBOM = false;
511
535
  }
512
536
  const max = Math.min(src.length, dst.length - j & ~(sz - 1));
513
537
  for (let i = 0; i < max; i++, j += sz) {
@@ -534,6 +558,12 @@ var UnicodeEncoder = class extends CharsetEncoderBase {
534
558
  }
535
559
  return this.byteLengthMax(text);
536
560
  }
561
+ /**
562
+ * @override
563
+ */
564
+ reset() {
565
+ this.appendBOM = this.doBOM;
566
+ }
537
567
  };
538
568
  var POSTFIX = ["LE", "BE", ""];
539
569
  var NAMES = [16, 32, 8];
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "iconv-tiny",
4
- "version": "1.2.0",
4
+ "version": "1.2.2",
5
5
  "description": "Pure JS ESM Encodings for Browser and NodeJS",
6
6
  "sideEffects": false,
7
7
  "keywords": [
@@ -18,7 +18,7 @@
18
18
  "license": "MIT",
19
19
  "author": "vip.delete",
20
20
  "files": [
21
- "dist/iconv-tiny.d.mjs",
21
+ "dist/iconv-tiny.d.mts",
22
22
  "dist/iconv-tiny.mjs",
23
23
  "dist/iconv-tiny.min.mjs",
24
24
  "LICENSE",
@@ -38,7 +38,7 @@
38
38
  "coverage": "vitest run --coverage main.test.mjs",
39
39
  "test": "vitest run",
40
40
  "build": "npm run lint && npm run generate && npm run test && npm run coverage",
41
- "dev": "node scripts/server.js"
41
+ "dev": "node scripts/server.mjs"
42
42
  },
43
43
  "devDependencies": {
44
44
  "@stylistic/eslint-plugin": "^5.x",
@@ -52,7 +52,7 @@
52
52
  "eslint": "^9.x",
53
53
  "eslint-plugin-jsdoc": "^51.x",
54
54
  "express": "^5.x",
55
- "google-closure-compiler": "^20250625.0.0",
55
+ "google-closure-compiler": "^20250709.0.0",
56
56
  "iconv-lite": "^0.6.3",
57
57
  "prettier": "^3.x",
58
58
  "serve-static": "^2.x",