Skip to content

Commit

Permalink
feat: Make it work in browser
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Shirokov committed Sep 25, 2020
1 parent b176134 commit 1b40042
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_modules
coverage
npm-debug.log
lib
TODO.md
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -87,3 +93,7 @@ Currently only these encodings are supported.
## Typescript?

Yes. Type definitions are included.

### References

- ICU project http://site.icu-project.org/
42 changes: 38 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
Expand All @@ -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 <[email protected]>",
"contributors": [
Expand All @@ -59,5 +90,8 @@
"@suisho",
"@seangarner",
"@zevanty"
]
],
"browser": {
"./lib/fs/node.js": "./lib/fs/browser.js"
}
}
3 changes: 3 additions & 0 deletions src/fs/browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default () => {
throw new Error('File system is not available');
};
9 changes: 9 additions & 0 deletions src/fs/node.ts
Original file line number Diff line number Diff line change
@@ -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');
};
11 changes: 2 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down

0 comments on commit 1b40042

Please sign in to comment.