First list and integer questions, spaces to tabs

This commit is contained in:
Will Boyd
2025-02-01 08:27:58 -05:00
parent cf338813f1
commit f6197d1d70
6 changed files with 84 additions and 55 deletions
+38 -21
View File
@@ -2,31 +2,48 @@ import fs from 'fs';
import path from 'path';
export function boolean(value) {
if (typeof value === 'boolean') {
return value;
} else if (value === 'true') {
return true;
} else if (value === 'false') {
return false;
}
if (typeof value === 'boolean') {
return value;
} else if (value === 'true') {
return true;
} else if (value === 'false') {
return false;
}
throw new Error('Must be true or false.');
throw new Error('Must be true or false.');
}
export function filePath(value) {
const unwrapped = value.replace(/"(.*?)"/, '$1');
const absolute = path.resolve(unwrapped);
const unwrapped = value.replace(/"(.*?)"/, '$1');
const absolute = path.resolve(unwrapped);
let fileExists;
try {
fileExists = fs.existsSync(absolute) && fs.statSync(absolute).isFile();
} catch (ex) {
fileExists = false;
}
let fileExists;
try {
fileExists = fs.existsSync(absolute) && fs.statSync(absolute).isFile();
} catch (ex) {
fileExists = false;
}
if (fileExists) {
return absolute;
} else {
throw new Error('File not found at ' + absolute + '.');
}
if (fileExists) {
return absolute;
}
throw new Error('File not found at ' + absolute + '.');
}
export function list(value) {
if (Array.isArray(value)) {
return value;
} else {
return value.trim().split(/\s*,\s*/);
}
}
export function integer(value) {
const int = parseInt(value);
if (!Number.isNaN(int) && int >= 0) {
return int;
}
throw new Error('Must be an integer >= 0.');
}