Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4ddc385

Browse files
committed
Add isHEX(str)
1 parent236c7c4 commit4ddc385

File tree

10 files changed

+151
-46
lines changed

10 files changed

+151
-46
lines changed

‎.travis.yml‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
language:node_js
2+
os:linux
3+
dist:trusty
24

35
node_js:
46
-"stable"
@@ -7,16 +9,15 @@ node_js:
79
-"11.0"
810
-"10.0"
911

10-
matrix:
12+
jobs:
1113
fast_finish:true
1214
allow_failures:
1315
-node_js:'10.0'
1416

15-
sudo:false
16-
1717
cache:
1818
directories:
1919
-node_modules
20+
timeout:1440
2021

2122
before_script:
2223
-command -v npx || npm i -g npx

‎README.md‎

Lines changed: 82 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
#string-encode.js[![Build Status](https://travis-ci.org/duzun/string-encode.js.svg?branch=master)](https://travis-ci.org/duzun/string-encode.js)[![codecov](https://codecov.io/gh/duzun/string-encode.js/branch/master/graph/badge.svg)](https://codecov.io/gh/duzun/string-encode.js)
1+
#string-encode[![Build Status](https://travis-ci.org/duzun/string-encode.js.svg?branch=master)](https://travis-ci.org/duzun/string-encode.js)[![codecov](https://codecov.io/gh/duzun/string-encode.js/branch/master/graph/badge.svg)](https://codecov.io/gh/duzun/string-encode.js)
22

