This commit is contained in:
MICHAEL JACKSON
2017-11-25 13:25:01 -08:00
parent f3974b5e2d
commit 3a309241da
64 changed files with 635 additions and 801 deletions

View File

@ -1,85 +1,85 @@
const parsePackageURL = require('../parsePackageURL')
const parsePackageURL = require("../parsePackageURL")
describe('parsePackageURL', () => {
it('parses plain packages', () => {
expect(parsePackageURL('/history@1.0.0/umd/history.min.js')).toEqual({
pathname: '/history@1.0.0/umd/history.min.js',
search: '',
describe("parsePackageURL", () => {
it("parses plain packages", () => {
expect(parsePackageURL("/history@1.0.0/umd/history.min.js")).toEqual({
pathname: "/history@1.0.0/umd/history.min.js",
search: "",
query: {},
packageName: 'history',
packageVersion: '1.0.0',
filename: '/umd/history.min.js'
packageName: "history",
packageVersion: "1.0.0",
filename: "/umd/history.min.js"
})
})
it('parses plain packages with a hyphen in the name', () => {
expect(parsePackageURL('/query-string@5.0.0/index.js')).toEqual({
pathname: '/query-string@5.0.0/index.js',
search: '',
it("parses plain packages with a hyphen in the name", () => {
expect(parsePackageURL("/query-string@5.0.0/index.js")).toEqual({
pathname: "/query-string@5.0.0/index.js",
search: "",
query: {},
packageName: 'query-string',
packageVersion: '5.0.0',
filename: '/index.js'
packageName: "query-string",
packageVersion: "5.0.0",
filename: "/index.js"
})
})
it('parses plain packages with no version specified', () => {
expect(parsePackageURL('/query-string/index.js')).toEqual({
pathname: '/query-string/index.js',
search: '',
it("parses plain packages with no version specified", () => {
expect(parsePackageURL("/query-string/index.js")).toEqual({
pathname: "/query-string/index.js",
search: "",
query: {},
packageName: 'query-string',
packageVersion: 'latest',
filename: '/index.js'
packageName: "query-string",
packageVersion: "latest",
filename: "/index.js"
})
})
it('parses plain packages with version spec', () => {
expect(parsePackageURL('/query-string@>=4.0.0/index.js')).toEqual({
pathname: '/query-string@>=4.0.0/index.js',
search: '',
it("parses plain packages with version spec", () => {
expect(parsePackageURL("/query-string@>=4.0.0/index.js")).toEqual({
pathname: "/query-string@>=4.0.0/index.js",
search: "",
query: {},
packageName: 'query-string',
packageVersion: '>=4.0.0',
filename: '/index.js'
packageName: "query-string",
packageVersion: ">=4.0.0",
filename: "/index.js"
})
})
it('parses scoped packages', () => {
expect(parsePackageURL('/@angular/router@4.3.3/src/index.d.ts')).toEqual({
pathname: '/@angular/router@4.3.3/src/index.d.ts',
search: '',
it("parses scoped packages", () => {
expect(parsePackageURL("/@angular/router@4.3.3/src/index.d.ts")).toEqual({
pathname: "/@angular/router@4.3.3/src/index.d.ts",
search: "",
query: {},
packageName: '@angular/router',
packageVersion: '4.3.3',
filename: '/src/index.d.ts'
packageName: "@angular/router",
packageVersion: "4.3.3",
filename: "/src/index.d.ts"
})
})
it('parses package names with a period in them', () => {
expect(parsePackageURL('/index.js')).toEqual({
pathname: '/index.js',
search: '',
it("parses package names with a period in them", () => {
expect(parsePackageURL("/index.js")).toEqual({
pathname: "/index.js",
search: "",
query: {},
packageName: 'index.js',
packageVersion: 'latest',
filename: ''
packageName: "index.js",
packageVersion: "latest",
filename: ""
})
})
it('parses valid query parameters', () => {
expect(parsePackageURL('/history?main=browser')).toEqual({
pathname: '/history',
search: '?main=browser',
query: { main: 'browser' },
packageName: 'history',
packageVersion: 'latest',
filename: ''
it("parses valid query parameters", () => {
expect(parsePackageURL("/history?main=browser")).toEqual({
pathname: "/history",
search: "?main=browser",
query: { main: "browser" },
packageName: "history",
packageVersion: "latest",
filename: ""
})
})
it('returns null for invalid pathnames', () => {
expect(parsePackageURL('history')).toBe(null)
expect(parsePackageURL('/.invalid')).toBe(null)
it("returns null for invalid pathnames", () => {
expect(parsePackageURL("history")).toBe(null)
expect(parsePackageURL("/.invalid")).toBe(null)
})
})

View File

@ -1,5 +1,5 @@
const url = require('url')
const validatePackageName = require('./validatePackageName')
const url = require("url")
const validatePackageName = require("./validatePackageName")
const URLFormat = /^\/((?:@[^\/@]+\/)?[^\/@]+)(?:@([^\/]+))?(\/.*)?$/
@ -12,7 +12,7 @@ function decodeParam(param) {
}
}
return ''
return ""
}
function parsePackageURL(packageURL) {
@ -28,7 +28,7 @@ function parsePackageURL(packageURL) {
// Disallow invalid npm package names.
if (!validatePackageName(packageName)) return null
const packageVersion = decodeParam(match[2]) || 'latest'
const packageVersion = decodeParam(match[2]) || "latest"
const filename = decodeParam(match[3])
return {

View File

@ -1,4 +1,4 @@
const validateNpmPackageName = require('validate-npm-package-name')
const validateNpmPackageName = require("validate-npm-package-name")
function validatePackageName(packageName) {
return validateNpmPackageName(packageName).errors == null