string-collapse-leading-whitespace
Collapse the leading and trailing whitespace of a string

Table of Contents
Install
npm i string-collapse-leading-whitespace
The default is exported, so instead of "collapseLeadingWhitespace" below, you can name the consumed function however you want.
Consume via a require():
const collapseLeadingWhitespace = require("string-collapse-leading-whitespace");
or as an ES Module:
import collapseLeadingWhitespace from "string-collapse-leading-whitespace";
or for web pages, as a production-ready minified script file (so-called "UMD build"), straight from CDN:
<script src="https://cdn.jsdelivr.net/npm/string-collapse-leading-whitespace/dist/string-collapse-leading-whitespace.umd.js"></script>
const collapseLeadingWhitespace = stringCollapseLeadingWhitespace;
This package has three builds in dist/ folder:
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 | 1018 B |
⬆ back to top
Idea
'aaa' => 'aaa'
' aaa ' => ' aaa '
' \n\n aaa \n\n\n ' => '\naaa\n'
⬆ back to top
API - Input
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.
⬆ back to top
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);
console.log(
`${`\u001b[${33}m${`res1`}\u001b[${39}m`} = ${JSON.stringify(res1, null, 4)}`
);
const res2 = collapseLeadingWhitespace(someStr, 2);
console.log(
`${`\u001b[${33}m${`res2`}\u001b[${39}m`} = ${JSON.stringify(res2, null, 4)}`
);
const res3 = collapseLeadingWhitespace(someStr, 3);
console.log(
`${`\u001b[${33}m${`res3`}\u001b[${39}m`} = ${JSON.stringify(res3, null, 4)}`
);
⬆ back to top
Purpose
I'm going to use it in ranges-push.
Contributing
- If you see an error, raise an issue.
- If you want a new feature but can't code it up yourself, also raise an issue. Let's discuss it.
- If you tried to use this package, but something didn't work out, also raise an issue. We'll try to help.
- If you want to contribute some code, fork the monorepo via GitLab, then write code, then file a pull request on GitLab. We'll merge it in and release.
In monorepo, npm libraries are located in packages/ folder. Inside, the source code is located either in src/ folder (normal npm library) or in the root, cli.js (if it's a command-line application).
The npm script "dev", the "dev": "rollup -c --dev --silent" builds the development version retaining all console.logs with row numbers. It's handy to have js-row-num-cli installed globally so you can automatically update the row numbers on all console.logs.
⬆ back to top
Licence
MIT License
Copyright (c) 2015-2019 Roy Revelt and other contributors