diff --git a/src/nanoid.js b/src/nanoid.js index 3143ff7..b17c851 100644 --- a/src/nanoid.js +++ b/src/nanoid.js @@ -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 { diff --git a/test/nanoid-node.js b/test/nanoid-node.js new file mode 100644 index 0000000..b5d21fa --- /dev/null +++ b/test/nanoid-node.js @@ -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) +}) diff --git a/test/nanoid-web.js b/test/nanoid-web.js index 4a2cb58..0c8effa 100644 --- a/test/nanoid-web.js +++ b/test/nanoid-web.js @@ -1,6 +1,7 @@ import test from "ava" global.document = {} +global.crypto = {} const expected = require("nanoid") const imported = require("../src/nanoid")