Skip to content

Commit

Permalink
Use ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Mar 8, 2021
1 parent 7b6192e commit 78e42e4
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 40 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
.DS_Store
*.log
.nyc_output/
coverage/
node_modules/
collapse-white-space.js
collapse-white-space.min.js
yarn.lock
3 changes: 0 additions & 3 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
coverage/
collapse-white-space.js
collapse-white-space.min.js
*.json
*.md
6 changes: 1 addition & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
'use strict'

module.exports = collapse

// `collapse(' \t\nbar \nbaz\t') // ' bar baz '`
function collapse(value) {
export function collapseWhiteSpace(value) {
return String(value).replace(/\s+/g, ' ')
}
28 changes: 10 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,25 @@
"contributors": [
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
],
"sideEffects": false,
"type": "module",
"main": "index.js",
"files": [
"index.js"
],
"devDependencies": {
"browserify": "^17.0.0",
"nyc": "^15.0.0",
"c8": "^7.0.0",
"prettier": "^2.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"tape": "^5.0.0",
"tinyify": "^3.0.0",
"xo": "^0.38.0"
},
"scripts": {
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"build-bundle": "browserify . -s collapseWhiteSpace -o collapse-white-space.js",
"build-mangle": "browserify . -s collapseWhiteSpace -p tinyify -o collapse-white-space.min.js",
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test.js",
"test": "npm run format && npm run build && npm run test-coverage"
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
"test": "npm run format && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
Expand All @@ -50,20 +48,14 @@
},
"xo": {
"prettier": true,
"esnext": false,
"ignores": [
"collapse-white-space.js"
]
"rules": {
"no-var": "off",
"prefer-arrow-callback": "off"
}
},
"remarkConfig": {
"plugins": [
"preset-wooorm"
]
},
"nyc": {
"check-coverage": true,
"lines": 100,
"functions": 100,
"branches": 100
}
}
12 changes: 9 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ Replace multiple whitespace characters with a single space.

## Install

This package is ESM only: Node 12+ is needed to use it and it must be `import`ed
instead of `require`d.

[npm][]:

```sh
Expand All @@ -18,14 +21,17 @@ npm install collapse-white-space
## Use

```js
var collapse = require('collapse-white-space')
import {collapseWhiteSpace} from 'collapse-white-space'

collapse('\tfoo \n\tbar \t\r\nbaz') //=> ' foo bar baz'
collapseWhiteSpace('\tfoo \n\tbar \t\r\nbaz') //=> ' foo bar baz'
```

## API

### `collapse(value)`
This package exports the following identifiers: `collapseWhiteSpace`.
There is no default export.

### `collapseWhiteSpace(value)`

Replace multiple whitespace characters in value with a single space.

Expand Down
15 changes: 7 additions & 8 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
'use strict'

var test = require('tape')
var collapse = require('.')
import test from 'tape'
import {collapseWhiteSpace} from './index.js'

test('collapseWhiteSpace(value)', function (t) {
t.equal(collapse(true), 'true', 'should coerce to string')
// @ts-ignore runtime
t.equal(collapseWhiteSpace(true), 'true', 'should coerce to string')

t.equal(collapse(' \t\nbar \nbaz\t'), ' bar baz ')
t.equal(collapse(' bar\t\t\tbaz\n\n\n'), ' bar baz ')
t.equal(collapse(' \n bar\t\n\tbaz\r\n'), ' bar baz ')
t.equal(collapseWhiteSpace(' \t\nbar \nbaz\t'), ' bar baz ')
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.end()
})

0 comments on commit 78e42e4

Please sign in to comment.