-
Notifications
You must be signed in to change notification settings - Fork 812
/
Copy pathprojectNameTransformations.js
82 lines (78 loc) · 2.58 KB
/
projectNameTransformations.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
82
/**
* Transforms a project name into a Readable name
*
* Example: project-slug into Project Slug
*
* @param {string} name - The project name
* @param {boolean} jetpackPrefix - Whether to prefix the name with Jetpack, default true.
* @return {string} The transformed string
*/
export function transformToReadableName( name, jetpackPrefix = true ) {
let readableName = name.replace( /[-._][a-z]/g, m => {
return ' ' + m[ 1 ].toUpperCase();
} );
readableName = readableName.charAt( 0 ).toUpperCase() + readableName.slice( 1 );
if ( jetpackPrefix ) {
readableName = 'Jetpack ' + readableName;
}
return readableName;
}
/**
* Normalize project name as slug
*
* Example: pro.ject_different-slug into pro-ject-different-slug
*
* @param {string} name - The project name
* @param {boolean} jetpackPrefix - Whether to prefix the name with Jetpack
* @param {string} separator - The separator to use
* @return {string} The transformed string
*/
export function normalizeSlug( name, jetpackPrefix = true, separator = '-' ) {
let slug = name.replace( /[-._]/g, separator );
if ( jetpackPrefix ) {
slug = 'jetpack-' + slug;
}
return slug;
}
/**
* Transforms a project name into the PHP Class name format
*
* Example: project-name into Project_Name
*
* @param {string} name - The project name
* @param {boolean} jetpackPrefix - Whether to prefix the name with Jetpack
* @return {string} The transformed string
*/
export function transformToPhpClassName( name, jetpackPrefix = true ) {
return transformToReadableName( name, jetpackPrefix ).replaceAll( ' ', '_' );
}
/**
* Transforms a project name into the PHP Constant name format
*
* Example: project-name into PROJECT_NAME
*
* @param {string} name - The project name
* @param {boolean} jetpackPrefix - Whether to prefix the name with Jetpack
* @return {string} The transformed string
*/
export function transformToPhpConstantName( name, jetpackPrefix = true ) {
return transformToPhpClassName( name, jetpackPrefix ).toUpperCase();
}
/**
* Transforms a project name into javascript variable camel case
*
* Example: project-name into projectName
*
* @param {string} name - The project name
* @param {boolean} jetpackPrefix - Whether to prefix the name with Jetpack
* @return {string} The transformed string
*/
export function transformToCamelCase( name, jetpackPrefix = true ) {
let slug = transformToReadableName( name, false ).replaceAll( ' ', '' );
if ( jetpackPrefix ) {
slug = 'jetpack' + slug;
} else {
slug = slug.charAt( 0 ).toLowerCase() + slug.slice( 1 );
}
return slug;
}