Skip to content

Commit

Permalink
cli: modify split behaviour
Browse files Browse the repository at this point in the history
Now it splits 'str' till limit is bound or no more separators
left in 'str' if leaveEmpty is true then multiple separators
in sequence are written in the resulting array as one empty
string (''), else they are skipped and doesn't get counted to limit

PR-URL: #132
Reviewed-By: Mykola Bilochub <nbelochub@gmail.com>
Reviewed-By: Alexey Orlenko <eaglexrlnk@gmail.com>
  • Loading branch information
lundibundi authored and belochub committed Jan 22, 2018
1 parent e22c0fd commit cd722e2
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions tools/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,24 +101,37 @@ commandProcessor.exit = () => {
process.exit();
};

// str - inputs string
// separator - string to use as a separator
// limit - resulting length of output array - 1 (last one is what's left),
// if !limit === true => means no limit and split till no more
// separators found
// leaveEmpty - if true multiple separators in sequence will be added as empty
// empty string, else they are skipped
//
// returns an array of strings
//
// the behaviour is as follows:
// splits 'str' till limit is bound or no more separators left in 'str'
// if leaveEmpty is true then multiple separators in sequence are written in
// resulting array as one empty string (''), else they are skipped
// and doesn't get counted to limit
function _split(str, separator, limit, leaveEmpty) {
const shouldTrim = (start, split) => !leaveEmpty && start === split;
const isLastEmpty = arr => !arr[arr.length - 1];

const result = [];
let start = 0;

// eslint-disable-next-line no-unmodified-loop-condition
for (let i = 0; !limit || i < limit; i++) {
while (!limit || limit - result.length > 0) {
const split = str.indexOf(separator, start);
if (split === -1) break;
if (!shouldTrim(start, split)) {
if (start !== split || leaveEmpty && !isLastEmpty(result)) {
result.push(str.slice(start, split));
} else {
i--;
}
start = split + separator.length;
}
if (!shouldTrim(start, str.length)) {
if (start !== str.length || leaveEmpty && !isLastEmpty(result)) {
result.push(str.slice(start));
}
return result;
Expand Down

0 comments on commit cd722e2

Please sign in to comment.