string-split-by-whitespace

Split string into array by chunks of whitespace

Usage no npm install needed!

<script type="module">
  import stringSplitByWhitespace from 'https://cdn.skypack.dev/string-split-by-whitespace';
</script>

README

string-split-by-whitespace

Split string into array by chunks of whitespace

Install

The latest version is ESM only: Node 12+ is needed to use it and it must be imported instead of required. If your project is not on ESM yet and you want to use require, use an older version of this program, 2.1.0.

npm i string-split-by-whitespace

Quick Take

import { strict as assert } from "assert";

import { splitByW } from "string-split-by-whitespace";

// Split by whitespace is easy - use native String.prototype.split()
assert.deepEqual("abc  def ghi".split(/\s+/), ["abc", "def", "ghi"]);

const source = `\n     \n    a\t \nb    \n      \t`;

// this program is nearly equivalent to regex-based split:
assert.deepEqual(source.split(/\s+/), ["", "a", "b", ""]);
assert.deepEqual(splitByW(source), ["a", "b"]);
// regex-based split needs more filtration but it's native solution

// ADDITIONALLY...

// this program allows to exclude certain index ranges:
assert.deepEqual(
  splitByW("a b c d e", {
    ignoreRanges: [[0, 2]], // that's "a" and space after it
  }),
  ["b", "c", "d", "e"]
);

Documentation

Please visit codsen.com for a full description of the API.

Contributing

To report bugs or request features or assistance, raise an issue on GitHub.

Licence

MIT License

Copyright (c) 2010-2022 Roy Revelt and other contributors

ok codsen star