@cnwhy/base64
Advanced tools
Comparing version 0.1.1 to 0.1.2
/*! | ||
* @cnwhy/base64 v0.1.1 | ||
* @cnwhy/base64 v0.1.2 | ||
* Homepage https://github.com/cnwhy/Base64.js#readme | ||
@@ -4,0 +4,0 @@ * License MIT |
@@ -8,3 +8,3 @@ (function (global, factory) { | ||
/*! | ||
* @cnwhy/base64 v0.1.1 | ||
* @cnwhy/base64 v0.1.2 | ||
* Homepage https://github.com/cnwhy/Base64.js#readme | ||
@@ -11,0 +11,0 @@ * License MIT |
{ | ||
"name": "@cnwhy/base64", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Base64 library, lossless transcoding.", | ||
@@ -5,0 +5,0 @@ "main": "dist/Base64.umd.js", |
**为什么重复造轮子?** | ||
1. `btoa` , `atob` 只支持 `Latin1` 字符. | ||
2. 常用的的 Base64 编码库处理字符串时会**主动**修改错误(空)编码字符, 导致解码的数据与原数据不一至. | ||
比如用 nodjs 中的 `Buffer`: | ||
比如用 nodejs 中的 `Buffer`: | ||
```js | ||
@@ -14,7 +14,9 @@ var s = '\ud800' | ||
### 本库方案 | ||
对于字符串的转换同样用`UTF-8`编码, 但无视无效符(解码按同一规则), 保证无损转换. | ||
`decode()` 方法返回字节数组, 不主动判定字符串及编码, 并重写`toString()`方法, 以`UTF-8`编码解析为字符串. | ||
对于字符串的转换用`UTF-8`编码, 但无视无效符(解码按同一规则), 保证js的字符([UCS-2](https://zh.wikipedia.org/wiki/UTF-16#UTF-16%E8%88%87UCS-2%E7%9A%84%E9%97%9C%E4%BF%82))串可以无损转换. | ||
`decode()` 单纯将`Base64`解析`Byte[]`; 但重写返回字节数组的`toString()`方法, 以`UTF-8`编码解析为字符串. | ||
### 适用场景 | ||
1. 二进制数据 与 Base64 互转 | ||
1. 二进制数据与Base64互转 | ||
2. 字符串与Base64互转 | ||
### Demo | ||
@@ -24,2 +26,5 @@ ```js | ||
### 兼容性 | ||
通用, 不支持`ArrayBuffer`的环境将会用`Array`代替`Uint8Array`. | ||
let str = 'Base64库\u{10400}\u{d800}'; | ||
@@ -31,4 +36,4 @@ console.log(str); // Base64库𐐀� | ||
let _str = Base64.decode(b64).toString(); | ||
console.log(b64); // | ||
console.log(str === _str) | ||
console.log(b64); | ||
console.log(str === _str) // true | ||
@@ -38,6 +43,4 @@ console.log('==Buffer==') | ||
let bf_str = Buffer.from(bf_b64,'base64').toString(); | ||
let bf_str1 = Buffer.from(b64,'base64').toString(); | ||
console.log(bf_b64); // QmFzZTY05bqT8JCQgO2ggA== | ||
console.log(bf_str1); // | ||
console.log(str === bf_str); // flase | ||
console.log(bf_b64); | ||
console.log(str === bf_str); // false | ||
@@ -50,7 +53,5 @@ //output | ||
true | ||
==Buffer== | ||
QmFzZTY05bqT8JCQgO+/vQ== | ||
false | ||
*/ | ||
``` | ||
@@ -57,0 +58,0 @@ |
// import Base64 from './Base64'; | ||
const Base64 = require('../') | ||
let str = 'Base64库\u{10400}\u{d800}'; | ||
console.log(str); // Base64库𐐀� | ||
console.log('== Base64.js ==') | ||
console.log('==Base64.js==') | ||
let b64 = Base64.encode(str); | ||
let _str = Base64.decode(b64).toString(); | ||
console.log(b64); | ||
console.log(str === _str) //true | ||
console.log(str === _str) // true | ||
console.log('== Buffer ==') | ||
console.log('==Buffer==') | ||
let bf_b64 = Buffer.from(str).toString('base64'); | ||
let bf_str = Buffer.from(bf_b64,'base64').toString(); | ||
console.log(bf_b64); | ||
console.log(str === bf_str); //flase | ||
let __str = '𐀀'; | ||
let __b64 = Base64.encode(__str); | ||
let ___str = Base64.decode(__b64).toString(); | ||
console.log(__str.split("").map(v=>v.charCodeAt(0).toString(16))) | ||
console.log(__b64); | ||
console.log(Array.from(Base64.decode(__b64)).map(v=>v.toString(2))); | ||
console.log(__str === ___str) //true | ||
let bf_str1 = Buffer.from(b64,'base64').toString(); | ||
console.log(bf_b64); | ||
console.log(bf_str1); | ||
console.log(str === bf_str); // false |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
68
37637
936