Add "unpkg" package

This commit is contained in:
MICHAEL JACKSON 2017-11-25 13:10:52 -08:00
parent af788777c6
commit f3974b5e2d
8 changed files with 124 additions and 0 deletions

1
packages/unpkg/README.md Normal file
View File

@ -0,0 +1 @@
The JavaScript API for [https://unpkg.com](unpkg).

View File

@ -0,0 +1,2 @@
exports.isBareModuleIdentifier = require('./utils/isBareModuleIdentifier')
exports.parseModuleIdentifier = require('./utils/parseModuleIdentifier')

View File

@ -0,0 +1,18 @@
const isBareModuleIdentifier = require('../isBareModuleIdentifier')
describe('isBareModuleIdentifier', () => {
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
)
})
})

View File

@ -0,0 +1,38 @@
const parseBareModuleIdentifier = require('../parseBareModuleIdentifier')
describe('parseBareModuleIdentifier', () => {
it('parses simple identifiers', () => {
expect(parseBareModuleIdentifier('react')).toEqual({
packageName: 'react',
file: ''
})
})
it('parses hyphenated identifiers', () => {
expect(parseBareModuleIdentifier('react-dom')).toEqual({
packageName: 'react-dom',
file: ''
})
})
it('parses hyphenated identifiers with a filename', () => {
expect(parseBareModuleIdentifier('react-dom/server')).toEqual({
packageName: 'react-dom',
file: '/server'
})
})
it('parses scoped identifiers', () => {
expect(parseBareModuleIdentifier('@babel/core')).toEqual({
packageName: '@babel/core',
file: ''
})
})
it('parses scoped identifiers with a filename', () => {
expect(parseBareModuleIdentifier('@babel/core/package.json')).toEqual({
packageName: '@babel/core',
file: '/package.json'
})
})
})

View File

@ -0,0 +1,11 @@
const URL = require('whatwg-url')
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
)
}
module.exports = isBareModuleIdentifier

View File

@ -0,0 +1,12 @@
const bareModuleIdentifierFormat = /^((?:@[^\/]+\/)?[^\/]+)(\/.*)?$/
function parseBareModuleIdentifier(id) {
const match = bareModuleIdentifierFormat.exec(id)
return {
packageName: match[1],
file: match[2] || ''
}
}
module.exports = parseBareModuleIdentifier

View File

@ -0,0 +1,13 @@
{
"name": "unpkg",
"version": "0.1.0",
"description": "The JavaScript API for unpkg",
"files": [
"modules/*.js",
"modules/utils/*.js"
],
"main": "modules/index.js",
"dependencies": {
"whatwg-url": "^6.3.0"
}
}

29
packages/unpkg/yarn.lock Normal file
View File

@ -0,0 +1,29 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
lodash.sortby@^4.7.0:
version "4.7.0"
resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438"
punycode@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.0.tgz#5f863edc89b96db09074bad7947bf09056ca4e7d"
tr46@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/tr46/-/tr46-1.0.1.tgz#a8b13fd6bfd2489519674ccde55ba3693b706d09"
dependencies:
punycode "^2.1.0"
webidl-conversions@^4.0.1:
version "4.0.2"
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-4.0.2.tgz#a855980b1f0b6b359ba1d5d9fb39ae941faa63ad"
whatwg-url@^6.3.0:
version "6.3.0"
resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-6.3.0.tgz#597ee5488371abe7922c843397ddec1ae94c048d"
dependencies:
lodash.sortby "^4.7.0"
tr46 "^1.0.0"
webidl-conversions "^4.0.1"