-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
81 lines (71 loc) · 1.77 KB
/
index.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"use strict";
/**
* @namespace AddonName
*/
// CONSTANTS
const VALIDATE_REGEX = "^[0-9a-z]{2,30}$";
/**
* @function validate
* @description validate and return a boolean to tell if the name is a valid and acceptable name for SlimIO.
* @memberof AddonName#
* @param {!string} addonName
* @returns {boolean}
*
* @example
* const assert = require("assert").strict;
*
* assert.equal(validate("Addon"), false);
* assert.equal(validate("myaddon5"), true);
* assert.equal(validate("1"), false);
*/
function validate(addonName) {
if (typeof addonName !== "string") {
return false;
}
return /^[0-9a-z]{2,30}$/g.test(addonName);
}
/**
* @function sanitize
* @description remove non-valid (wide) characters from a given string.
* @memberof AddonName#
* @param {!string} addonName
* @returns {string}
*
* @throws {TypeError}
*
* @example
* const assert = require("assert").strict;
*
* assert.equal(sanitize("Addon-Name"), "addonname");
*/
function sanitize(addonName) {
if (typeof addonName !== "string") {
throw new TypeError("addonName must be a string");
}
return addonName.replace(/[^0-9a-z]/gi, "").trim().toLowerCase().normalize();
}
/**
* @function decamelize
* @memberof AddonName#
* @description decamelize a given string
* @param {!string} text
* @returns {string}
*
* @example
* console.log(decamelize("sayHello")); // say_hello
*/
function decamelize(text) {
if (typeof text !== "string") {
throw new TypeError("text must be a string");
}
return text
.replace(/([\p{Ll}\d])(\p{Lu})/gu, "$1_$2")
.replace(/(\p{Lu}+)(\p{Lu}[\p{Ll}\d]+)/gu, "$1_$2")
.toLowerCase();
}
module.exports = {
validate,
sanitize,
decamelize,
CONSTANTS: Object.freeze({ VALIDATE_REGEX })
};