UNPKG

@augment-vir/common

Version:

A collection of augments, helpers types, functions, and classes for any JavaScript environment.

54 lines (53 loc) 1.97 kB
import { setRegExpCaseSensitivity } from '@augment-vir/core'; import { findSubstringIndexes } from './substring-index.js'; /** * Same as * * [`''.split`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String/split) * but includes the split delimiter in the output array. * * @category String * @category Package : @augment-vir/common * @example * * ```ts * import {splitIncludeSplit} from '@augment-vir/common'; * * splitIncludeSplit('1,2,3', ',', {caseSensitive: false}); // outputs `['1', ',', '2', ',', '3']` * ``` * * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common) */ export function splitIncludeSplit(original, splitDelimiter, { caseSensitive }) { const indexLengths = findSubstringIndexes({ searchIn: original, searchFor: splitDelimiter, caseSensitive, includeLength: true, }); const splitter = setRegExpCaseSensitivity(splitDelimiter, { caseSensitive }); const splits = original.split(splitter); return splits.reduce((accum, current, index) => { // this will be undefined on the last index const splitterLength = indexLengths[index]; const includeCurrent = accum.concat(current); if (splitterLength) { const splitterMatch = original.slice(splitterLength.index, splitterLength.index + splitterLength.length); return includeCurrent.concat(splitterMatch); } else { return includeCurrent; } }, []); } /** * Same as * [`''.split`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String/split) * but typed better: it always returns an array with at least one string. * * @category String * @category Package : @augment-vir/common * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common) */ export function safeSplit(input, splitString) { return input.split(splitString); }