Add compactWhitespace snippet

This commit is contained in:
Angelos Chalaris
2018-12-12 19:11:33 +02:00
parent d439420f39
commit 4f661ee158
4 changed files with 1534 additions and 1500 deletions

View File

@ -0,0 +1,14 @@
### compactWhitespace
Returns a string with whitespaces compacted.
Use `String.prototype.replace()` with a regular expression to replace all occurences of 2 or more whitespace characters with a single space.
```js
const compactWhitespace = str => str.replace(/\s{2,}/g,' ');
```
```js
compactWhitespace('Lorem Ipsum'); // 'Lorem Ipsum'
compactWhitespace('Lorem \n Ipsum'); // 'Lorem Ipsum'
```

View File

@ -31,6 +31,7 @@ coalesceFactory:utility,intermediate
collectInto:adapter,function,array,intermediate
colorize:node,utility,string,intermediate
compact:array,beginner
compactWhitespace:string,regexp,beginner
compose:function,intermediate
composeRight:function,intermediate
converge:function,intermediate

File diff suppressed because it is too large Load Diff

18
test/compactWhitespace.js Normal file
View File

@ -0,0 +1,18 @@
const expect = require('expect');
const {compactWhitespace} = require('./_30s.js');
test('compactWhitespace is a Function', () => {
expect(compactWhitespace).toBeInstanceOf(Function);
});
test('compactWhitespace returns a string with compacted whitespaces', () => {
expect(compactWhitespace('Lorem Ipsum')).toBe('Lorem Ipsum');
});
test('compactWhitespace returns a string with compacted whitespaces', () => {
expect(compactWhitespace('Lorem Ipsum')).toBe('Lorem Ipsum');
});
test('compactWhitespace returns a string with compacted whitespaces', () => {
expect(compactWhitespace('Lorem \t Ipsum')).toBe('Lorem Ipsum');
});
test('compactWhitespace returns a string with compacted whitespaces', () => {
expect(compactWhitespace('\t Lorem \n\n \n Ipsum ')).toBe('Lorem Ipsum');
});