-
Notifications
You must be signed in to change notification settings - Fork 2k
/
dedent.js
54 lines (49 loc) · 1.45 KB
/
dedent.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
*/
import invariant from './invariant';
/**
* fixes indentation by removing leading spaces and tabs from each line
*/
function fixIndent(str: string): string {
const trimmedStr = str
.replace(/^\n*/m, '') // remove leading newline
.replace(/[ \t]*$/, ''); // remove trailing spaces and tabs
const indentMatch = /^[ \t]*/.exec(trimmedStr);
invariant(Array.isArray(indentMatch));
const indent = indentMatch[0]; // figure out indent
return trimmedStr.replace(RegExp('^' + indent, 'mg'), ''); // remove indent
}
/**
* An ES6 string tag that fixes indentation. Also removes leading newlines
* and trailing spaces and tabs, but keeps trailing newlines.
*
* Example usage:
* const str = dedent`
* {
* test
* }
* `;
* str === "{\n test\n}\n";
*/
export default function dedent(
strings: string | Array<string>,
...values: Array<string>
): string {
// when used as an ordinary function, allow passing a singleton string
const strArray = typeof strings === 'string' ? [strings] : strings;
const numValues = values.length;
const str = strArray.reduce((prev, cur, index) => {
let next = prev + cur;
if (index < numValues) {
next += values[index]; // interpolation
}
return next;
}, '');
return fixIndent(str);
}