用工小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.3 KiB

6 months ago
  1. [![build status](https://secure.travis-ci.org/dankogai/js-base64.png)](http://travis-ci.org/dankogai/js-base64)
  2. # base64.js
  3. Yet another Base64 transcoder
  4. ## Usage
  5. ### Install
  6. ```javascript
  7. $ npm install --save js-base64
  8. ```
  9. If you are using it on ES6 transpilers, you may also need:
  10. ```javascript
  11. $ npm install --save babel-preset-env
  12. ```
  13. Note `js-base64` itself is stand-alone so its `package.json` has no `dependencies`.  However, it is also tested on ES6 environment so `"babel-preset-env": "^1.7.0"` is on `devDependencies`.
  14. ### In Browser
  15. * Locally
  16. ```html
  17. <script src="base64.js"></script>
  18. ```
  19. * Directly from CDN. In which case you don't even need to install.
  20. ```html
  21. <!-- the latest -->
  22. <script src="https://cdn.jsdelivr.net/npm/js-base64/base64.min.js">
  23. ```
  24. ```html
  25. <!-- with version fixed -->
  26. <script src="https://cdn.jsdelivr.net/npm/js-base64@2.6.4/base64.min.js">
  27. ```
  28. ### node.js
  29. ```javascript
  30. var Base64 = require('js-base64').Base64;
  31. ```
  32. ## es6+
  33. ```javascript
  34. import { Base64 } from 'js-base64';
  35. ```
  36. ## SYNOPSIS
  37. ```javascript
  38. Base64.encode('dankogai'); // ZGFua29nYWk=
  39. Base64.btoa( 'dankogai'); // ZGFua29nYWk=
  40. Base64.fromUint8Array( // ZGFua29nYWk=
  41. new Uint8Array([100,97,110,107,111,103,97,105])
  42. );
  43. Base64.fromUint8Array( // ZGFua29nYW which is URI safe
  44. new Uint8Array([100,97,110,107,111,103,97,105]), true
  45. );
  46. Base64.encode( '小飼弾'); // 5bCP6aO85by+
  47. Base64.encodeURI('小飼弾'); // 5bCP6aO85by- which equals to Base64.encode('小飼弾', true)
  48. Base64.btoa( '小飼弾'); // raises exception
  49. ```
  50. ```javascript
  51. Base64.decode('ZGFua29nYWk='); // dankogai
  52. Base64.atob( 'ZGFua29nYWk='); // dankogai
  53. Base64.toUint8Array( // new Uint8Array([100,97,110,107,111,103,97,105])
  54. 'ZGFua29nYWk='
  55. );
  56. Base64.decode('5bCP6aO85by+'); // 小飼弾
  57. // note .decodeURI() is unnecessary since it accepts both flavors
  58. Base64.decode('5bCP6aO85by-'); // 小飼弾
  59. Base64.atob( '5bCP6aO85by+'); // '小飼弾' which is nonsense
  60. ```
  61. ### String Extension for ES5
  62. ```javascript
  63. if (Base64.extendString) {
  64. // you have to explicitly extend String.prototype
  65. Base64.extendString();
  66. // once extended, you can do the following
  67. 'dankogai'.toBase64(); // ZGFua29nYWk=
  68. '小飼弾'.toBase64(); // 5bCP6aO85by+
  69. '小飼弾'.toBase64(true); // 5bCP6aO85by-
  70. '小飼弾'.toBase64URI(); // 5bCP6aO85by-
  71. 'ZGFua29nYWk='.fromBase64(); // dankogai
  72. '5bCP6aO85by+'.fromBase64(); // 小飼弾
  73. '5bCP6aO85by-'.fromBase64(); // 小飼弾
  74. }
  75. ```
  76. ### TypeScript
  77. TypeScript 2.0 type definition was added to the [DefinitelyTyped repository](https://github.com/DefinitelyTyped/DefinitelyTyped).
  78. ```bash
  79. $ npm install --save @types/js-base64
  80. ```
  81. ## `.decode()` vs `.atob` (and `.encode()` vs `btoa()`)
  82. Suppose you have:
  83. ```
  84. var pngBase64 =
  85. "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=";
  86. ```
  87. Which is a Base64-encoded 1x1 transparent PNG, **DO NOT USE** `Base64.decode(pngBase64)`.  Use `Base64.atob(pngBase64)` instead.  `Base64.decode()` decodes to UTF-8 string while `Base64.atob()` decodes to bytes, which is compatible to browser built-in `atob()` (Which is absent in node.js).  The same rule applies to the opposite direction.
  88. ## SEE ALSO
  89. + http://en.wikipedia.org/wiki/Base64