isutf8 4.0.1 → 4.0.4

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2024 Denis Seleznev, hcodes@yandex.ru
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,49 +1,127 @@
1
1
  [![NPM Version](https://img.shields.io/npm/v/isutf8.svg?style=flat)](https://www.npmjs.org/package/isutf8)
2
2
  [![NPM Downloads](https://img.shields.io/npm/dm/isutf8.svg?style=flat)](https://www.npmjs.org/package/isutf8)
3
- [![Bundlephobia](https://badgen.net/bundlephobia/minzip/isutf8)](https://bundlephobia.com/result?p=isutf8)
4
3
  [![install size](https://packagephobia.com/badge?p=isutf8)](https://packagephobia.com/result?p=isutf8)
5
4
 
6
5
  isutf8
7
6
  ======
8
7
 
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
+
9
12
  Quick check if a Node.js Buffer or Uint8Array is valid UTF-8.
10
13
 
11
14
  ## Advantages
15
+
12
16
  - Ultra-small package size
13
- - No dependencies
14
- - No pre-compilation
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`.
15
58
 
16
59
  ## Install
17
- `npm install isutf8`
60
+
61
+ ```sh
62
+ npm install isutf8
63
+ ```
18
64
 
19
65
  ## Usage
20
66
 
21
67
  ### CommonJS
68
+
22
69
  ```js
23
70
  const isUtf8 = require('isutf8');
24
71
 
25
72
  const buf = Buffer.from([0xd0, 0x90]);
26
- console.log(isUtf8(buf)); // => boolean
73
+ console.log(isUtf8(buf)); // true
27
74
 
28
- // or
75
+ // or
29
76
 
30
77
  const arr = new Uint8Array([0xd0, 0x90]);
31
- console.log(isUtf8(arr)); // => boolean
32
-
78
+ console.log(isUtf8(arr)); // true
33
79
  ```
34
80
 
35
81
  ### ES Modules or TypeScript
82
+
36
83
  ```js
37
84
  import isUtf8 from 'isutf8';
38
85
 
39
86
  const buf = Buffer.from([0xd0, 0x90]);
40
- console.log(isUtf8(buf)); // => boolean
87
+ console.log(isUtf8(buf)); // true
41
88
 
42
- // or
89
+ // or
43
90
 
44
91
  const arr = new Uint8Array([0xd0, 0x90]);
45
- console.log(isUtf8(arr)); // => boolean
92
+ console.log(isUtf8(arr)); // true
46
93
  ```
47
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
109
+ ```
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
+
48
125
  ## License
126
+
49
127
  [MIT License](./LICENSE)
package/dist/index.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  /**
3
2
  * Check if a Node.js Buffer or Uint8Array is UTF-8.
4
3
  */
package/dist/index.esm.js CHANGED
@@ -25,7 +25,9 @@ function isUtf8(buf) {
25
25
  if (!buf) {
26
26
  return false;
27
27
  }
28
+ // eslint-disable-next-line no-var
28
29
  var i = 0;
30
+ // eslint-disable-next-line no-var
29
31
  var len = buf.length;
30
32
  while (i < len) {
31
33
  // UTF8-1 = %x00-7F
package/dist/index.js CHANGED
@@ -27,7 +27,9 @@ function isUtf8(buf) {
27
27
  if (!buf) {
28
28
  return false;
29
29
  }
30
+ // eslint-disable-next-line no-var
30
31
  var i = 0;
32
+ // eslint-disable-next-line no-var
31
33
  var len = buf.length;
32
34
  while (i < len) {
33
35
  // UTF8-1 = %x00-7F
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.1",
4
+ "version": "4.0.4",
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",
@@ -35,20 +36,24 @@
35
36
  "node": ">= 12"
36
37
  },
37
38
  "devDependencies": {
38
- "@rollup/plugin-typescript": "^11.1.6",
39
- "@types/jest": "^29.5.12",
40
- "@typescript-eslint/eslint-plugin": "^8.3.0",
41
- "@typescript-eslint/parser": "^8.3.0",
42
- "del-cli": "^5.1.0",
43
- "eslint": "^8.3.0",
44
- "jest": "^29.7.0",
45
- "rollup": "^4.21.1",
46
- "ts-jest": "^29.2.5",
47
- "tslib": "^2.7.0",
48
- "typescript": "^5.5.4"
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"
49
54
  },
50
55
  "scripts": {
51
- "test": "npm run eslint && npm run unit",
56
+ "test": "npm run eslint && npm run typecheck && npm run unit",
52
57
  "eslint": "eslint --ext .ts",
53
58
  "clean": "del-cli dist/*",
54
59
  "unit": "jest --config ./jest.config.js",