From 1b40042222aece92e48b0ab0c46c24ad3ec2ac54 Mon Sep 17 00:00:00 2001 From: Dmitry Shirokov Date: Fri, 25 Sep 2020 21:27:49 +1000 Subject: [PATCH] feat: Make it work in browser --- .gitignore | 1 + README.md | 16 +++++++++++++--- package.json | 42 ++++++++++++++++++++++++++++++++++++++---- src/fs/browser.ts | 3 +++ src/fs/node.ts | 9 +++++++++ src/index.ts | 11 ++--------- 6 files changed, 66 insertions(+), 16 deletions(-) create mode 100644 src/fs/browser.ts create mode 100644 src/fs/node.ts diff --git a/.gitignore b/.gitignore index 0d608f6..05061fb 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ node_modules coverage npm-debug.log lib +TODO.md diff --git a/README.md b/README.md index d49d4dd..819a98c 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,14 @@ # chardet [![Build Status](https://travis-ci.org/runk/node-chardet.png)](https://travis-ci.org/runk/node-chardet) -Chardet is a character detection module for NodeJS written in pure Javascript. -Module is based on ICU project http://site.icu-project.org/, which uses character -occurency analysis to determine the most probable encoding. +*Chardet* is a character detection module written in pure Javascript (Typescript). Module uses occurrence analysis to determine the most probable encoding. + +- Packed size is only **22 KB** +- Works in all environments: Node / Browser / Native +- Works on all platforms: Linux / Mac / Windows +- No dependencies +- No native code / bindings +- 100% written in Typescript +- Extensive code coverage ## Installation @@ -87,3 +93,7 @@ Currently only these encodings are supported. ## Typescript? Yes. Type definitions are included. + +### References + +- ICU project http://site.icu-project.org/ diff --git a/package.json b/package.json index de04c47..d3fe8aa 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "chardet", "version": "0.0.0-development", "homepage": "https://github.com/runk/node-chardet", - "description": "Character detector", + "description": "Character encoding detector", "license": "MIT", "repository": { "type": "git", @@ -22,8 +22,8 @@ "prepublish": "npm run build", "semantic-release": "semantic-release" }, - "main": "lib/index.js", "files": ["lib"], + "main": "lib/index.js", "typings": "lib/index.d.ts", "engine": { "node": ">=4" @@ -50,7 +50,38 @@ "utf8", "detector", "chardet", - "icu" + "icu", + "character detection", + "character encoding", + "language", + "iconv", + "iconv-light", + "UTF-8", + "UTF-16", + "UTF-32", + "ISO-2022-JP", + "ISO-2022-KR", + "ISO-2022-CN", + "Shift_JIS", + "Big5", + "EUC-JP", + "EUC-KR", + "GB18030", + "ISO-8859-1", + "ISO-8859-2", + "ISO-8859-5", + "ISO-8859-6", + "ISO-8859-7", + "ISO-8859-8", + "ISO-8859-9", + "windows-1250", + "windows-1251", + "windows-1252", + "windows-1253", + "windows-1254", + "windows-1255", + "windows-1256", + "KOI8-R" ], "author": "Dmitry Shirokov ", "contributors": [ @@ -59,5 +90,8 @@ "@suisho", "@seangarner", "@zevanty" - ] + ], + "browser": { + "./lib/fs/node.js": "./lib/fs/browser.js" + } } diff --git a/src/fs/browser.ts b/src/fs/browser.ts new file mode 100644 index 0000000..9795f91 --- /dev/null +++ b/src/fs/browser.ts @@ -0,0 +1,3 @@ +export default () => { + throw new Error('File system is not available'); +}; diff --git a/src/fs/node.ts b/src/fs/node.ts new file mode 100644 index 0000000..ebbedde --- /dev/null +++ b/src/fs/node.ts @@ -0,0 +1,9 @@ +let fsModule: any; + +export default () => { + if (typeof module === 'object' && typeof module.exports === 'object') { + fsModule = fsModule ? fsModule : require('fs'); + return fsModule; + } + throw new Error('File system is not available'); +}; diff --git a/src/index.ts b/src/index.ts index e5edcb7..6f94c9c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,21 +1,14 @@ import { Match } from './match'; import { Recogniser, Context } from './encoding'; +import loadFs from './fs/node'; + import Utf8 from './encoding/utf8'; import * as unicode from './encoding/unicode'; import * as mbcs from './encoding/mbcs'; import * as sbcs from './encoding/sbcs'; import * as iso2022 from './encoding/iso2022'; -let fsModule: any; -const loadFs = () => { - if (typeof module === 'object' && typeof module.exports === 'object') { - fsModule = fsModule ? fsModule : require('fs'); - return fsModule; - } - throw new Error('File system is not available'); -} - interface FullOptions { sampleSize: number }