-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
66 lines (59 loc) · 1.55 KB
/
utils.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
// Import Node dependencies
const fs = require('fs');
/**
* Convert any string to Title Case.
*
* To convert to tileCase, there are several steps:
* - Remove excess spaces around words (leave only one)
* - Convert all words to lower case
* - Capitalize the first letter of each word
*
* @since 1.0.0
* @access public
*
* @param {string} str The string to convert.
*
* @return {string} A string in Title Case format.
*/
function toTitleCase(str) {
let niu = '';
// Remove excess spaces, convert to lowercase and split words
let words = str.replace(/\s+/g, ' ').trim().toLowerCase().split(' ');
words.forEach((word) => {
// Convert first letter to uppercase
niu += word.charAt(0).toUpperCase() + word.slice(1) + ' ';
});
// Remove the space at the end
return niu.slice(0, -1);
}
/**
* Write a JSON object to the file system.
*
* @since 1.0.0
* @access public
*
* @param {Object} obj The JSON object to save.
* @param {string} path The path of the system file to write to.
*/
function writeJSONToFile(obj, path) {
// Convert JSON object to a string
const data = JSON.stringify(obj);
// Attempt to write file to disk
fs.writeFile(path, data, 'utf8', (err) => {
if (err) {
throw err;
}
});
}
/**
* Simplify a string for easier comparison
*
* @since 1.5.2
* @access public
*
* @param {string} str The str to simplify.
*/
function simplifyString(str) {
return str.toLowerCase().replace(/[\s\n]+/, '');
}
module.exports = { toTitleCase, writeJSONToFile, simplifyString };