string-split-by-whitespace
TypeScript icon, indicating that this package has built-in type declarations

4.0.19 • Public • Published

string-split-by-whitespace

Split string into array by chunks of whitespace

page on codsen.com page on npm page on github Downloads per month changelog MIT Licence

Install

This package is pure ESM. If you're not ready yet, install an older version of this program, 2.1.0 (npm i string-split-by-whitespace@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 © 2010-2024 Roy Revelt and other contributors.

ok codsen star

Package Sidebar

Install

npm i string-split-by-whitespace

Weekly Downloads

4

Version

4.0.19

License

MIT

Unpacked Size

13.8 kB

Total Files

7

Last publish

Collaborators

  • royston