"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.matchAt = matchAt;
exports.replaceAt = replaceAt;
exports.toBase64 = toBase64;
exports.fromBase64 = fromBase64;
exports.uniqueStrings = uniqueStrings;
exports.looseEquals = looseEquals;
exports.titleCase = titleCase;
exports.copystr = copystr;
exports.coerceString = coerceString;
// Return true if the match string is found at index in content
function matchAt(content, index, match) {
    return content.substring(index, index + match.length) === match;
}
// Replace oldString with newString at location index of content
function replaceAt(content, index, oldString, newString) {
    return (content.substring(0, index) +
        newString +
        content.substring(index + oldString.length));
}
/**
 * Converts from utf-8 string to base64-encoded string
 */
function toBase64(input) {
    return Buffer.from(input).toString('base64');
}
/**
 * Converts from base64-encoded string to utf-8 string
 */
function fromBase64(input) {
    return Buffer.from(input, 'base64').toString();
}
function uniqueStrings(element, index, elements) {
    return elements.indexOf(element) === index;
}
function looseEquals(a, b) {
    if (!(a && b)) {
        return a === b;
    }
    return a.localeCompare(b, undefined, { sensitivity: 'base' }) === 0;
}
function titleCase(input) {
    const words = input.toLowerCase().split(' ');
    for (let i = 0; i < words.length; i++) {
        const word = words[i];
        words[i] = word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
    }
    return words.join(' ');
}
/**
 * Sometimes we extract small strings from a multi-megabyte files.
 * If we then save them in the in-memory cache, V8 may not free
 * the initial buffer, which can lead to memory leaks:
 *
 *   https://bugs.chromium.org/p/v8/issues/detail?id=2869
 *
 */
function copystr(x) {
    const len = Buffer.byteLength(x, 'utf8');
    const buf = Buffer.allocUnsafeSlow(len);
    buf.write(x, 'utf8');
    return buf.toString('utf8');
}
/**
 * Coerce a value to a string with optional default value.
 * @param val value to coerce
 * @returns the coerced value.
 */
function coerceString(val, def) {
    return val ?? def ?? '';
}
//# sourceMappingURL=string.js.map