-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.js
More file actions
68 lines (62 loc) · 1.73 KB
/
utils.js
File metadata and controls
68 lines (62 loc) · 1.73 KB
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
// ============================================
// utils.js - Shared Utility Functions
// ============================================
/**
* Remove BOM (Byte Order Mark) if present at start of content
* @param {string} content - The content to process
* @returns {string} Content without BOM
*/
function removeBOM(content) {
if (content && content.charCodeAt(0) === 0xFEFF) {
return content.substring(1);
}
return content;
}
/**
* Get filename from a file path (cross-platform)
* @param {string} filePath - The full file path
* @returns {string} Just the filename
*/
function getFileName(filePath) {
if (!filePath) return '';
return filePath.split(/[\\/]/).pop();
}
/**
* Get directory from a file path (cross-platform)
* @param {string} filePath - The full file path
* @returns {string} The directory path
*/
function getDirectory(filePath) {
if (!filePath) return '';
const parts = filePath.split(/[\\/]/);
parts.pop();
return parts.join('/');
}
/**
* Escape special regex characters in a string
* @param {string} string - The string to escape
* @returns {string} Escaped string safe for use in RegExp
*/
function escapeRegex(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
/**
* Format bytes to human readable string
* @param {number} bytes - Number of bytes
* @returns {string} Formatted string (e.g., "1.5 MB")
*/
function formatBytes(bytes) {
if (bytes === 0) return '0 B';
const k = 1024;
const sizes = ['B', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(1)) + ' ' + sizes[i];
}
// Export for use in other modules
module.exports = {
removeBOM,
getFileName,
getDirectory,
escapeRegex,
formatBytes
};