Skip to content
 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

216 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

es2-base64

dankogai 氏の js-base64 を元に、正規表現を不使用としました。正規表現をサポートしない Windows CE 版 Internet Explorer 4.01 でも動作します。(未確認)

Closure Compiler で必要な関数だけをプロジェクトに組み込み、高度に最適化する想定で @define を定義し externs.js を用意しています。

Closure Compiler を使った最適化は gulpfile.js を参照します。

命名ルール

僕のWeb開発の、変数と関数、ファイル名とフォルダ名の命名ルール

mocha test

npm install
npm test

Build

gulp dist

Usage

In Browser

<script src="base64.js"></script>

node.js

var Base64 = require('js-base64').Base64;

SYNOPSIS

Base64.encode('dankogai'); // ZGFua29nYWk=
Base64.btoa(  'dankogai'); // ZGFua29nYWk=
Base64.fromUint8Array(     // ZGFua29nYWk=
    new Uint8Array([100,97,110,107,111,103,97,105])
);
Base64.fromUint8Array(     // ZGFua29nYW which is URI safe
    new Uint8Array([100,97,110,107,111,103,97,105]), true
);
Base64.encode(   '小飼弾'); // 5bCP6aO85by+
Base64.encodeURI('小飼弾'); // 5bCP6aO85by- which equals to Base64.encode('小飼弾', true)
Base64.btoa(     '小飼弾'); // raises exception 
Base64.decode('ZGFua29nYWk=');  // dankogai
Base64.atob(  'ZGFua29nYWk=');  // dankogai
Base64.toUint8Array(            // new Uint8Array([100,97,110,107,111,103,97,105])
    'ZGFua29nYWk='
);
Base64.decode('5bCP6aO85by+');  // 小飼弾
// note .decodeURI() is unnecessary since it accepts both flavors
Base64.decode('5bCP6aO85by-');  // 小飼弾
Base64.atob(  '5bCP6aO85by+');  // '�飼弾' which is nonsense

.decode() vs .atob (and .encode() vs btoa())

Suppose you have:

var pngBase64 = 
  "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=";

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.

SEE ALSO

About

Base64 implementation with no RegEXp.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Used by

Contributors

Languages