-
Notifications
You must be signed in to change notification settings - Fork 4k
/
validateHookName.js
30 lines (25 loc) · 887 Bytes
/
validateHookName.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
/**
* Validate a hookName string.
*
* @param {string} hookName The hook name to validate. Should be a non empty string containing
* only numbers, letters, dashes, periods and underscores. Also,
* the hook name cannot begin with `__`.
*
* @return {bool} Whether the hook name is valid.
*/
function validateHookName( hookName ) {
if ( 'string' !== typeof hookName || '' === hookName ) {
console.error( 'The hook name must be a non-empty string.' );
return false;
}
if ( /^__/.test( hookName ) ) {
console.error( 'The hook name cannot begin with `__`.' );
return false;
}
if ( ! /^[a-zA-Z][a-zA-Z0-9_.-]*$/.test( hookName ) ) {
console.error( 'The hook name can only contain numbers, letters, dashes, periods and underscores.' );
return false;
}
return true;
}
export default validateHookName;