isutf8 2.1.0 → 4.0.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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2019 Denis Seleznev, hcodes@yandex.ru
3
+ Copyright (c) 2021 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,8 +1,7 @@
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
- [![Dependency Status](https://img.shields.io/david/hcodes/isutf8.svg?style=flat)](https://david-dm.org/hcodes/isutf8)
4
- [![Build Status](https://img.shields.io/travis/hcodes/isutf8.svg?style=flat)](https://travis-ci.org/hcodes/isutf8)
5
- [![Coverage Status](https://img.shields.io/coveralls/hcodes/isutf8.svg?branch=master)](https://coveralls.io/r/hcodes/isutf8)
3
+ [![Bundlephobia](https://badgen.net/bundlephobia/minzip/isutf8)](https://bundlephobia.com/result?p=isutf8)
4
+ [![install size](https://packagephobia.com/badge?p=isutf8)](https://packagephobia.com/result?p=isutf8)
6
5
 
7
6
  isutf8
8
7
  ======
@@ -13,6 +12,8 @@ Quick check if a Node.js Buffer or Uint8Array is UTF-8.
13
12
  `npm install isutf8`
14
13
 
15
14
  ## Usage
15
+
16
+ ### CommonJS
16
17
  ```js
17
18
  'use strict';
18
19
 
@@ -28,5 +29,18 @@ console.log(isUtf8(arr)); // => true
28
29
 
29
30
  ```
30
31
 
32
+ ### ES Modules or TypeScript
33
+ ```js
34
+ import isUtf8 from 'isutf8';
35
+
36
+ const buf = Buffer.from([0xd0, 0x90]);
37
+ console.log(isUtf8(buf)); // => true
38
+
39
+ // or
40
+
41
+ const arr = new Uint8Array([0xd0, 0x90]);
42
+ console.log(isUtf8(arr)); // => true
43
+ ```
44
+
31
45
  ## License
32
46
  [MIT License](./LICENSE)
@@ -0,0 +1,5 @@
1
+ /// <reference types="node" />
2
+ /**
3
+ * Check if a Node.js Buffer or Uint8Array is UTF-8.
4
+ */
5
+ export default function isUtf8(buf?: Buffer | Uint8Array): boolean;
@@ -0,0 +1,79 @@
1
+ /*
2
+ https://tools.ietf.org/html/rfc3629
3
+
4
+ UTF8-char = UTF8-1 / UTF8-2 / UTF8-3 / UTF8-4
5
+
6
+ UTF8-1 = %x00-7F
7
+
8
+ UTF8-2 = %xC2-DF UTF8-tail
9
+
10
+ UTF8-3 = %xE0 %xA0-BF UTF8-tail
11
+ %xE1-EC 2( UTF8-tail )
12
+ %xED %x80-9F UTF8-tail
13
+ %xEE-EF 2( UTF8-tail )
14
+
15
+ UTF8-4 = %xF0 %x90-BF 2( UTF8-tail )
16
+ %xF1-F3 3( UTF8-tail )
17
+ %xF4 %x80-8F 2( UTF8-tail )
18
+
19
+ UTF8-tail = %x80-BF
20
+ */
21
+ /**
22
+ * Check if a Node.js Buffer or Uint8Array is UTF-8.
23
+ */
24
+ function isUtf8(buf) {
25
+ if (!buf) {
26
+ return false;
27
+ }
28
+ var i = 0;
29
+ var len = buf.length;
30
+ while (i < len) {
31
+ // UTF8-1 = %x00-7F
32
+ if (buf[i] <= 0x7F) {
33
+ i++;
34
+ continue;
35
+ }
36
+ // UTF8-2 = %xC2-DF UTF8-tail
37
+ if (buf[i] >= 0xC2 && buf[i] <= 0xDF) {
38
+ // if(buf[i + 1] >= 0x80 && buf[i + 1] <= 0xBF) {
39
+ if (buf[i + 1] >> 6 === 2) {
40
+ i += 2;
41
+ continue;
42
+ }
43
+ else {
44
+ return false;
45
+ }
46
+ }
47
+ // UTF8-3 = %xE0 %xA0-BF UTF8-tail
48
+ // UTF8-3 = %xED %x80-9F UTF8-tail
49
+ if (((buf[i] === 0xE0 && buf[i + 1] >= 0xA0 && buf[i + 1] <= 0xBF) ||
50
+ (buf[i] === 0xED && buf[i + 1] >= 0x80 && buf[i + 1] <= 0x9F)) && buf[i + 2] >> 6 === 2) {
51
+ i += 3;
52
+ continue;
53
+ }
54
+ // UTF8-3 = %xE1-EC 2( UTF8-tail )
55
+ // UTF8-3 = %xEE-EF 2( UTF8-tail )
56
+ if (((buf[i] >= 0xE1 && buf[i] <= 0xEC) ||
57
+ (buf[i] >= 0xEE && buf[i] <= 0xEF)) &&
58
+ buf[i + 1] >> 6 === 2 &&
59
+ buf[i + 2] >> 6 === 2) {
60
+ i += 3;
61
+ continue;
62
+ }
63
+ // UTF8-4 = %xF0 %x90-BF 2( UTF8-tail )
64
+ // %xF1-F3 3( UTF8-tail )
65
+ // %xF4 %x80-8F 2( UTF8-tail )
66
+ if (((buf[i] === 0xF0 && buf[i + 1] >= 0x90 && buf[i + 1] <= 0xBF) ||
67
+ (buf[i] >= 0xF1 && buf[i] <= 0xF3 && buf[i + 1] >> 6 === 2) ||
68
+ (buf[i] === 0xF4 && buf[i + 1] >= 0x80 && buf[i + 1] <= 0x8F)) &&
69
+ buf[i + 2] >> 6 === 2 &&
70
+ buf[i + 3] >> 6 === 2) {
71
+ i += 4;
72
+ continue;
73
+ }
74
+ return false;
75
+ }
76
+ return true;
77
+ }
78
+
79
+ export { isUtf8 as default };
@@ -1,4 +1,5 @@
1
1
  'use strict';
2
+
2
3
  /*
3
4
  https://tools.ietf.org/html/rfc3629
4
5
 
@@ -19,88 +20,61 @@
19
20
 
20
21
  UTF8-tail = %x80-BF
21
22
  */
22
-
23
23
  /**
24
- * Check if a Node.js Buffer or Uint8Array is utf-8
25
- *
26
- * @param {Buffer|Uint8Array} buf
27
- * @returns {boolean}
24
+ * Check if a Node.js Buffer or Uint8Array is UTF-8.
28
25
  */
29
26
  function isUtf8(buf) {
30
27
  if (!buf) {
31
28
  return false;
32
29
  }
33
-
34
- var i = 0, len = buf.length;
35
-
36
- while(i < len) {
30
+ var i = 0;
31
+ var len = buf.length;
32
+ while (i < len) {
37
33
  // UTF8-1 = %x00-7F
38
34
  if (buf[i] <= 0x7F) {
39
35
  i++;
40
-
41
36
  continue;
42
37
  }
43
-
44
38
  // UTF8-2 = %xC2-DF UTF8-tail
45
39
  if (buf[i] >= 0xC2 && buf[i] <= 0xDF) {
46
40
  // if(buf[i + 1] >= 0x80 && buf[i + 1] <= 0xBF) {
47
41
  if (buf[i + 1] >> 6 === 2) {
48
42
  i += 2;
49
-
50
43
  continue;
51
- } else {
44
+ }
45
+ else {
52
46
  return false;
53
47
  }
54
48
  }
55
-
56
49
  // UTF8-3 = %xE0 %xA0-BF UTF8-tail
57
50
  // UTF8-3 = %xED %x80-9F UTF8-tail
58
- if (
59
- (
60
- (buf[i] === 0xE0 && buf[i + 1] >= 0xA0 && buf[i + 1] <= 0xBF) ||
61
- (buf[i] === 0xED && buf[i + 1] >= 0x80 && buf[i + 1] <= 0x9F)
62
- ) && buf[i + 2] >> 6 === 2
63
- ) {
51
+ if (((buf[i] === 0xE0 && buf[i + 1] >= 0xA0 && buf[i + 1] <= 0xBF) ||
52
+ (buf[i] === 0xED && buf[i + 1] >= 0x80 && buf[i + 1] <= 0x9F)) && buf[i + 2] >> 6 === 2) {
64
53
  i += 3;
65
-
66
54
  continue;
67
55
  }
68
-
69
56
  // UTF8-3 = %xE1-EC 2( UTF8-tail )
70
57
  // UTF8-3 = %xEE-EF 2( UTF8-tail )
71
- if (
72
- (
73
- (buf[i] >= 0xE1 && buf[i] <= 0xEC) ||
74
- (buf[i] >= 0xEE && buf[i] <= 0xEF)
75
- ) &&
76
- buf[i + 1] >> 6 === 2 &&
77
- buf[i + 2] >> 6 === 2
78
- ) {
58
+ if (((buf[i] >= 0xE1 && buf[i] <= 0xEC) ||
59
+ (buf[i] >= 0xEE && buf[i] <= 0xEF)) &&
60
+ buf[i + 1] >> 6 === 2 &&
61
+ buf[i + 2] >> 6 === 2) {
79
62
  i += 3;
80
-
81
63
  continue;
82
64
  }
83
-
84
65
  // UTF8-4 = %xF0 %x90-BF 2( UTF8-tail )
85
66
  // %xF1-F3 3( UTF8-tail )
86
67
  // %xF4 %x80-8F 2( UTF8-tail )
87
- if (
88
- (
89
- (buf[i] === 0xF0 && buf[i + 1] >= 0x90 && buf[i + 1] <= 0xBF) ||
90
- (buf[i] >= 0xF1 && buf[i] <= 0xF3 && buf[i + 1] >> 6 === 2) ||
91
- (buf[i] === 0xF4 && buf[i + 1] >= 0x80 && buf[i + 1] <= 0x8F)
92
- ) &&
68
+ if (((buf[i] === 0xF0 && buf[i + 1] >= 0x90 && buf[i + 1] <= 0xBF) ||
69
+ (buf[i] >= 0xF1 && buf[i] <= 0xF3 && buf[i + 1] >> 6 === 2) ||
70
+ (buf[i] === 0xF4 && buf[i + 1] >= 0x80 && buf[i + 1] <= 0x8F)) &&
93
71
  buf[i + 2] >> 6 === 2 &&
94
- buf[i + 3] >> 6 === 2
95
- ) {
72
+ buf[i + 3] >> 6 === 2) {
96
73
  i += 4;
97
-
98
74
  continue;
99
75
  }
100
-
101
76
  return false;
102
77
  }
103
-
104
78
  return true;
105
79
  }
106
80
 
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "isutf8",
3
- "description": "Check if a Node.js Buffer or Uint8Array is utf-8",
4
- "version": "2.1.0",
3
+ "description": "Check if a Node.js Buffer or Uint8Array is UTF-8",
4
+ "version": "4.0.0",
5
5
  "author": {
6
6
  "name": "Denis Seleznev",
7
7
  "email": "hcodes@yandex.ru",
8
8
  "url": "https://github.com/hcodes"
9
9
  },
10
- "main": "index.js",
10
+ "main": "dist/index.js",
11
+ "module": "dist/index.esm.js",
11
12
  "homepage": "https://github.com/hcodes/isutf8",
12
13
  "license": "MIT",
13
14
  "repository": {
@@ -24,22 +25,32 @@
24
25
  "Uint8Array"
25
26
  ],
26
27
  "engines": {
27
- "node": ">= 0.12"
28
+ "node": ">= 12"
28
29
  },
29
30
  "devDependencies": {
30
- "chai": "^4.2.0",
31
- "eslint": "^6.2.2",
32
- "mocha": "^6.2.0",
33
- "nyc": "^14.1.1"
31
+ "@rollup/plugin-typescript": "^8.3.0",
32
+ "@types/jest": "^27.0.2",
33
+ "@typescript-eslint/eslint-plugin": "^5.2.0",
34
+ "@typescript-eslint/parser": "^5.2.0",
35
+ "del-cli": "^4.0.1",
36
+ "eslint": "^8.1.0",
37
+ "jest": "^27.3.1",
38
+ "rollup": "^2.58.3",
39
+ "ts-jest": "^27.0.7",
40
+ "tslib": "^2.3.1",
41
+ "typescript": "^4.4.4"
34
42
  },
35
43
  "scripts": {
36
- "test": "npm run-script eslint && npm run-script unit-test",
37
- "eslint": "./node_modules/.bin/eslint .",
38
- "unit-test": "nyc mocha -u bdd -R spec --recursive test"
44
+ "test": "npm run eslint && npm run unit",
45
+ "eslint": "eslint --ext .ts",
46
+ "clean": "del-cli dist/*",
47
+ "unit": "jest --config ./jest.config.js",
48
+ "build": "npm run clean && rollup --config rollup.config.js"
39
49
  },
50
+ "typings": "dist/index.d.ts",
40
51
  "files": [
41
- "index.js",
42
52
  "LICENSE",
43
- "README.md"
53
+ "README.md",
54
+ "dist"
44
55
  ]
45
56
  }
package/CHANGELOG.md DELETED
@@ -1,15 +0,0 @@
1
- # Changelog
2
-
3
- ## v2.1.0
4
- - Add support for `Uint8Array`.
5
- - Updated dev deps in package.json.
6
-
7
- ## v2.0.4
8
- - Updated dev deps in package.json.
9
-
10
- ## v2.0.3
11
- - Updated dev deps in package.json.
12
-
13
- ## v2.0.2
14
- - Added package-lock.json.
15
- - Updated the example in README.md.