25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

README.md 2.4 KiB

3 yıl önce
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. ## Install
  5. ```javascript
  6. $ npm install --save js-base64
  7. ```
  8. If you are using it on ES6 transpilers, you may also need:
  9. ```javascript
  10. $ npm install --save babel-preset-env
  11. ```
  12. 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`.
  13. ## Usage
  14. ### In Browser
  15. ```html
  16. <script src="base64.js"></script>
  17. ```
  18. ### node.js
  19. ```javascript
  20. var Base64 = require('js-base64').Base64;
  21. ```
  22. ## es6+
  23. ```javascript
  24. import { Base64 } from 'js-base64';
  25. ```
  26. ## SYNOPSIS
  27. ```javascript
  28. Base64.encode('dankogai'); // ZGFua29nYWk=
  29. Base64.encode('小飼弾'); // 5bCP6aO85by+
  30. Base64.encodeURI('小飼弾'); // 5bCP6aO85by-
  31. Base64.decode('ZGFua29nYWk='); // dankogai
  32. Base64.decode('5bCP6aO85by+'); // 小飼弾
  33. // note .decodeURI() is unnecessary since it accepts both flavors
  34. Base64.decode('5bCP6aO85by-'); // 小飼弾
  35. ```
  36. ### String Extension for ES5
  37. ```javascript
  38. if (Base64.extendString) {
  39. // you have to explicitly extend String.prototype
  40. Base64.extendString();
  41. // once extended, you can do the following
  42. 'dankogai'.toBase64(); // ZGFua29nYWk=
  43. '小飼弾'.toBase64(); // 5bCP6aO85by+
  44. '小飼弾'.toBase64(true); // 5bCP6aO85by-
  45. '小飼弾'.toBase64URI(); // 5bCP6aO85by-
  46. 'ZGFua29nYWk='.fromBase64(); // dankogai
  47. '5bCP6aO85by+'.fromBase64(); // 小飼弾
  48. '5bCP6aO85by-'.fromBase64(); // 小飼弾
  49. }
  50. ```
  51. ### TypeScript
  52. TypeScript 2.0 type definition was added to the [DefinitelyTyped repository](https://github.com/DefinitelyTyped/DefinitelyTyped).
  53. ```bash
  54. $ npm install --save @types/js-base64
  55. ```
  56. ## `.decode()` vs `.atob` (and `.encode()` vs `btoa()`)
  57. Suppose you have:
  58. ```
  59. var pngBase64 =
  60. "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=";
  61. ```
  62. 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.
  63. ## SEE ALSO
  64. + http://en.wikipedia.org/wiki/Base64