1 | 'use strict'
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | const packageNameRegexp = /^(?:@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*(?:@.+)?$/
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | function parse (packageName) {
|
22 | if (!packageName || !packageNameRegexp.test(packageName)) {
|
23 | return null
|
24 | }
|
25 |
|
26 | const slashIndex = packageName.indexOf('/')
|
27 | const atLastIndex = packageName.lastIndexOf('@')
|
28 | const hasScope = slashIndex !== -1
|
29 | const hasVersion = atLastIndex !== -1 && atLastIndex !== 0
|
30 |
|
31 | let scope = ''
|
32 | let name = ''
|
33 | let version = ''
|
34 |
|
35 | if (hasScope && hasVersion) {
|
36 | scope = packageName.slice(1, slashIndex)
|
37 | name = packageName.slice(slashIndex + 1, atLastIndex)
|
38 | version = packageName.slice(atLastIndex + 1)
|
39 | } else if (hasScope) {
|
40 | scope = packageName.slice(1, slashIndex)
|
41 | name = packageName.slice(slashIndex + 1)
|
42 | } else if (hasVersion) {
|
43 | name = packageName.slice(slashIndex + 1, atLastIndex)
|
44 | version = packageName.slice(atLastIndex + 1)
|
45 | } else {
|
46 | name = packageName
|
47 | }
|
48 |
|
49 | return {
|
50 | name: normalize(name),
|
51 | scope,
|
52 | version
|
53 | }
|
54 | }
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | function stringify (parsedPackageName) {
|
63 | const { scope, version } = parsedPackageName
|
64 | let name = parsedPackageName.name
|
65 |
|
66 | if (scope) {
|
67 | name = `@${scope}/${name}`
|
68 | }
|
69 |
|
70 | if (version) {
|
71 | name = `${name}@${version}`
|
72 | }
|
73 |
|
74 | return name
|
75 | }
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | function normalize (presetName) {
|
84 | return !presetName || presetName.startsWith('elint-preset')
|
85 | ? presetName
|
86 | : `elint-preset-${presetName}`
|
87 | }
|
88 |
|
89 | module.exports = {
|
90 | parse,
|
91 | stringify,
|
92 | normalize
|
93 | }
|