From efad3c3f0d194ff19421e9bb73fcbd20451d6ea8 Mon Sep 17 00:00:00 2001 From: Dmitry Shirokov Date: Fri, 30 Sep 2022 12:41:58 +1000 Subject: [PATCH 1/2] Test exports --- .github/workflows/build.yml | 1 + .github/workflows/test-build.js | 22 ++++++++++++++++++++++ .github/workflows/test-build.sh | 6 ++++++ .github/workflows/test-build.ts | 29 +++++++++++++++++++++++++++++ package.json | 1 + 5 files changed, 59 insertions(+) create mode 100644 .github/workflows/test-build.js create mode 100644 .github/workflows/test-build.sh create mode 100644 .github/workflows/test-build.ts diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 917ac9e..c672943 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -23,3 +23,4 @@ jobs: - run: npm i - run: npm test - run: npm run build + - run: node .github/workflows/test-build.sh diff --git a/.github/workflows/test-build.js b/.github/workflows/test-build.js new file mode 100644 index 0000000..a3e8ebc --- /dev/null +++ b/.github/workflows/test-build.js @@ -0,0 +1,22 @@ +const assert = require('assert'); +const path = require('path'); + +const chardet = require(process.cwd()); + +assert(typeof chardet.analyse, 'function'); +assert(typeof chardet.detect, 'function'); +assert(typeof chardet.detectFile, 'function'); +assert(typeof chardet.detectFileSync, 'function'); + +assert.deepStrictEqual(chardet.analyse(Buffer.from('This is a test')), [ + { confidence: 98, name: 'ISO-8859-1', lang: 'en' }, + { confidence: 98, name: 'ISO-8859-2', lang: 'hu' }, + { confidence: 10, name: 'UTF-8', lang: undefined }, + { confidence: 10, name: 'Shift_JIS', lang: 'ja' }, + { confidence: 10, name: 'Big5', lang: 'zh' }, + { confidence: 10, name: 'EUC-JP', lang: 'ja' }, + { confidence: 10, name: 'EUC-KR', lang: 'ko' }, + { confidence: 10, name: 'GB18030', lang: 'zh' }, +]); + +console.log(' > test-build.js OK'); diff --git a/.github/workflows/test-build.sh b/.github/workflows/test-build.sh new file mode 100644 index 0000000..9bde9a1 --- /dev/null +++ b/.github/workflows/test-build.sh @@ -0,0 +1,6 @@ +#!/bin/sh -ex + +export PATH=$PATH:$(npm bin) + +node ./.github/workflows/test-build.js +ts-node ./.github/workflows/test-build.ts \ No newline at end of file diff --git a/.github/workflows/test-build.ts b/.github/workflows/test-build.ts new file mode 100644 index 0000000..54eba2d --- /dev/null +++ b/.github/workflows/test-build.ts @@ -0,0 +1,29 @@ +import assert from 'assert'; +import path from 'path'; + +const main = async () => { + const chardet = await import(process.cwd()); + + assert(typeof chardet.analyse, 'function'); + assert(typeof chardet.detect, 'function'); + assert(typeof chardet.detectFile, 'function'); + assert(typeof chardet.detectFileSync, 'function'); + + assert.deepStrictEqual(chardet.analyse(Buffer.from('This is a test')), [ + { confidence: 98, name: 'ISO-8859-1', lang: 'en' }, + { confidence: 98, name: 'ISO-8859-2', lang: 'hu' }, + { confidence: 10, name: 'UTF-8', lang: undefined }, + { confidence: 10, name: 'Shift_JIS', lang: 'ja' }, + { confidence: 10, name: 'Big5', lang: 'zh' }, + { confidence: 10, name: 'EUC-JP', lang: 'ja' }, + { confidence: 10, name: 'EUC-KR', lang: 'ko' }, + { confidence: 10, name: 'GB18030', lang: 'zh' }, + ]); +}; + +main() + .then(() => console.log(' > test-build.ts OK')) + .catch((err) => { + console.error(err); + process.exit(1); + }); diff --git a/package.json b/package.json index 3e97308..32e46bd 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "prettier": "^2.1.2", "semantic-release": "^17.1.2", "ts-jest": "^26.4.0", + "ts-node": "^10.9.1", "typescript": "^4.8.4" }, "keywords": [ From a1935b1ba7d5a411cecdac182a3127e64bbb5f20 Mon Sep 17 00:00:00 2001 From: Dmitry Shirokov Date: Fri, 30 Sep 2022 17:04:00 +1000 Subject: [PATCH 2/2] chore: Test exports --- .github/workflows/build.yml | 2 +- .github/workflows/test-build.js | 1 - .github/workflows/test-build.sh | 0 .github/workflows/test-build.ts | 1 - README.md | 12 ++++++------ 5 files changed, 7 insertions(+), 9 deletions(-) mode change 100644 => 100755 .github/workflows/test-build.sh diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c672943..b597ec6 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -23,4 +23,4 @@ jobs: - run: npm i - run: npm test - run: npm run build - - run: node .github/workflows/test-build.sh + - run: .github/workflows/test-build.sh diff --git a/.github/workflows/test-build.js b/.github/workflows/test-build.js index a3e8ebc..4dd0e53 100644 --- a/.github/workflows/test-build.js +++ b/.github/workflows/test-build.js @@ -1,5 +1,4 @@ const assert = require('assert'); -const path = require('path'); const chardet = require(process.cwd()); diff --git a/.github/workflows/test-build.sh b/.github/workflows/test-build.sh old mode 100644 new mode 100755 diff --git a/.github/workflows/test-build.ts b/.github/workflows/test-build.ts index 54eba2d..79f925c 100644 --- a/.github/workflows/test-build.ts +++ b/.github/workflows/test-build.ts @@ -1,5 +1,4 @@ import assert from 'assert'; -import path from 'path'; const main = async () => { const chardet = await import(process.cwd()); diff --git a/README.md b/README.md index bc32617..7da2139 100644 --- a/README.md +++ b/README.md @@ -21,19 +21,19 @@ npm i chardet To return the encoding with the highest confidence: ```javascript -const chardet = require('chardet'); +import chardet from 'chardet'; -chardet.detect(Buffer.from('hello there!')); +const encoding = chardet.detect(Buffer.from('hello there!')); // or -chardet.detectFile('/path/to/file').then(encoding => console.log(encoding)); +const encoding = await chardet.detectFile('/path/to/file'); // or -chardet.detectFileSync('/path/to/file'); +const encoding = chardet.detectFileSync('/path/to/file'); ``` To return the full list of possible encodings use `analyse` method. ```javascript -const chardet = require('chardet'); +import chardet from 'chardet'; chardet.analyse(Buffer.from('hello there!')); ``` @@ -48,7 +48,7 @@ Returned value is an array of objects sorted by confidence value in decending or ## Working with large data sets -Sometimes, when data set is huge and you want to optimize performace (in tradeoff of less accuracy), +Sometimes, when data set is huge and you want to optimize performace (with a tradeoff of less accuracy), you can sample only first N bytes of the buffer: ```javascript