abab
WHATWG spec-compliant implementations of window.atob and window.btoa.
Last updated 8 months ago by jeffcarp .
BSD-3-Clause · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install abab 
SYNC missed versions from official npm registry.

abab npm version Build Status

A JavaScript module that implements window.atob and window.btoa according the forgiving-base64 algorithm in the Infra Standard. The original code was forked from w3c/web-platform-tests.

Compatibility: Node.js version 3+ and all major browsers.

Install with npm:

npm install abab

API

btoa (base64 encode)

const { btoa } = require('abab');
btoa('Hello, world!'); // 'SGVsbG8sIHdvcmxkIQ=='

atob (base64 decode)

const { atob } = require('abab');
atob('SGVsbG8sIHdvcmxkIQ=='); // 'Hello, world!'

Valid characters

Per the spec, btoa will accept strings "containing only characters in the range U+0000 to U+00FF." If passed a string with characters above U+00FF, btoa will return null. If atob is passed a string that is not base64-valid, it will also return null. In both cases when null is returned, the spec calls for throwing a DOMException of type InvalidCharacterError.

Browsers

If you want to include just one of the methods to save bytes in your client-side code, you can require the desired module directly.

const atob = require('abab/lib/atob');
const btoa = require('abab/lib/btoa');

Checklists

If you're submitting a PR or deploying to npm, please use the checklists in CONTRIBUTING.md

Remembering atob vs. btoa

Here's a mnemonic that might be useful: if you have a plain string and want to base64 encode it, then decode it, btoa is what you run before (before - btoa), and atob is what you run after (after - atob).

Current Tags

  • 2.0.3                                ...           latest (8 months ago)

9 Versions

  • 2.0.3                                ...           8 months ago
  • 2.0.2                                ...           10 months ago
  • 2.0.1                                ...           a year ago
  • 2.0.0                                ...           2 years ago
  • 1.0.4                                ...           3 years ago
  • 1.0.3                                ...           5 years ago
  • 1.0.2                                ...           5 years ago
  • 1.0.1                                ...           5 years ago
  • 1.0.0                                ...           5 years ago
Maintainers (1)
Downloads
Today 5,586
This Week 39,019
This Month 189,515
Last Day 16,910
Last Week 95,766
Last Month 352,577
Dependencies (0)
None
Dev Dependencies (8)
Dependents (176)

Copyright 2014 - 2017 © taobao.org |