yini-parser
Version:
Node.js parser for YINI — a clean, structured INI alternative with types, simple section nesting, comments, and strict mode.
98 lines (97 loc) • 3.27 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.stripNLAndAfter = exports.isDigit = exports.isAlpha = exports.isEnclosedInBackticks = exports.trimBackticks = void 0;
exports.splitLines = splitLines;
const print_1 = require("./print");
/**
* Splits a string into an array of lines, handling both LF and CRLF newlines.
* @param content The input string.
* @returns Array of lines (strings).
*/
function splitLines(content) {
// Chould handle \n (LF), \r\n (CRLF), and even just \r (old Mac style).
return content.split(/\r\n|\r|\n/);
}
/**
* 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;