source: trip-planner-front/node_modules/validate-npm-package-name/index.js@ eed0bf8

Last change on this file since eed0bf8 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.8 KB
Line 
1'use strict'
2
3var scopedPackagePattern = new RegExp('^(?:@([^/]+?)[/])?([^/]+?)$')
4var builtins = require('builtins')
5var blacklist = [
6 'node_modules',
7 'favicon.ico'
8]
9
10var validate = module.exports = function (name) {
11 var warnings = []
12 var errors = []
13
14 if (name === null) {
15 errors.push('name cannot be null')
16 return done(warnings, errors)
17 }
18
19 if (name === undefined) {
20 errors.push('name cannot be undefined')
21 return done(warnings, errors)
22 }
23
24 if (typeof name !== 'string') {
25 errors.push('name must be a string')
26 return done(warnings, errors)
27 }
28
29 if (!name.length) {
30 errors.push('name length must be greater than zero')
31 }
32
33 if (name.match(/^\./)) {
34 errors.push('name cannot start with a period')
35 }
36
37 if (name.match(/^_/)) {
38 errors.push('name cannot start with an underscore')
39 }
40
41 if (name.trim() !== name) {
42 errors.push('name cannot contain leading or trailing spaces')
43 }
44
45 // No funny business
46 blacklist.forEach(function (blacklistedName) {
47 if (name.toLowerCase() === blacklistedName) {
48 errors.push(blacklistedName + ' is a blacklisted name')
49 }
50 })
51
52 // Generate warnings for stuff that used to be allowed
53
54 // core module names like http, events, util, etc
55 builtins.forEach(function (builtin) {
56 if (name.toLowerCase() === builtin) {
57 warnings.push(builtin + ' is a core module name')
58 }
59 })
60
61 // really-long-package-names-------------------------------such--length-----many---wow
62 // the thisisareallyreallylongpackagenameitshouldpublishdowenowhavealimittothelengthofpackagenames-poch.
63 if (name.length > 214) {
64 warnings.push('name can no longer contain more than 214 characters')
65 }
66
67 // mIxeD CaSe nAMEs
68 if (name.toLowerCase() !== name) {
69 warnings.push('name can no longer contain capital letters')
70 }
71
72 if (/[~'!()*]/.test(name.split('/').slice(-1)[0])) {
73 warnings.push('name can no longer contain special characters ("~\'!()*")')
74 }
75
76 if (encodeURIComponent(name) !== name) {
77 // Maybe it's a scoped package name, like @user/package
78 var nameMatch = name.match(scopedPackagePattern)
79 if (nameMatch) {
80 var user = nameMatch[1]
81 var pkg = nameMatch[2]
82 if (encodeURIComponent(user) === user && encodeURIComponent(pkg) === pkg) {
83 return done(warnings, errors)
84 }
85 }
86
87 errors.push('name can only contain URL-friendly characters')
88 }
89
90 return done(warnings, errors)
91}
92
93validate.scopedPackagePattern = scopedPackagePattern
94
95var done = function (warnings, errors) {
96 var result = {
97 validForNewPackages: errors.length === 0 && warnings.length === 0,
98 validForOldPackages: errors.length === 0,
99 warnings: warnings,
100 errors: errors
101 }
102 if (!result.warnings.length) delete result.warnings
103 if (!result.errors.length) delete result.errors
104 return result
105}
Note: See TracBrowser for help on using the repository browser.