Skip to content

Commit 3383ac5

Browse files
authored
feat: add nerf-dart package (#24)
ref pnpm/pnpm#9492
1 parent 19bd04c commit 3383ac5

File tree

10 files changed

+236
-50
lines changed

10 files changed

+236
-50
lines changed

.bitmap

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,20 @@
7979
"mainFile": "index.ts",
8080
"rootDir": "util/lex-comparator"
8181
},
82+
"nerf-dart": {
83+
"name": "nerf-dart",
84+
"scope": "",
85+
"version": "",
86+
"defaultScope": "pnpm.config",
87+
"mainFile": "index.ts",
88+
"rootDir": "config/nerf-dart",
89+
"config": {
90+
"pnpm.env/envs/pnpm-env": {},
91+
"teambit.envs/envs": {
92+
"env": "pnpm.env/envs/pnpm-env"
93+
}
94+
}
95+
},
8296
"policy": {
8397
"name": "policy",
8498
"scope": "pnpm.builder",

config/nerf-dart/LICENSE

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
The ISC License
2+
3+
Copyright (c) npm, Inc.
4+
5+
Permission to use, copy, modify, and/or distribute this software for any
6+
purpose with or without fee is hereby granted, provided that the above
7+
copyright notice and this permission notice appear in all copies.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
15+
IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

config/nerf-dart/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { nerfDart } from './nerf-dart'

config/nerf-dart/nerf-dart.docs.mdx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
labels: ['NerfDart', 'module']
3+
description: 'Maps a URL to an identifier.'
4+
---
5+
6+
```ts
7+
import { nerfDart } from 'nerf-dart'
8+
9+
nerfDart('http://registry.npmjs.org')
10+
//> //registry.npmjs.org/
11+
```
12+
13+
Originally from npm. Taken from <https://github.com/npm/cli/blob/latest/workspaces/config/lib/nerf-dart.js> and made available as a standalone package for easier reuse.

config/nerf-dart/nerf-dart.spec.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { nerfDart } from './nerf-dart'
2+
3+
test.each([
4+
['//registry.npmjs.org/', [
5+
'https://registry.npmjs.org',
6+
'https://registry.npmjs.org/package-name',
7+
'https://registry.npmjs.org/package-name?write=true',
8+
'https://registry.npmjs.org/@scope%2fpackage-name',
9+
'https://registry.npmjs.org/@scope%2fpackage-name?write=true',
10+
'https://username:[email protected]/package-name?write=true',
11+
'https://registry.npmjs.org/#hash',
12+
'https://registry.npmjs.org/?write=true#hash',
13+
'https://registry.npmjs.org/package-name?write=true#hash',
14+
'https://registry.npmjs.org/package-name#hash',
15+
'https://registry.npmjs.org/@scope%2fpackage-name?write=true#hash',
16+
'https://registry.npmjs.org/@scope%2fpackage-name#hash',
17+
]],
18+
['//my-couch:5984/registry/_design/app/rewrite/', [
19+
'https://my-couch:5984/registry/_design/app/rewrite/',
20+
'https://my-couch:5984/registry/_design/app/rewrite/package-name',
21+
'https://my-couch:5984/registry/_design/app/rewrite/package-name?write=true',
22+
'https://my-couch:5984/registry/_design/app/rewrite/@scope%2fpackage-name',
23+
'https://my-couch:5984/registry/_design/app/rewrite/@scope%2fpackage-name?write=true',
24+
'https://username:password@my-couch:5984/registry/_design/app/rewrite/package-name?write=true',
25+
'https://my-couch:5984/registry/_design/app/rewrite/#hash',
26+
'https://my-couch:5984/registry/_design/app/rewrite/?write=true#hash',
27+
'https://my-couch:5984/registry/_design/app/rewrite/package-name?write=true#hash',
28+
'https://my-couch:5984/registry/_design/app/rewrite/package-name#hash',
29+
'https://my-couch:5984/registry/_design/app/rewrite/@scope%2fpackage-name?write=true#hash',
30+
'https://my-couch:5984/registry/_design/app/rewrite/@scope%2fpackage-name#hash',
31+
]],
32+
])('nerfDart', (dart, tests) => {
33+
expect.assertions(tests.length)
34+
for (const url of tests) {
35+
expect(nerfDart(url)).toBe(dart)
36+
}
37+
})
38+
39+
test('nerfDart to throw', () => {
40+
expect(() => nerfDart('not a valid url')).toThrow()
41+
})

config/nerf-dart/nerf-dart.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { URL } from 'url'
2+
3+
/**
4+
* Maps a URL to an identifier.
5+
*
6+
* Name courtesy schiffertronix media LLC, a New Jersey corporation
7+
*
8+
* @param {String} url The URL to be nerfed.
9+
*
10+
* @returns {String} A nerfed URL.
11+
*/
12+
export function nerfDart (url: string): string {
13+
const parsed = new URL(url)
14+
const from = `${parsed.protocol}//${parsed.host}${parsed.pathname}`
15+
const rel = new URL('.', from)
16+
const res = `//${rel.host}${rel.pathname}`
17+
return res
18+
}

env/envs/pnpm-env/types/nerf.d.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
declare module 'nerf-dart' {
2-
export default function (uri: string): string;
3-
}

network/config/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import nerfDart from 'nerf-dart';
1+
import { nerfDart } from '@pnpm/config.nerf-dart';
22

33
function getMaxParts(uris: string[]) {
44
return uris.reduce((max, uri) => {

0 commit comments

Comments
 (0)