3-
Convert different types of JavaScript String to/from Uint8Array.
3+
- Convert different types of JavaScript`String` to/from`Uint8Array`.
4+
- Check for[`String` encoding](#string-types-table).
5+
6+
The main target of this library is the Browser, where there is no`Buffer` type.
7+
8+
Node.js is welcome too, except for`toString('base64')` which depends on`btoa`.
9+
See[Node.js equivalents](#nodejs-equivalents).
410

511
##Install
612

@@ -34,41 +40,47 @@ let buffer = str2buffer(binaryString, false);
3440
let processedSting=buffer2str(buffer,false);
3541

3642
// When you know your string might contain multibyte characters:
37-
let buffer=str2buffer(ut8EncodedStr,true);
43+
let buffer=str2buffer(mbString,true);
3844
// ...
39-
letprocessedUtf8Sting=buffer2str(buffer,true);
45+
letprocessedMbString=buffer2str(buffer,true);
4046

41-
// Let it guess whether to utf8 encode/decode or not:
47+
// Let it guess whether to utf8 encode/decode or not - not recommended:
4248
let buffer=str2buffer(anyStr);
4349
// ...
4450
let processedSting=buffer2str(buffer);
4551

4652
```
4753

48-
####Example
54+
####Example: sha1
4955

50-
Simple`sha1` function forbrowser that works with`String`, using crypto, compatible with the PHP counterpart:
56+
Simple`sha1` functionusing`crypto`forBrowser, that works with`String` and is compatible with the PHP counterpart:
5157

5258
```js
5359
import {str2buffer,toString }from'string-encode';
5460

61+
constcrypto=scope.crypto||scope.msCrypto;
62+
constsubtle=crypto.subtle||crypto.webkitSubtle;
63+
5564
asyncfunctionsha1(str,enc='hex') {
5665
let buf=str2buffer(str,true);
57-
buf=awaitcrypto.subtle.digest('SHA-1', buf);
66+
buf=awaitsubtle.digest('SHA-1', buf);
5867
buf=newUint8Array(buf);
5968
returntoString.call(buf, enc);
6069
}
70+
```
71+
72+
How to use this`sha1` function:
6173

62-
// How to use the sha1() function:
63-
awaitsha1('something');// "1af17e73721dbe0c40011b82ed4bb1a7dbe3ce29"
74+
```js
75+
awaitsha1('something');// "1af17e73721dbe0c40011b82ed4bb1a7dbe3ce29"
6476
awaitsha1('something',false);// "\u001añ~sr\u001d¾\f@\u0001\u001b\u0082íK±§ÛãÎ)"
65-
awaitsha1('что-то');// "991fe0590dfec23402d71c0e817bc7a7ab217e2b"
77+
awaitsha1('что-то');// "991fe0590dfec23402d71c0e817bc7a7ab217e2b"
6678
awaitsha1('что-то','base64');// "mR/gWQ3+wjQC1xwOgXvHp6shfis="
6779
```
6880

69-
###utf8Encode() and utf8Decode()
81+
###utf8Encode(str) and utf8Decode(str)
7082

71-
####Example
83+
####Example: btoa/atob
7284

7385
Base64 encode/decode a multibyte string:
7486

@@ -79,6 +91,20 @@ btoa(utf8Encode('⚔ или 😄')); // "4pqUINC40LvQuCDwn5iE"
7991
utf8Decode(atob('4pqUINC40LvQuCDwn5iE'));// "⚔ или 😄"
8092
```
8193

94+
##Node.js equivalents
95+
96+
|`string-encode` in Browser|`Buffer` in Node.js|
97+
| :---| :---|
98+
| str2buffer(str, false)| Buffer.from(str, 'binary')|
99+
| str2buffer(str, true)| Buffer.from(str, 'utf8')|
100+
| hex2buffer(str)| Buffer.from(str, 'hex')|
101+
| str2buffer(atob(str), false)| Buffer.from(str, 'base64')|
102+
| -| -|
103+
| buffer2str(str, false)| Buffer.toString('binary')|
104+
| buffer2str(str, true)| Buffer.toString('utf8')|
105+
| buffer2hex(str)| Buffer.toString('hex')|
106+
| btoa(buffer2str(str, false))| Buffer.toString('base64')|
107+
82108
###.toString()
83109

84110
If you want your`Uint8Array` to be one step closer to the Node.js's`Buffer`,
@@ -91,26 +117,30 @@ let buf = Uint8Array.from([65, 108, 111, 104, 97, 44]);
91117
buf.toString= toString;// the magic method
92118

93119
console.log(buf+' world!');
94-
console.log(buf.toString('hex'));// "416c6f68612c"
95-
console.log(buf.toString('base64'));// "QWxvaGEs"
120+
buf.toString('hex');// "416c6f68612c"
121+
buf.toString('base64');// "QWxvaGEs"
96122
```
97123

124+
Besides encoding/decoding, there are few more functions for testing[string encoding](#string-types-table).
125+
98126
---
99127

100128
#The theory of`String` 😉
101129

102130
A JavaScript[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) is a unicode string, which means that it is a[list of unicode characters](https://en.wikipedia.org/wiki/List_of_Unicode_characters), not a list of bytes!
103131
And it does not map one-to-one to an array of bytes without some encoding either.
104-
This is because a unicode character requires 3 bytes to be able to encode any of the growing list of137 000 symbols.
132+
This is because a unicode character requires 3 bytes to be able to encode any of the growing list ofabout 144 000 symbols.
105133
Thus`String` is not the best data type for working with binary data.
106134

107135
This is the main reason why the Node.js devs have come up with the[Buffer](https://nodejs.org/api/buffer.html) type.
108-
Later on there have been invented the[TypedArray](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) standard to the rescue and the Node.js devs have adopted the new typeas the parent type for the existing`Buffer` type (starting with Node.js v4).
136+
Later on there have been invented the[TypedArray](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) standard to the rescue and the Node.js devs have adopted the new type, namely`Uint8Array`,as the parent type for the existing`Buffer` type,starting with Node.js v4.
109137

110138
Meanwhile there have been written many libraries to encode, encrypt, hash or otherwise transform the data, all using the plain`String` type that was available to the community since the beginning of JS.
111139

112140
Even some browser built-in functions that came before the`TypedArray` standard rely on the`String` type to do their encoding (eg.[btoa](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/btoa) == "binary to ASCII").
113141

142+
Today, if you want to manipulate some bytes in JavaScript, you most likely need a`Uint8Array` instead of a`String` for best performance and compatibility with other environments and tools.
143+
114144
##String kinds (or encodings)
115145

116146
Judging by content, there are a few kinds of JS`String`s used in almost all applications.
@@ -145,24 +175,27 @@ ord(mbStr[2]); // 9876
145175

146176
Most encoding algorithms would not accept a multibyte`String`.
147177

148-
###ASCII
149-
150-
A subset of binary strings is[**ASCII**](https://www.asciitable.com/) only strings,
151-
which represent the class of strings with character codes in the range[0..127].
152-
Each ASCII character can be represented with only 7 bits.
178+
If you try to run`btoa('€')`, you'll get an error like:
153179

154180
```js
155-
constasciiStr='Any text using the 26 English letters, digits and punctuation!';
156-
isASCII(asciiStr);// true
157-
158-
isASCII(binStr);// false
159-
isASCII(utf8Str);// false
181+
UncaughtDOMException:
182+
Failed to execute'btoa' on'Window':
183+
The string to be encoded contains characters outsideof the Latin1 range.
160184
```
161185

186+
Because`` is a multibyte character.
187+
188+
The solution is to encode the multibyte string into a singe-byte string somehow.
189+
162190
###UTF8 encoded
163191

164-
[UTF8](https://en.wikipedia.org/wiki/UTF-8) is the most used byte encoding of unicode/multibyte strings in computers today. It is the default encoding of web pages that travel over the wire (`content-type: text/html; charset=UTF-8`) and the default in many programing languages.
165-
The important feature of UTF8 is that it is fully compatible with ASCII strings, which means any ASCII string is also a valid UTF8 encoded string. Unless you need symbols outside the ASCII table, this encoding is very compact, and uses more than a byte per character only where needed.
192+
[UTF8](https://en.wikipedia.org/wiki/UTF-8) is the most widely used byte encoding of unicode/multibyte strings in computers today.
193+
It is the default encoding of web pages that travel over the wire (`content-type: text/html; charset=UTF-8`)
194+
and the default in many programing languages.
195+
The important feature of UTF8 is that it is fully compatible with ASCII strings,
196+
which means any ASCII string is also a valid UTF8 encoded string.
197+
Unless you need symbols outside the ASCII table, this encoding is very compact,
198+
and uses more than a byte per character only where needed.
166199

167200
```js
168201
constmbStr='$ ⚔ ₽ 😄 € ™';
@@ -176,17 +209,32 @@ btoa(utf8Str); // '4oK9IOKalCAkIPCfmIQg4oKsIOKEog=='
176209
str2buffer(utf8Str);// Uint8Array([226, 130, 189, 32, 226, 154, 148, 32, 36, 32, 240, 159, 152, 132, 32, 226, 130, 172, 32, 226, 132, 162])
177210
```
178211

179-
Even though`utf8Str` is still a`String`, it is no longer a multibyte string.
212+
Even though`utf8Str` is still of type`String`, it is no longer a multibyte string,
213+
and thus can be manipulated as an array of bytes.
214+
215+
###ASCII
216+
217+
A subset of binary strings is[**ASCII**](https://www.asciitable.com/) only strings,
218+
which represent the class of strings with character codes in the range[0..127].
219+
Each ASCII character can be represented with only 7 bits.
220+
221+
```js
222+
constasciiStr='Any text using the 26 English letters, digits and punctuation!';
223+
isASCII(asciiStr);// true
224+
225+
isASCII(binStr);// false
226+
isASCII(utf8Str);// false
227+
```
180228

181229
---
182230

183231
##String Types Table
184232

185-
All tableheaders are functions exported by this library.
233+
All tableheadings are functions exported by this library.
186234

187235
| String| guessEncoding| hasMultibyte| isBinary| isASCII| isUTF8| utf8bytes|
188236
|:-------------------------:|:-------------:|:------------:|:--------:|:-------:|:------:|:---------:|
189-
| ""|ascii| false| true| true| true| 0|
237+
| ""| hex| false| true| true| true| 0|
190238
| "English alphabet is 26"| ascii| false| true| true| true| 0|
191239
| "$ ⚔ ₽ 😄 € ™"| mb| "⚔"| false| false| false| false|
192240
| utf8Encode("$ ⚔ ₽ 😄 € ™")| utf8| false| true| false| true| 16|
@@ -195,6 +243,8 @@ All table headers are functions exported by this library.
195243
| utf8Decode("Xש")| mb| "Xש"| false| false| false| false|
196244
| "© binary? ×"|~utf8| false| true| false| false| false\| 2|
197245

246+
I did not add the`isHEX` column because it is a trivial format - you can't confuse it with the others.
247+
198248
**Note 1:**
199249

200250
Sometimes you can't tell whether the string has been`utf8Encode`ed

‎dist/string-encode.js‎

Lines changed: 23 additions & 3 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp