-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
113 lines (95 loc) · 3.65 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const path = require('path')
const fs = require('fs')
/**
* Save content in a file using utf8 format.
*
* @param {string} filename The filename to create. It can also include a path ending with the filename. Path will be created if not exists.
* @param {string} content The content to place in the file.
* @throws {Error} If input is invalid or if there was an error during the save process.
*/
function saveDocument(filename, content) {
// Validate inputs
if (typeof filename !== 'string' || typeof content !== 'string') {
throw new Error('Invalid input: `filename` and `content` must be strings.');
}
// Check if filename contains invalid characters
const invalidChars = /[<>:"/\\|?*\x00-\x1F]/g;
if (invalidChars.test(path.basename(filename))) {
throw new Error('Invalid filename: contains prohibited characters.');
}
// Ensure the filename is an absolute path
if (!path.isAbsolute(filename)) {
filename = path.resolve(filename);
}
// Create directory if it doesn't exist
const dir = path.dirname(filename);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
// Write content to file
fs.writeFileSync(filename, content, 'utf8');
}
/**
* Append specified extension if needed.
*
* @param {string} filename The filename to check for an existing extension.
* @param {string} extension The extension to append if filename has no extension. It should start with a dot (e.g. `.txt`)
* @returns {string} filename with either existing or specified extension
* @throws {TypeError} If filename or extension is not a string.
* @throws {Error} If extension is empty or doesn't start with a dot.
*/
function setDefaultExtension(filename, extension) {
// Validate inputs
const validExtensionPattern = /^\.[a-zA-Z0-9]+$/;
if (typeof filename !== 'string') {
throw new TypeError('Invalid input: `filename` must be a string.');
}
if (typeof extension !== 'string') {
throw new TypeError('Invalid input: `extension` must be a string.');
}
if (filename.trim() === '') {
throw new Error('Invalid input: `filename` cannot be empty.');
}
// Validate extension
if (!validExtensionPattern.test(extension)) {
throw new Error('Invalid extension: must start with a dot and contain only alphanumeric characters.');
}
// Return filename with existing extension, or append the specified extension
if (path.extname(filename)) {
return filename;
}
return `${filename}${extension}`;
}
/**
* Convert a string into an identifier.
*
* @param {string} s The string to convert.
* @returns {string} The identifier string.
*
* The following operations are performed on the string:
* 1. Trim and convert to lower case.
* 2. Remove diacritics (transliterate or remove non-ASCII characters).
* 3. Replace spaces and special characters with hyphens, allowing underscores.
* 4. Replace multiple hyphens with a single hyphen.
* 5. Remove leading and trailing hyphens.
*/
function slug(s) {
if (typeof s !== 'string') {
throw new TypeError('Input must be a string');
}
s = s.trim().toLowerCase();
// Transliterate or remove non-ASCII characters
s = s.normalize('NFD').replace(/[\u0300-\u036f]/g, ''); // Remove diacritics
// Replace spaces and special characters with hyphens, allowing underscores
s = s.replace(/[^a-z0-9-_]+/g, '-');
// Replace multiple hyphens with a single hyphen
s = s.replace(/-+/g, '-');
// Remove leading and trailing hyphens
s = s.replace(/^-+|-+$/g, '');
return s;
}
module.exports = {
saveDocument,
setDefaultExtension,
slug,
}