cmpstr-cli
Version:
Simple CLI wrapper for the CmpStr package to normalize and compare strings directly via terminal
50 lines • 1.48 kB
JavaScript
/**
* @fileoverview
* Input utilities for CmpStr CLI.
*
* Provides functions to resolve string or file input and
* to parse lists from input.
*
* @author Paul Köhler (komed3)
* @license MIT
*/
;
import { access, readFile } from 'node:fs/promises';
/**
* Resolves the input string.
* If the input is a path to a readable file, reads and returns its trimmed content.
* Otherwise, returns the trimmed input string itself.
*
* @async
* @param {string} input - The input string or file path.
* @returns {Promise< string >} The resolved input content.
*/
export async function resolveInput(input) {
try {
await access(input);
return (await readFile(input, 'utf-8')).trim();
}
catch {
return input.trim();
}
}
/**
* Resolves a list of strings from input.
* If the input is a file, reads and splits its content by line or delimiter.
* If the input is a string, splits by delimiter.
*
* @async
* @param {string} input - The input string or file path.
* @param {string} [delimiter=','] - The delimiter to use for splitting (default: `,`).
* @returns {Promise< string[] >} The resolved list of strings.
*/
export async function resolveListInput(input, delimiter = ',') {
try {
const text = await resolveInput(input);
return text.split(/\n/.exec(text) ? /\r?\n/ : delimiter).map(s => s.trim()).filter(Boolean);
}
catch {
return [];
}
}
//# sourceMappingURL=input.js.map