Skip to content
This repository has been archived by the owner on Sep 16, 2020. It is now read-only.

Commit

Permalink
Close #27. Nanoid/non-secure for old ie (#34)
Browse files Browse the repository at this point in the history
Close #27. Nanoid/non-secure for old ie
  • Loading branch information
sergeysova authored Nov 7, 2019
2 parents d359c77 + eecb478 commit 42b70f8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/nanoid.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
/* eslint-disable global-require */
if (typeof document !== "undefined") {
/* eslint-disable global-require, no-restricted-globals */
let globalSelf = null

if (typeof window !== "undefined") {
globalSelf = window
} else if (typeof global !== "undefined") {
globalSelf = global
} else if (typeof self !== "undefined") {
globalSelf = self
}

const document = globalSelf && globalSelf.document
const crypto = globalSelf && (globalSelf.crypto || globalSelf.msCrypto)
const navigator = globalSelf && globalSelf.navigator

if (document && crypto) {
// web
module.exports = require("nanoid")
} else if (
typeof navigator !== "undefined" &&
navigator.product === "ReactNative"
) {
} else if (navigator && navigator.product === "ReactNative") {
// react native
module.exports = require("nanoid/non-secure")
} else {
Expand Down
8 changes: 8 additions & 0 deletions test/nanoid-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import test from "ava"

const expected = require("nanoid/non-secure")
const imported = require("../src/nanoid")

test("should load non-secure nanoid", (t) => {
t.true(expected === imported)
})
1 change: 1 addition & 0 deletions test/nanoid-web.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import test from "ava"

global.document = {}
global.crypto = {}

const expected = require("nanoid")
const imported = require("../src/nanoid")
Expand Down

0 comments on commit 42b70f8

Please sign in to comment.