Merge pull request #893 from 30-seconds/compact-whitespace

[FEATURE] Add compactWhitespace snippet
This commit is contained in:
Angelos Chalaris
2018-12-17 11:39:29 +02:00
committed by GitHub
4 changed files with 1905 additions and 1511 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

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 ');
});