Skip to content

Commit

Permalink
Add trim, preserveLineEndings, and HTML options
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Oct 27, 2021
1 parent 06d35b7 commit fadb5d1
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 12 deletions.
81 changes: 78 additions & 3 deletions index.js
@@ -1,11 +1,86 @@
/**
* Collapse white space to a single space.
* @typedef {'html'|'js'} Style
*
* @typedef Options
* Configuration.
* @property {Style} [style='js']
* Style of white space to support.
* @property {boolean} [preserveLineEndings=false]
* Whether to collapse white space containing a line ending to that line
* ending.
* The default is to collapse to a single space.
* @property {boolean} [trim=false]
* Whether to drop white space at the start and end of `value`.
* The default is to keep it.
*/

const js = /\s+/g
const html = /[\t\n\v\f\r ]+/g

/**
* Collapse white space.
*
* @param {string} value
* Value to collapse white space in.
* @param {Style|Options} [options='js']
* Configuration.
* @returns {string}
* Value with collapsed white space.
*/
export function collapseWhiteSpace(value) {
return String(value).replace(/\s+/g, ' ')
export function collapseWhiteSpace(value, options) {
if (!options) {
options = {}
} else if (typeof options === 'string') {
options = {style: options}
}

const replace = options.preserveLineEndings ? replaceLineEnding : replaceSpace

return String(value).replace(
options.style === 'html' ? html : js,
options.trim ? trimFactory(replace) : replace
)
}

/**
* Replace white space with a line ending as that line ending and otherwise a
* space.
*
* @param {string} value
* @returns {string}
*/
function replaceLineEnding(value) {
const match = /\r?\n|\r/.exec(value)
return match ? match[0] : ' '
}

/**
* Replace white space with a space.
*
* @returns {string}
*/
function replaceSpace() {
return ' '
}

/**
* @param {(value: string) => string} replace
*/
function trimFactory(replace) {
return dropOrReplace

/**
* Replace white space with nothing if it starts or ends the string.
* Calls `replace` otherwise.
*
* @param {string} value
* @param {number} index
* @param {string} all
* @returns {string}
*/
function dropOrReplace(value, index, all) {
return index === 0 || index + value.length === all.length
? ''
: replace(value)
}
}
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "collapse-white-space",
"version": "2.0.0",
"description": "Collapse white space to a single space",
"description": "Collapse white space",
"license": "MIT",
"keywords": [
"collapse",
Expand Down
52 changes: 44 additions & 8 deletions readme.md
Expand Up @@ -5,21 +5,35 @@
[![Downloads][downloads-badge]][downloads]
[![Size][size-badge]][size]

Collapse white space to a single space.
Collapse white space.

## Contents

* [What is this?](#what-is-this)
* [When should I use this?](#when-should-i-use-this)
* [Install](#install)
* [Use](#use)
* [API](#api)
* [`collapseWhiteSpace(value)`](#collapsewhitespacevalue)
* [`collapseWhiteSpace(value[, options|style])`](#collapsewhitespacevalue-optionsstyle)
* [Types](#types)
* [Compatibility](#compatibility)
* [Security](#security)
* [Related](#related)
* [Contribute](#contribute)
* [License](#license)

## What is this?

This is a small package that collapses multiple white space characters into one.

## When should I use this?

You can use this package if you want to HTML or JavaScript (default) white space
to a single character.
You can optionally drop initial and final white space.
By default it collapses to a single space, but optionally line endings can be
preserved.

## Install

This package is [ESM only][esm].
Expand Down Expand Up @@ -56,18 +70,40 @@ collapseWhiteSpace('\tfoo \n\tbar \t\r\nbaz') //=> ' foo bar baz'
This package exports the following identifier: `collapseWhiteSpace`.
There is no default export.

### `collapseWhiteSpace(value)`
### `collapseWhiteSpace(value[, options|style])`

Collapse white space in `value` (`string`).

##### `style`

Treated as `options.style`.

##### `options`

Configuration.

###### `options.style`

Style of white space to support (`'html'` or `'js'`, default: `'js'`).
JavaScript white space matches the pattern `\s+`.
HTML white space matches `[\t\n\v\f\r ]`.

###### `options.preserveLineEndings`

Collapse white space to a single space.
Whether to collapse white space containing a line ending to that line ending
(`boolean`, default: `false`).
The default is to collapse to a single space.
Line endings matches the pattern `\r?\n|\r`.

###### Parameters
###### `options.trim`

* `value` (`string`)
– value to collapse white space in
Whether to drop white space at the start and end of `value` (`boolean`, default:
`false`).
The default is to keep it.

###### Returns

`value` (`string`) – value with collapsed white space.
`string` – value with collapsed white space.

## Types

Expand Down
29 changes: 29 additions & 0 deletions test.js
Expand Up @@ -9,5 +9,34 @@ test('collapseWhiteSpace(value)', function (t) {
t.equal(collapseWhiteSpace(' bar\t\t\tbaz\n\n\n'), ' bar baz ')
t.equal(collapseWhiteSpace(' \n bar\t\n\tbaz\r\n'), ' bar baz ')

t.equal(collapseWhiteSpace(' \u00A0 ', 'js'), ' ', 'style: js')
t.equal(collapseWhiteSpace(' \u00A0 ', 'html'), ' \u00A0 ', 'style: html')
t.equal(
collapseWhiteSpace('a \n b c', {preserveLineEndings: true}),
'a\nb c',
'`options.preserveLineEndings: true`'
)
t.equal(
collapseWhiteSpace('a \n b c', {preserveLineEndings: false}),
'a b c',
'`options.preserveLineEndings: false`'
)

t.equal(
collapseWhiteSpace(' a b ', {trim: true}),
'a b',
'`options.trim: true`'
)
t.equal(
collapseWhiteSpace(' ', {trim: true}),
'',
'`options.trim: true` (2)'
)
t.equal(
collapseWhiteSpace(' a ', {trim: false}),
' a ',
'`options.trim: false`'
)

t.end()
})

0 comments on commit fadb5d1

Please sign in to comment.