forked from package-url/packageurl-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.js
34 lines (31 loc) · 878 Bytes
/
error.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
'use strict'
function formatPurlErrorMessage(message = '') {
const { length } = message
let formatted = ''
if (length) {
// Lower case start of message.
const code0 = message.charCodeAt(0)
formatted =
code0 >= 65 /*'A'*/ || code0 <= 90 /*'Z'*/
? `${message[0].toLowerCase()}${message.slice(1)}`
: message
// Remove period from end of message.
if (
length > 1 &&
message.charCodeAt(length - 1) === 46 /*'.'*/ &&
message.charCodeAt(length - 2) !== 46
) {
formatted = formatted.slice(0, -1)
}
}
return `Invalid purl: ${formatted}`
}
class PurlError extends Error {
constructor(message) {
super(formatPurlErrorMessage(message))
}
}
module.exports = {
formatPurlErrorMessage,
PurlError
}