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

Commit 42b70f8

Browse files
authored
Close #27. Nanoid/non-secure for old ie (#34)
Close #27. Nanoid/non-secure for old ie
2 parents d359c77 + eecb478 commit 42b70f8

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

src/nanoid.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
1-
/* eslint-disable global-require */
2-
if (typeof document !== "undefined") {
1+
/* eslint-disable global-require, no-restricted-globals */
2+
let globalSelf = null
3+
4+
if (typeof window !== "undefined") {
5+
globalSelf = window
6+
} else if (typeof global !== "undefined") {
7+
globalSelf = global
8+
} else if (typeof self !== "undefined") {
9+
globalSelf = self
10+
}
11+
12+
const document = globalSelf && globalSelf.document
13+
const crypto = globalSelf && (globalSelf.crypto || globalSelf.msCrypto)
14+
const navigator = globalSelf && globalSelf.navigator
15+
16+
if (document && crypto) {
317
// web
418
module.exports = require("nanoid")
5-
} else if (
6-
typeof navigator !== "undefined" &&
7-
navigator.product === "ReactNative"
8-
) {
19+
} else if (navigator && navigator.product === "ReactNative") {
920
// react native
1021
module.exports = require("nanoid/non-secure")
1122
} else {

test/nanoid-node.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import test from "ava"
2+
3+
const expected = require("nanoid/non-secure")
4+
const imported = require("../src/nanoid")
5+
6+
test("should load non-secure nanoid", (t) => {
7+
t.true(expected === imported)
8+
})

test/nanoid-web.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import test from "ava"
22

33
global.document = {}
4+
global.crypto = {}
45

56
const expected = require("nanoid")
67
const imported = require("../src/nanoid")

0 commit comments

Comments
 (0)