yini-parser
Version:
Readable configuration without YAML foot-guns or JSON noise. The official Node.js parser for YINI config format — An INI-inspired configuration format with clear nesting, explicit types, and predictable parsing.
156 lines • 5.45 kB
JavaScript
;
/**
* This file contains general string helper functions (utils).
* @note More specific YINI helper functions should go into yiniHelpers.ts-file.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeSuffix = exports.toLowerKebabCase = exports.toLowerSnakeCase = exports.stripNLAndAfter = exports.isDigit = exports.isAlpha = exports.isEnclosedInBackticks = exports.trimBackticks = exports.trimTrailingNonLetters = exports.splitLines = exports.computeSha256 = exports.capitalizeFirst = exports.toColRow = void 0;
const crypto_1 = require("crypto");
const print_1 = require("./print");
const toColRow = (size, label, value) => {
return `${label.padEnd(size)} ${value}`;
};
exports.toColRow = toColRow;
// export const stripBOM = (input: string): string => {
// // (!) NOTE: slice(1) only because UTF-8 BOM appears as one single Unicode code characte, even though it is 3 bytes (EF BB BF) on disk.
// return input.startsWith('\uFEFF') ? input.slice(1) : input
// }
/**
* Capitalizes the first character of a string.
*
* @param str The input string.
* @returns A new string with the first character uppercased.
*/
const capitalizeFirst = (str) => {
if (!str)
return str;
return str.charAt(0).toUpperCase() + str.slice(1);
};
exports.capitalizeFirst = capitalizeFirst;
const computeSha256 = (content) => {
return (0, crypto_1.createHash)('sha256').update(content, 'utf8').digest('hex');
};
exports.computeSha256 = computeSha256;
/**
* Splits a string into an array of lines, handling both LF and CRLF newlines.
* @param content The input string.
* @returns Array of lines (strings).
*/
const splitLines = (content) => {
// Chould handle \n (LF), \r\n (CRLF), and even just \r (old Mac style).
return content.split(/\r\n|\r|\n/);
};
exports.splitLines = splitLines;
/**
* Trims trailing non-letter characters (A–Z, a–z) from the end of a string.
*
* @param str Input string
* @returns String with trailing non-letters removed
*/
const trimTrailingNonLetters = (str) => {
return str.replace(/[^a-zA-Z]+$/g, '');
};
exports.trimTrailingNonLetters = trimTrailingNonLetters;
/**
* If a string starts and ends with a backtick `, if so trims the
* first and last character (the backticks).
*/
const trimBackticks = (str) => {
if (str.length >= 2 && str.startsWith('`') && str.endsWith('`')) {
return str.slice(1, -1);
}
return str;
};
exports.trimBackticks = trimBackticks;
/**
* Returns true if the provided string is enclosed in backticks, e.g. `name`.
*/
const isEnclosedInBackticks = (str) => {
if (str.length >= 2 && str.startsWith('`') && str.endsWith('`')) {
return true;
}
return false;
};
exports.isEnclosedInBackticks = isEnclosedInBackticks;
/**
* Check if the character is A-Z or a-z.
* @note The string must be of length 1.
* @param character A character in a string.
*/
const isAlpha = (character) => {
if (character.length !== 1) {
throw Error('Argument into function isAlpha(..) is not of length 1');
}
const ch = character;
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
return true;
}
return false;
};
exports.isAlpha = isAlpha;
/**
* Check if the character is a digit (number): 0-9.
* @note The string must be of length 1.
* @param character A character in a string.
*/
const isDigit = (character) => {
if (character.length !== 1) {
throw Error('Argument into function isDigit(..) is not of length 1');
}
const ch = character;
if (ch >= '0' && ch <= '9') {
return true;
}
return false;
};
exports.isDigit = isDigit;
/**
* @returns Returns the beginning up to (but not including) any first
* encountered newline.
* @note If no newline is found, returns the whole string.
* @example
* `SectionName1
* //value = 11`
* => 'SectionName1'
* @deprecated This seems not useful anymore, use stripCommentsAndAfter(..) instead.
*/
const stripNLAndAfter = (line) => {
let idx1 = line.indexOf('\n');
let idx2 = line.indexOf('\r');
if (idx1 < 0)
idx1 = Number.MAX_SAFE_INTEGER;
if (idx2 < 0)
idx2 = Number.MAX_SAFE_INTEGER;
// debugPrint('stripNLAndAfter(..): idx1 = ' + idx1)
// debugPrint('stripNLAndAfter(..): idx2 = ' + idx2)
const idx = Math.min(idx1, idx2);
const resultLine = idx === Number.MAX_SAFE_INTEGER ? line : line.substring(0, idx);
(0, print_1.debugPrint)('stripNLAndAfter(..), line: >>>' + line + '<<<');
(0, print_1.debugPrint)('stripNLAndAfter(..), resultLine: >>>' + resultLine + '<<<');
return resultLine;
};
exports.stripNLAndAfter = stripNLAndAfter;
/**
* Transforms strings such as 'Id-Name' to 'id_name'.
* Replaces all '-' to '_', and returns rusult in lower case.
*/
const toLowerSnakeCase = (txt) => {
return txt.trim().toLowerCase().replace(/[-]/g, '_');
};
exports.toLowerSnakeCase = toLowerSnakeCase;
/**
* Transforms strings such as 'Id-Name' to 'id-name'.
* Replaces all '_' to '-', and returns rusult in lower case.
*/
const toLowerKebabCase = (txt) => {
return txt.trim().toLowerCase().replace(/[_]/g, '-');
};
exports.toLowerKebabCase = toLowerKebabCase;
const removeSuffix = (str, suffix) => {
if (str.endsWith(suffix)) {
return str.slice(0, -suffix.length);
}
return str;
};
exports.removeSuffix = removeSuffix;
//# sourceMappingURL=string.js.map