1 | const slugify = require( 'slugify' );
|
2 | const homedir = require( 'os' ).homedir();
|
3 |
|
4 | const sanitize = {
|
5 |
|
6 | /**
|
7 | * Sanitize path.
|
8 | * Convert ~ to absolute home directory path.
|
9 | * @param {String} input User input.
|
10 | * @return {String}
|
11 | */
|
12 | path( input ) {
|
13 | let output = input;
|
14 |
|
15 | // Change ~ to absolute home directory path.
|
16 | output = output.replace( /^~/, homedir );
|
17 |
|
18 | // Remove trailing slashes from path.
|
19 | output = output.replace( /\/+$/, '' );
|
20 |
|
21 | return output;
|
22 | },
|
23 |
|
24 | /**
|
25 | * Sanitize project name for use as project slug.
|
26 | * @param {String} name Project name.
|
27 | * @return {String}
|
28 | */
|
29 | projectName( name ) {
|
30 | const args = {
|
31 | replacement : '-',
|
32 | remove : /[^\w_ ]/,
|
33 | lower : true,
|
34 | };
|
35 | return slugify( name, args );
|
36 | },
|
37 |
|
38 | };
|
39 |
|
40 | module.exports = sanitize;
|