isutf8 4.0.0 → 4.0.3
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 +1 -1
- package/README.md +93 -12
- package/dist/index.d.ts +0 -1
- package/dist/index.esm.js +2 -0
- package/dist/index.js +2 -0
- package/package.json +27 -14
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
The MIT License (MIT)
|
|
2
2
|
|
|
3
|
-
Copyright (c)
|
|
3
|
+
Copyright (c) 2026 Denis Seleznev, hcodes@yandex.ru
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/README.md
CHANGED
|
@@ -1,46 +1,127 @@
|
|
|
1
1
|
[](https://www.npmjs.org/package/isutf8)
|
|
2
2
|
[](https://www.npmjs.org/package/isutf8)
|
|
3
|
-
[](https://bundlephobia.com/result?p=isutf8)
|
|
4
3
|
[](https://packagephobia.com/result?p=isutf8)
|
|
5
4
|
|
|
6
5
|
isutf8
|
|
7
6
|
======
|
|
8
7
|
|
|
9
|
-
|
|
8
|
+
> **Deprecated:** Use [`isUtf8` from `node:buffer`](https://nodejs.org/api/buffer.html#bufferisutf8input)
|
|
9
|
+
> in Node.js, or [`TextDecoder`](https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder/decode)
|
|
10
|
+
> with `{ fatal: true }` in browsers. See the migration examples below.
|
|
11
|
+
|
|
12
|
+
Quick check if a Node.js Buffer or Uint8Array is valid UTF-8.
|
|
13
|
+
|
|
14
|
+
## Advantages
|
|
15
|
+
|
|
16
|
+
- Ultra-small package size
|
|
17
|
+
- No runtime dependencies
|
|
18
|
+
- No native compilation required
|
|
19
|
+
|
|
20
|
+
## Migration to built-in APIs
|
|
21
|
+
|
|
22
|
+
For modern Node.js applications, prefer the built-in
|
|
23
|
+
[`isUtf8` from `node:buffer`](https://nodejs.org/api/buffer.html#bufferisutf8input),
|
|
24
|
+
available since Node.js 18.14.0 and 19.4.0:
|
|
25
|
+
|
|
26
|
+
```js
|
|
27
|
+
import { isUtf8 } from 'node:buffer';
|
|
28
|
+
|
|
29
|
+
console.log(isUtf8(Buffer.from([0xd0, 0x90]))); // true
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
In modern browsers, use
|
|
33
|
+
[`TextDecoder`](https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder/decode)
|
|
34
|
+
with `fatal: true` to reject invalid UTF-8:
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
function isUtf8(input) {
|
|
38
|
+
if (input === undefined) return false;
|
|
39
|
+
|
|
40
|
+
try {
|
|
41
|
+
new TextDecoder('utf-8', { fatal: true }).decode(input);
|
|
42
|
+
return true;
|
|
43
|
+
} catch (error) {
|
|
44
|
+
if (error instanceof TypeError) return false;
|
|
45
|
+
throw error;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
console.log(isUtf8(new Uint8Array([0xd0, 0x90]))); // true
|
|
50
|
+
console.log(isUtf8(new Uint8Array([0xc3]))); // false
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Unlike a byte-only validator, `TextDecoder` also produces a decoded string.
|
|
54
|
+
This package mainly remains useful for older environments without these APIs
|
|
55
|
+
(it supports Node.js 12 or later), or for retaining its existing API.
|
|
56
|
+
When migrating, note that this package returns `false` for a missing argument,
|
|
57
|
+
while Node.js's built-in `isUtf8` throws a `TypeError`.
|
|
10
58
|
|
|
11
59
|
## Install
|
|
12
|
-
|
|
60
|
+
|
|
61
|
+
```sh
|
|
62
|
+
npm install isutf8
|
|
63
|
+
```
|
|
13
64
|
|
|
14
65
|
## Usage
|
|
15
66
|
|
|
16
67
|
### CommonJS
|
|
17
|
-
```js
|
|
18
|
-
'use strict';
|
|
19
68
|
|
|
69
|
+
```js
|
|
20
70
|
const isUtf8 = require('isutf8');
|
|
21
71
|
|
|
22
72
|
const buf = Buffer.from([0xd0, 0x90]);
|
|
23
|
-
console.log(isUtf8(buf)); //
|
|
73
|
+
console.log(isUtf8(buf)); // true
|
|
24
74
|
|
|
25
|
-
// or
|
|
75
|
+
// or
|
|
26
76
|
|
|
27
77
|
const arr = new Uint8Array([0xd0, 0x90]);
|
|
28
|
-
console.log(isUtf8(arr)); //
|
|
29
|
-
|
|
78
|
+
console.log(isUtf8(arr)); // true
|
|
30
79
|
```
|
|
31
80
|
|
|
32
81
|
### ES Modules or TypeScript
|
|
82
|
+
|
|
33
83
|
```js
|
|
34
84
|
import isUtf8 from 'isutf8';
|
|
35
85
|
|
|
36
86
|
const buf = Buffer.from([0xd0, 0x90]);
|
|
37
|
-
console.log(isUtf8(buf)); //
|
|
87
|
+
console.log(isUtf8(buf)); // true
|
|
38
88
|
|
|
39
|
-
// or
|
|
89
|
+
// or
|
|
40
90
|
|
|
41
91
|
const arr = new Uint8Array([0xd0, 0x90]);
|
|
42
|
-
console.log(isUtf8(arr)); //
|
|
92
|
+
console.log(isUtf8(arr)); // true
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## API
|
|
96
|
+
|
|
97
|
+
`isUtf8(input?: Buffer | Uint8Array): boolean`
|
|
98
|
+
|
|
99
|
+
Returns `true` for valid UTF-8 byte sequences, including ASCII and empty input.
|
|
100
|
+
Returns `false` for invalid or incomplete sequences, or a missing argument.
|
|
101
|
+
It validates bytes; it does not detect the original encoding or decode text.
|
|
102
|
+
|
|
103
|
+
```js
|
|
104
|
+
const isUtf8 = require('isutf8');
|
|
105
|
+
|
|
106
|
+
console.log(isUtf8(new Uint8Array())); // true
|
|
107
|
+
console.log(isUtf8(new Uint8Array([0xc3]))); // false: incomplete sequence
|
|
108
|
+
console.log(isUtf8()); // false
|
|
43
109
|
```
|
|
44
110
|
|
|
111
|
+
## Development
|
|
112
|
+
|
|
113
|
+
Use Node.js 22.13.0 or later in the Node.js 22 line, or Node.js 24 or later,
|
|
114
|
+
for the development tools.
|
|
115
|
+
|
|
116
|
+
```sh
|
|
117
|
+
npm ci
|
|
118
|
+
npm test
|
|
119
|
+
npm run build
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
TypeScript is kept on 6.0.x because the current `typescript-eslint` and `ts-jest`
|
|
123
|
+
releases do not support TypeScript 7 yet.
|
|
124
|
+
|
|
45
125
|
## License
|
|
126
|
+
|
|
46
127
|
[MIT License](./LICENSE)
|
package/dist/index.d.ts
CHANGED
package/dist/index.esm.js
CHANGED
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "isutf8",
|
|
3
3
|
"description": "Check if a Node.js Buffer or Uint8Array is UTF-8",
|
|
4
|
-
"version": "4.0.
|
|
4
|
+
"version": "4.0.3",
|
|
5
|
+
"deprecated": "Use isUtf8 from node:buffer in modern Node.js, or TextDecoder with { fatal: true } in browsers.",
|
|
5
6
|
"author": {
|
|
6
7
|
"name": "Denis Seleznev",
|
|
7
8
|
"email": "hcodes@yandex.ru",
|
|
@@ -18,8 +19,15 @@
|
|
|
18
19
|
"keywords": [
|
|
19
20
|
"charset",
|
|
20
21
|
"utf-8",
|
|
22
|
+
"is utf",
|
|
23
|
+
"is utf-8",
|
|
24
|
+
"is utf8",
|
|
21
25
|
"utf8",
|
|
26
|
+
"unicode",
|
|
27
|
+
"is unicode",
|
|
22
28
|
"text",
|
|
29
|
+
"check",
|
|
30
|
+
"validate",
|
|
23
31
|
"encoding",
|
|
24
32
|
"Buffer",
|
|
25
33
|
"Uint8Array"
|
|
@@ -28,24 +36,29 @@
|
|
|
28
36
|
"node": ">= 12"
|
|
29
37
|
},
|
|
30
38
|
"devDependencies": {
|
|
31
|
-
"@
|
|
32
|
-
"@
|
|
33
|
-
"@
|
|
34
|
-
"@
|
|
35
|
-
"
|
|
36
|
-
"eslint": "^8.
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
39
|
+
"@eslint/js": "^10.0.1",
|
|
40
|
+
"@rollup/plugin-typescript": "^12.3.0",
|
|
41
|
+
"@types/jest": "^30.0.0",
|
|
42
|
+
"@types/node": "^22.20.2",
|
|
43
|
+
"@typescript-eslint/eslint-plugin": "^8.70.0",
|
|
44
|
+
"@typescript-eslint/parser": "^8.70.0",
|
|
45
|
+
"del-cli": "^7.0.0",
|
|
46
|
+
"eslint": "^10.10.0",
|
|
47
|
+
"globals": "^17.12.0",
|
|
48
|
+
"jest": "^30.5.1",
|
|
49
|
+
"rollup": "^4.63.1",
|
|
50
|
+
"ts-jest": "^29.4.12",
|
|
51
|
+
"tslib": "^2.8.1",
|
|
52
|
+
"typescript": "~6.0.3",
|
|
53
|
+
"typescript-eslint": "^8.70.0"
|
|
42
54
|
},
|
|
43
55
|
"scripts": {
|
|
44
|
-
"test": "npm run eslint && npm run unit",
|
|
56
|
+
"test": "npm run eslint && npm run typecheck && npm run unit",
|
|
45
57
|
"eslint": "eslint --ext .ts",
|
|
46
58
|
"clean": "del-cli dist/*",
|
|
47
59
|
"unit": "jest --config ./jest.config.js",
|
|
48
|
-
"build": "npm run clean && rollup --config rollup.config.
|
|
60
|
+
"build": "npm run clean && rollup --config rollup.config.mjs",
|
|
61
|
+
"typecheck": "tsc --noEmit"
|
|
49
62
|
},
|
|
50
63
|
"typings": "dist/index.d.ts",
|
|
51
64
|
"files": [
|