Package Exports
- string-collapse-leading-whitespace
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (string-collapse-leading-whitespace) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
string-collapse-leading-whitespace
Collapse the leading and trailing whitespace of a string
Table of Contents
Install
npm i string-collapse-leading-whitespace// consume via a CommonJS require:
const collapseLeadingWhitespace = require("string-collapse-leading-whitespace");
// or as an ES Module:
import collapseLeadingWhitespace from "string-collapse-leading-whitespace";Here's what you'll get:
| Type | Key in package.json |
Path | Size |
|---|---|---|---|
Main export - CommonJS version, transpiled to ES5, contains require and module.exports |
main |
dist/string-collapse-leading-whitespace.cjs.js |
2 KB |
ES module build that Webpack/Rollup understands. Untranspiled ES6 code with import/export. |
module |
dist/string-collapse-leading-whitespace.esm.js |
2 KB |
UMD build for browsers, transpiled, minified, containing iife's and has all dependencies baked-in |
browser |
dist/string-collapse-leading-whitespace.umd.js |
729 B |
Idea
// does nothing to trimmed strings:
'aaa' => 'aaa'
// if leading or trailing whitespace doesn't contain \n, collapse to a single space
' aaa ' => ' aaa '
// otherwise, collapse to a single \n
' \n\n aaa \n\n\n ' => '\naaa\n'API - Input
| Input argument | Type | Obligatory? | Default | Description |
|---|---|---|---|---|
str |
String | yes | undefined | Source string to work on |
originalLimitLinebreaksCount |
Natural number (excl. zero) | no | 1 |
Maximum line breaks that will be put when leading or trailing whitespace contains any. |
If first input argument is not a string, it will be just returned back, untouched.
If second input argument is zero or falsey or not a number, it will be set to 1 and application will continue as normal.
API - Output
String of zero or more characters. If input was not a string, same thing will be returned back, without an error.
Example
const collapseLeadingWhitespace = require("string-collapse-leading-whitespace");
const someStr = "\n\n\n tralalaa \n\n";
const res1 = collapseLeadingWhitespace(someStr); // default is one leading/trailing line break
console.log(
`${`\u001b[${33}m${`res1`}\u001b[${39}m`} = ${JSON.stringify(res1, null, 4)}`
);
// res1 = "\ntralalaa\n"
// result has single leading/trailing linebreak because second argument's default is 1.
// -----------------------------------------------------------------------------
// now, same thing, but set it to two:
const res2 = collapseLeadingWhitespace(someStr, 2); // notice second arg set
console.log(
`${`\u001b[${33}m${`res2`}\u001b[${39}m`} = ${JSON.stringify(res2, null, 4)}`
);
// res2 = "\n\ntralalaa\n\n"
// result has two leading, two trailing. Leading count was capped, trailing reached max anyway. There were two only leading line breaks.
// Notice spaces/tabs are/would be removed.
// -----------------------------------------------------------------------------
// now set it to three:
const res3 = collapseLeadingWhitespace(someStr, 3); // notice second arg set
console.log(
`${`\u001b[${33}m${`res3`}\u001b[${39}m`} = ${JSON.stringify(res3, null, 4)}`
);
// res3 = "\n\n\ntralalaa\n\n"
// result has three leading line breaks, them maxed out - there were three. There were two trailing linebreaks, allowance was for three. End result - two trailing linebreaks.
// All spaces were removed.Purpose
I'm going to use it in string-slices-array-push.
Contributing
If you want a new feature in this package or you would like us to change some of its functionality, raise an issue on this repo.
If you tried to use this library but it misbehaves, or you need advice setting it up, and its readme doesn't make sense, just document it and raise an issue on this repo.
If you would like to add or change some features, just fork it, hack away, and file a pull request. We'll do our best to merge it quickly. Prettier is enabled, so you don't need to worry about the code style.
Licence
MIT License (MIT)
Copyright © 2018 Codsen Ltd, Roy Revelt