Skip to content

Commit

Permalink
[minor] add optional options input to collapseWhiteSpace
Browse files Browse the repository at this point in the history
  • Loading branch information
electrovir committed Feb 1, 2024
1 parent 04e48c9 commit 9d231b1
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 87 deletions.
60 changes: 30 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "augment-vir",
"version": "23.1.1",
"version": "23.2.0",
"private": true,
"homepage": "https://github.com/electrovir/augment-vir",
"bugs": {
Expand Down
6 changes: 3 additions & 3 deletions packages/browser-testing/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@augment-vir/browser-testing",
"version": "23.1.1",
"version": "23.2.0",
"homepage": "https://github.com/electrovir/augment-vir/tree/main/packages/common",
"bugs": {
"url": "https://github.com/electrovir/augment-vir/issues"
Expand All @@ -23,8 +23,8 @@
"test:coverage": "npm test"
},
"dependencies": {
"@augment-vir/common": "^23.1.1",
"@augment-vir/testing": "^23.1.1",
"@augment-vir/common": "^23.2.0",
"@augment-vir/testing": "^23.2.0",
"@open-wc/testing": "^4.0.0",
"@types/mocha": "^10.0.6",
"@web/test-runner-commands": "^0.9.0",
Expand Down
6 changes: 3 additions & 3 deletions packages/browser/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@augment-vir/browser",
"version": "23.1.1",
"version": "23.2.0",
"homepage": "https://github.com/electrovir/augment-vir/tree/main/packages/browser",
"bugs": {
"url": "https://github.com/electrovir/augment-vir/issues"
Expand All @@ -24,12 +24,12 @@
"test:watch": "web-test-runner --color --config configs/web-test-runner.config.mjs --watch"
},
"dependencies": {
"@augment-vir/common": "^23.1.1",
"@augment-vir/common": "^23.2.0",
"html-spec-tags": "^1.0.3",
"run-time-assertions": "^0.3.0"
},
"devDependencies": {
"@augment-vir/browser-testing": "^23.1.1",
"@augment-vir/browser-testing": "^23.2.0",
"@open-wc/testing": "^4.0.0",
"@types/chai": "^4.3.11",
"@types/mocha": "^10.0.6",
Expand Down
6 changes: 3 additions & 3 deletions packages/chai/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@augment-vir/chai",
"version": "23.1.1",
"version": "23.2.0",
"homepage": "https://github.com/electrovir/augment-vir/tree/main/packages/common",
"bugs": {
"url": "https://github.com/electrovir/augment-vir/issues"
Expand All @@ -23,8 +23,8 @@
"test:coverage": "npm test"
},
"dependencies": {
"@augment-vir/common": "^23.1.1",
"@augment-vir/testing": "^23.1.1",
"@augment-vir/common": "^23.2.0",
"@augment-vir/testing": "^23.2.0",
"type-fest": "^4.9.0"
},
"devDependencies": {
Expand Down
8 changes: 4 additions & 4 deletions packages/common-tests/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@augment-vir/common-tests",
"version": "23.1.1",
"version": "23.2.0",
"private": true,
"homepage": "https://github.com/electrovir/augment-vir/tree/main/packages/common-tests",
"bugs": {
Expand All @@ -22,9 +22,9 @@
"test:types": "tsc --noEmit"
},
"devDependencies": {
"@augment-vir/chai": "^23.1.1",
"@augment-vir/common": "^23.1.1",
"@augment-vir/node-js": "^23.1.1",
"@augment-vir/chai": "^23.2.0",
"@augment-vir/common": "^23.2.0",
"@augment-vir/node-js": "^23.2.0",
"@electrovir/nyc": "^15.1.0-fix0",
"@istanbuljs/nyc-config-typescript": "^1.0.2",
"@types/chai": "^4.3.11",
Expand Down
52 changes: 32 additions & 20 deletions packages/common-tests/src/tests/common-string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,26 +93,38 @@ describe(joinWithFinalConjunction.name, () => {
});

describe(collapseWhiteSpace.name, () => {
it(`check simple space collapsing`, () => {
expect(collapseWhiteSpace('hello there')).to.equal('hello there');
});

it(`multiple space collapsing`, () => {
expect(
collapseWhiteSpace(
'hello there you are a bold one',
),
).to.equal('hello there you are a bold one');
});

it(`beginning and ending space is trimmed as well`, () => {
expect(collapseWhiteSpace(' hello there ')).to.equal('hello there');
});

it('should trim inner newlines', () => {
expect(collapseWhiteSpace('hello\n\nthere')).to.equal('hello there');
expect(collapseWhiteSpace('hello\n\nthere\n\nstuff')).to.equal('hello there stuff');
});
itCases(collapseWhiteSpace, [
{
it: 'works with single repeating spaces',
inputs: ['hello there'],
expect: 'hello there',
},
{
it: 'works with multiple repeating spaces',
inputs: ['hello there you are a bold one'],
expect: 'hello there you are a bold one',
},
{
it: 'also trims',
inputs: [' hello there '],
expect: 'hello there',
},
{
it: 'collapses newlines by default',
inputs: [' \n hello \n there \n\n '],
expect: 'hello there',
},
{
it: 'can preserve newlines',
inputs: [
' \n hello \n there \n\n ',
{
keepNewLines: true,
},
],
expect: 'hello\nthere',
},
]);
});

describe(removeCommasFromNumberString.name, () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@augment-vir/common",
"version": "23.1.1",
"version": "23.2.0",
"homepage": "https://github.com/electrovir/augment-vir/tree/main/packages/common",
"bugs": {
"url": "https://github.com/electrovir/augment-vir/issues"
Expand Down
18 changes: 10 additions & 8 deletions packages/common/src/augments/common-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,16 @@ export function removeCommasFromNumberString(numberString: string): string {
}

/** Collapse all consecutive white space into just one space and trim surrounding whitespace. */
export function collapseWhiteSpace(input: string): string {
return (
input
// sometimes \n isn't included in \s
.replace(/\n/g, ' ')
.trim()
.replace(/\s{2,}/g, ' ')
);
export function collapseWhiteSpace(
input: string,
{keepNewLines}: PartialAndUndefined<{keepNewLines: boolean}> = {},
): string {
const newLineReplacement = keepNewLines
? input.replace(/[\s\n]*\n+[\s\n]*/g, '\n')
: // sometimes \n isn't included in \s
input.replace(/\n/g, ' ');

return newLineReplacement.trim().replace(/\s{2,}/g, ' ');
}

/** Same as String.prototype.split but includes the delimiter to split by in the output array. */
Expand Down
6 changes: 3 additions & 3 deletions packages/docker/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@augment-vir/docker",
"version": "23.1.1",
"version": "23.2.0",
"homepage": "https://github.com/electrovir/augment-vir/tree/main/packages/docker",
"bugs": {
"url": "https://github.com/electrovir/augment-vir/issues"
Expand All @@ -22,8 +22,8 @@
"test:coverage": "npm test"
},
"dependencies": {
"@augment-vir/common": "^23.1.1",
"@augment-vir/node-js": "^23.1.1"
"@augment-vir/common": "^23.2.0",
"@augment-vir/node-js": "^23.2.0"
},
"devDependencies": {
"typescript": "5.3.3"
Expand Down
6 changes: 3 additions & 3 deletions packages/node-js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@augment-vir/node-js",
"version": "23.1.1",
"version": "23.2.0",
"homepage": "https://github.com/electrovir/augment-vir/tree/main/packages/node-js",
"bugs": {
"url": "https://github.com/electrovir/augment-vir/issues"
Expand All @@ -22,7 +22,7 @@
"test:coverage": "npm test coverage"
},
"dependencies": {
"@augment-vir/common": "^23.1.1",
"@augment-vir/common": "^23.2.0",
"ansi-colors": "^4.1.3",
"axios": "^1.6.5",
"fs-extra": "^11.2.0",
Expand All @@ -31,7 +31,7 @@
"type-fest": "^4.9.0"
},
"devDependencies": {
"@augment-vir/chai": "^23.1.1",
"@augment-vir/chai": "^23.2.0",
"@electrovir/nyc": "^15.1.0-fix0",
"@istanbuljs/nyc-config-typescript": "^1.0.2",
"@types/chai": "^4.3.11",
Expand Down
Loading

0 comments on commit 9d231b1

Please sign in to comment.