🚨 Shai-Hulud Strikes Again:834 Packages Compromised.Technical Analysis
Socket
Book a DemoInstallSign in
Socket

string-collapse-leading-whitespace

Package Overview
Dependencies
Maintainers
1
Versions
142
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

string-collapse-leading-whitespace

Collapse the leading and trailing whitespace of a string

Source
npmnpm
Version
1.12.18
Version published
Weekly downloads
209K
54.23%
Maintainers
1
Weekly downloads
 
Created
Source

string-collapse-leading-whitespace

Collapse the leading and trailing whitespace of a string

Minimum Node version required Repository is on GitLab Coverage View dependencies as 2D chart Downloads/Month Test in browser Code style: prettier MIT License

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:

TypeKey in package.jsonPathSize
Main export - CommonJS version, transpiled to ES5, contains require and module.exportsmaindist/string-collapse-leading-whitespace.cjs.js2 KB
ES module build that Webpack/Rollup understands. Untranspiled ES6 code with import/export.moduledist/string-collapse-leading-whitespace.esm.js2 KB
UMD build for browsers, transpiled, minified, containing iife's and has all dependencies baked-inbrowserdist/string-collapse-leading-whitespace.umd.js1018 B

⬆ back to top

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'

⬆ back to top

API - Input

Input argumentTypeObligatory?DefaultDescription
strStringyesundefinedSource string to work on
originalLimitLinebreaksCountNatural number (excl. zero)no1Maximum 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); // 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.

⬆ 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

Keywords

string

FAQs

Package last updated on 22 Mar 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts