A few more methods
This commit is contained in:
parent
3a309241da
commit
69a68890c5
|
@ -1 +1,3 @@
|
|||
The JavaScript API for [https://unpkg.com](unpkg).
|
||||
|
||||
** Work in progress! **
|
||||
|
|
|
@ -1,2 +1,4 @@
|
|||
exports.isBareModuleIdentifier = require("./utils/isBareModuleIdentifier")
|
||||
exports.parseModuleIdentifier = require("./utils/parseModuleIdentifier")
|
||||
exports.isLocalModuleIdentifier = require("./utils/isLocalModuleIdentifier")
|
||||
exports.isRemoteModuleIdentifier = require("./utils/isRemoteModuleIdentifier")
|
||||
exports.parseBareModuleIdentifier = require("./utils/parseBareModuleIdentifier")
|
||||
|
|
|
@ -1,16 +1,19 @@
|
|||
const isBareModuleIdentifier = require("../isBareModuleIdentifier")
|
||||
|
||||
describe("isBareModuleIdentifier", () => {
|
||||
it("returns false for local module identifiers", () => {
|
||||
expect(isBareModuleIdentifier("/absolute-path")).toBe(false)
|
||||
expect(isBareModuleIdentifier("./relative-path")).toBe(false)
|
||||
})
|
||||
|
||||
it("returns false for remote module identifiers", () => {
|
||||
expect(isBareModuleIdentifier("https://www.example.com/script.js")).toBe(false)
|
||||
expect(isBareModuleIdentifier("//www.example.com/script.js")).toBe(false)
|
||||
})
|
||||
|
||||
it("returns true for bare module identifiers", () => {
|
||||
expect(isBareModuleIdentifier("react")).toBe(true)
|
||||
expect(isBareModuleIdentifier("react-dom")).toBe(true)
|
||||
expect(isBareModuleIdentifier("react-dom/server")).toBe(true)
|
||||
})
|
||||
|
||||
it("returns false for non-bare module identifiers", () => {
|
||||
expect(isBareModuleIdentifier("/absolute-path")).toBe(false)
|
||||
expect(isBareModuleIdentifier("./relative-path")).toBe(false)
|
||||
expect(isBareModuleIdentifier("//www.example.com/script.js")).toBe(false)
|
||||
expect(isBareModuleIdentifier("https://www.example.com/script.js")).toBe(false)
|
||||
})
|
||||
})
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
const isLocalModuleIdentifier = require("../isLocalModuleIdentifier")
|
||||
|
||||
describe("isLocalModuleIdentifier", () => {
|
||||
it("returns true for local module identifiers", () => {
|
||||
expect(isLocalModuleIdentifier("/absolute-path")).toBe(true)
|
||||
expect(isLocalModuleIdentifier("./relative-path")).toBe(true)
|
||||
})
|
||||
|
||||
it("returns false for remote module identifiers", () => {
|
||||
expect(isLocalModuleIdentifier("https://www.example.com/script.js")).toBe(false)
|
||||
expect(isLocalModuleIdentifier("//www.example.com/script.js")).toBe(false)
|
||||
})
|
||||
|
||||
it("returns false for bare module identifiers", () => {
|
||||
expect(isLocalModuleIdentifier("react")).toBe(false)
|
||||
expect(isLocalModuleIdentifier("react-dom")).toBe(false)
|
||||
expect(isLocalModuleIdentifier("react-dom/server")).toBe(false)
|
||||
})
|
||||
})
|
|
@ -0,0 +1,19 @@
|
|||
const isRemoteModuleIdentifier = require("../isRemoteModuleIdentifier")
|
||||
|
||||
describe("isRemoteModuleIdentifier", () => {
|
||||
it("returns false for local module identifiers", () => {
|
||||
expect(isRemoteModuleIdentifier("/absolute-path")).toBe(false)
|
||||
expect(isRemoteModuleIdentifier("./relative-path")).toBe(false)
|
||||
})
|
||||
|
||||
it("returns true for remote module identifiers", () => {
|
||||
expect(isRemoteModuleIdentifier("https://www.example.com/script.js")).toBe(true)
|
||||
expect(isRemoteModuleIdentifier("//www.example.com/script.js")).toBe(true)
|
||||
})
|
||||
|
||||
it("returns false for bare module identifiers", () => {
|
||||
expect(isRemoteModuleIdentifier("react")).toBe(false)
|
||||
expect(isRemoteModuleIdentifier("react-dom")).toBe(false)
|
||||
expect(isRemoteModuleIdentifier("react-dom/server")).toBe(false)
|
||||
})
|
||||
})
|
|
@ -1,11 +1,8 @@
|
|||
const URL = require("whatwg-url")
|
||||
const isLocalModuleIdentifier = require("./isLocalModuleIdentifier")
|
||||
const isRemoteModuleIdentifier = require("./isRemoteModuleIdentifier")
|
||||
|
||||
function isBareModuleIdentifier(id) {
|
||||
return !(
|
||||
URL.parseURL(id) !== null || // fully qualified URL
|
||||
id.substr(0, 2) === "//" || // URL w/out protocol
|
||||
[".", "/"].includes(id.charAt(0))
|
||||
) // local path
|
||||
return !(isLocalModuleIdentifier(id) || isRemoteModuleIdentifier(id))
|
||||
}
|
||||
|
||||
module.exports = isBareModuleIdentifier
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
const isRemoteModuleIdentifier = require("./isRemoteModuleIdentifier")
|
||||
|
||||
function isLocalModuleIdentifier(id) {
|
||||
return [".", "/"].includes(id.charAt(0)) && !isRemoteModuleIdentifier(id)
|
||||
}
|
||||
|
||||
module.exports = isLocalModuleIdentifier
|
|
@ -0,0 +1,10 @@
|
|||
const URL = require("whatwg-url")
|
||||
|
||||
function isRemoteModuleIdentifier(id) {
|
||||
// Fully-qualified URL or URL w/out protocol
|
||||
return (
|
||||
URL.parseURL(id) !== null || (id.substr(0, 2) === "//" && URL.parseURL(`http:${id}`) !== null)
|
||||
)
|
||||
}
|
||||
|
||||
module.exports = isRemoteModuleIdentifier
|
Loading…
Reference in New Issue