Add compactWhitespace snippet
This commit is contained in:
14
snippets/compactWhitespace.md
Normal file
14
snippets/compactWhitespace.md
Normal 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'
|
||||
```
|
||||
@ -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
|
||||
|
||||
3001
test/_30s.js
3001
test/_30s.js
File diff suppressed because it is too large
Load Diff
18
test/compactWhitespace.js
Normal file
18
test/compactWhitespace.js
Normal 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');
|
||||
});
|
||||
Reference in New Issue
Block a user