Skip to content

Commit ad1fffb

Browse files
authored
Update README.md
1 parent 968ef68 commit ad1fffb

File tree

1 file changed

+75
-10
lines changed

1 file changed

+75
-10
lines changed

README.md

Lines changed: 75 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,83 @@ Feel free to test the functionality of this NPM package [here](https://encoding-
3333
* [Known Issues](#known-issues)
3434
* [License](#license)
3535

36-
## Usage (Javascript)
36+
## Usage
37+
There are several ways in which you can use this NPM package. You can use it as a command-line interface (create a link), server-side with Node.js (create a link) or client-side in the browser (create a link).
3738

38-
### Installation
39-
```bash
40-
$ npm install detect-file-encoding-and-language
39+
### In the browser
40+
In the body section of your html file, create an input element of type `file` and give it an id.
41+
42+
```js
43+
// index.html
44+
<body>
45+
<input type="file" id="my-input-field" />
46+
<script src="app.js"></script>
47+
</body>
4148
```
4249

43-
### In the browser
50+
Next, load the module either via a `<script>` tag (create a link) or by using bundler (create a link)!
51+
52+
#### Via a `<script>` tag
53+
When loading it via the `<script>` tag, you can either use the CDN version (create a link) or download the code itself and include it in your project (create a link). The easiest and fastest to start with is using the CDN version (create a link). If you want to be able to use it offline, download and include it (create a link)!
54+
55+
##### Using the CDN version
4456
```js
4557
// index.html
4658

47-
<input type="file" id="my-input-field" >
59+
<body>
60+
<input type="file" id="my-input-field" />
61+
<script src="https://unpkg.com/detect-file-encoding-and-language/umd/language-encoding.min.js"></script>
62+
<script src="app.js"></script>
63+
</body>
64+
```
65+
66+
Now that you've loaded the module, you can finally start using it (create a link).
67+
68+
##### Downloading and including it
69+
1. Create a new folder called `lib` inside your root directory
70+
2. Inside `lib` create a new file and call it *language-encoding.min.js*
71+
3. **Important:** Make sure the encoding of your newly created file is either UTF-8 or UTF-8 with BOM!
72+
4. Go to https://unpkg.com/detect-file-encoding-and-language/umd/language-encoding.min.js and copy the code
73+
5. Paste it into *language-encoding.min.js* and save it
74+
6. Use the code below to load *language-encoding.min.js* via a `<script>` tag.
75+
76+
```js
77+
// index.html
4878

79+
<body>
80+
<input type="file" id="my-input-field" />
81+
<script src="lib/language-encoding.min.js"></script>
82+
<script src="app.js"></script>
83+
</body>
4984
```
5085

51-
> Note: This should work fine with frameworks such as React but if you're using pure vanilla Javascript make sure to use a bundler such as Browserify!
86+
##### Usage (Javascript)
87+
The `<script>` tag exposes the `languageEncoding` function to everything in the DOM that's beneath it. You should have no trouble accessing it in `app.js` by calling the languageEncoding function and passing in the file that you want to analyze as the argument. As you can see in the example below, languageEncoding returns a Promise that you can use to get the encoding, language and confidenc score.
5288

5389
```js
5490
// app.js
5591

92+
document.getElementById("my-input-field").addEventListener("change", inputHandler);
93+
94+
function inputHandler(e) {
95+
const file = e.target.files[0];
96+
97+
languageEncoding(file).then(fileInfo => console.log(fileInfo));
98+
// Possible result: { language: english, encoding: UTF-8, confidence: 0.97}
99+
}
100+
```
101+
102+
#### Using a bundler
103+
104+
##### Installation
105+
```bash
106+
$ npm install detect-file-encoding-and-language
107+
```
108+
109+
##### Usage
110+
```js
111+
// app.js
112+
56113
const languageEncoding = require("detect-file-encoding-and-language");
57114

58115
document.getElementById("my-input-field").addEventListener("change", inputHandler);
@@ -65,7 +122,16 @@ function inputHandler(e) {
65122
}
66123
```
67124

125+
Note: This works great with frameworks such as React because they are doing the bundling for you. However, if you're using pure vanilla Javascript you will have to bundle it yourself!
126+
68127
### In Node.js
128+
129+
#### Installation
130+
```bash
131+
$ npm install detect-file-encoding-and-language
132+
```
133+
134+
#### Usage
69135
```js
70136
// index.js
71137

@@ -77,14 +143,13 @@ languageEncoding(pathToFile).then(fileInfo => console.log(fileInfo));
77143
// Possible result: { language: japanese, encoding: Shift-JIS, confidence: 1 }
78144
```
79145

80-
## Usage (CLI)
146+
### In the terminal (CLI)
81147

82-
### Installation
148+
#### Installation
83149
```bash
84150
$ npm install -g detect-file-encoding-and-language
85151
```
86152

87-
### In the terminal
88153
Use the command `dfeal` to retrieve the encoding and language of your file:
89154

90155
```bash

0 commit comments

Comments
 (0)