UNPKG

@eagleoutice/flowr

Version:

Static Dataflow Analyzer and Program Slicer for the R Programming Language

54 lines 1.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.startAndEndsWith = startAndEndsWith; exports.withoutWhitespace = withoutWhitespace; exports.longestCommonPrefix = longestCommonPrefix; exports.joinWithLast = joinWithLast; /** * Check if the given string starts and ends with the given letter */ function startAndEndsWith(str, letter) { return str.startsWith(letter) && str.endsWith(letter); } /** * Removes all whitespace in the given string */ function withoutWhitespace(output) { return output.replace(/\s/g, ''); } /** * Find the longest common prefix in an array of strings */ function longestCommonPrefix(strings) { if (strings.length === 0) { return ''; } let prefix = strings[0]; for (const str of strings) { if (prefix.length === 0) { break; } let i = 0; while (i < prefix.length && prefix[i] === str[i]) { i++; } if (i !== prefix.length) { prefix = prefix.slice(0, i); } } return prefix; } /** * Join a list of strings, but with special handling for the last element/scenarios in which the array contains exactly two elements. * The goal is to create (partial) sentences like `a, b, and c` or `a and b`. */ function joinWithLast(strs, { join = ', ', last = ', and ', joinTwo = ' and ' } = {}) { if (strs.length <= 1) { return strs.join(''); } else if (strs.length === 2) { return strs.join(joinTwo); } return strs.slice(0, -1).join(join) + last + strs[strs.length - 1]; } //# sourceMappingURL=strings.js.map