feat: Make it work in browser

This commit is contained in:
Dmitry Shirokov 2020-09-25 21:27:49 +10:00
parent b1761347d5
commit 1b40042222
No known key found for this signature in database
GPG Key ID: 0D8CF8C72764BA46
6 changed files with 66 additions and 16 deletions

1
.gitignore vendored
View File

@ -4,3 +4,4 @@ node_modules
coverage coverage
npm-debug.log npm-debug.log
lib lib
TODO.md

View File

@ -1,8 +1,14 @@
# chardet [![Build Status](https://travis-ci.org/runk/node-chardet.png)](https://travis-ci.org/runk/node-chardet) # 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. *Chardet* is a character detection module written in pure Javascript (Typescript). Module uses occurrence analysis to determine the most probable encoding.
Module is based on ICU project http://site.icu-project.org/, which uses character
occurency 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 ## Installation
@ -87,3 +93,7 @@ Currently only these encodings are supported.
## Typescript? ## Typescript?
Yes. Type definitions are included. Yes. Type definitions are included.
### References
- ICU project http://site.icu-project.org/

View File

@ -2,7 +2,7 @@
"name": "chardet", "name": "chardet",
"version": "0.0.0-development", "version": "0.0.0-development",
"homepage": "https://github.com/runk/node-chardet", "homepage": "https://github.com/runk/node-chardet",
"description": "Character detector", "description": "Character encoding detector",
"license": "MIT", "license": "MIT",
"repository": { "repository": {
"type": "git", "type": "git",
@ -22,8 +22,8 @@
"prepublish": "npm run build", "prepublish": "npm run build",
"semantic-release": "semantic-release" "semantic-release": "semantic-release"
}, },
"main": "lib/index.js",
"files": ["lib"], "files": ["lib"],
"main": "lib/index.js",
"typings": "lib/index.d.ts", "typings": "lib/index.d.ts",
"engine": { "engine": {
"node": ">=4" "node": ">=4"
@ -50,7 +50,38 @@
"utf8", "utf8",
"detector", "detector",
"chardet", "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 <deadrunk@gmail.com>", "author": "Dmitry Shirokov <deadrunk@gmail.com>",
"contributors": [ "contributors": [
@ -59,5 +90,8 @@
"@suisho", "@suisho",
"@seangarner", "@seangarner",
"@zevanty" "@zevanty"
] ],
"browser": {
"./lib/fs/node.js": "./lib/fs/browser.js"
}
} }

3
src/fs/browser.ts Normal file
View File

@ -0,0 +1,3 @@
export default () => {
throw new Error('File system is not available');
};

9
src/fs/node.ts Normal file
View File

@ -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');
};

View File

@ -1,21 +1,14 @@
import { Match } from './match'; import { Match } from './match';
import { Recogniser, Context } from './encoding'; import { Recogniser, Context } from './encoding';
import loadFs from './fs/node';
import Utf8 from './encoding/utf8'; import Utf8 from './encoding/utf8';
import * as unicode from './encoding/unicode'; import * as unicode from './encoding/unicode';
import * as mbcs from './encoding/mbcs'; import * as mbcs from './encoding/mbcs';
import * as sbcs from './encoding/sbcs'; import * as sbcs from './encoding/sbcs';
import * as iso2022 from './encoding/iso2022'; 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 { interface FullOptions {
sampleSize: number sampleSize: number
} }