Skip to content

Commit

Permalink
feat: add support for React Native
Browse files Browse the repository at this point in the history
Accomodate React Native and other environments that might have neither
btoa nor Buffer.
  • Loading branch information
laurent22 authored and Trott committed Oct 11, 2020
1 parent bc36285 commit 7962f8b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
31 changes: 27 additions & 4 deletions slug.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,36 @@
throw new Error('String "' + str + '" reaches code believed to be unreachable; please open an issue at https://github.com/Trott/slug/issues/new')
}

if (typeof window === 'undefined') {
base64 = function (input) {
return Buffer.from(input).toString('base64')
if (typeof window !== 'undefined') {
if (window.btoa) {
base64 = function (input) {
return btoa(unescape(encodeURIComponent(input)))
}
} else {
// Polyfill for environments that don't have btoa or Buffer class (notably, React Native).
// Based on https://github.com/davidchambers/Base64.js/blob/a121f75bb10c8dd5d557886c4b1069b31258d230/base64.js
base64 = function (input) {
var str = unescape(encodeURIComponent(input + ''))
for (
var block, charCode, idx = 0, map = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=', output = '';
str.charAt(idx | 0) || (map = '=', idx % 1);
output += map.charAt(63 & block >> 8 - idx % 1 * 8)
) {
charCode = str.charCodeAt(idx += 3 / 4)
// TODO: The if condition may be guaranteed to be false. Verify and
// remove or otherwise write a test to cover it.
/* istanbul ignore if */
if (charCode > 0xFF) {
throw new Error("'btoa' failed: The string to be encoded contains characters outside of the Latin1 range.")
}
block = block << 8 | charCode
}
return output
}
}
} else {
base64 = function (input) {
return btoa(unescape(encodeURIComponent(input)))
return Buffer.from(input).toString('base64')
}
}

Expand Down
27 changes: 27 additions & 0 deletions test/react-native.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* global after, before, describe, it */

// Only run in Node.js.
if (typeof window === 'undefined') {
describe('React Native-like environment', function () {
let slug

before(function () {
global.window = {}
delete require.cache[require.resolve('../slug')]
})
after(function () {
delete global.window
delete require.cache[require.resolve('../slug')]
})
const assert = require('assert')

it('should work for window object with no btoa function', function () {
assert.strictEqual(window.btoa, undefined)
slug = require('../slug')
assert.strictEqual(slug('鳄梨'), '6boe5qko')
assert.strictEqual(slug(String.fromCodePoint(56714, 36991)), 'iombvw')
assert.strictEqual(slug(String.fromCodePoint(56714)), 'ia')
assert.strictEqual(slug(String.fromCodePoint(55296)), 'ia')
})
})
}

0 comments on commit 7962f8b

Please sign in to comment.