Extract `dedent` to `util`, adding tests (#538)

Summary:
There have been a couple of occasions on which we’ve considered using
it, but didn’t want to require from `app/`.

Test Plan:
Unit tests added, with full coverage.

wchargin-branch: extract-dedent
This commit is contained in:
William Chargin 2018-07-27 12:30:28 -07:00 committed by GitHub
parent 6b13ab64a0
commit 4058a3fd85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 1 deletions

View File

@ -7,7 +7,7 @@ import {match, RouterContext} from "react-router";
import {createRoutes} from "./createRoutes"; import {createRoutes} from "./createRoutes";
import {resolveTitleFromPath} from "./routeData"; import {resolveTitleFromPath} from "./routeData";
import dedent from "./dedent"; import dedent from "../util/dedent";
export default function render( export default function render(
locals: {+path: string, +assets: {[string]: string}}, locals: {+path: string, +assets: {[string]: string}},

47
src/util/dedent.test.js Normal file
View File

@ -0,0 +1,47 @@
// @flow
import dedent from "./dedent";
describe("util/dedent", () => {
it("dedents a simple example", () => {
const actual = dedent`\
hello
good
world
`;
const expected = "hello\n good\nworld\n";
expect(actual).toEqual(expected);
});
it("interpolates components", () => {
const ell = "l";
const actual = dedent`\
he${ell}${ell}o
good
wor${ell}d
`;
const expected = "hello\n good\nworld\n";
expect(actual).toEqual(expected);
});
it("does not strip leading whitespace in components", () => {
// See: https://github.com/wchargin/wchargin.github.io/commit/06475d4cc44a0437c911dd2d4d6275be4381142e
const code = 'if (true) {\n console.log("hi");\n}';
const actual = dedent`\
<pre><code>${code}</code></pre>
`;
const expected = `<pre><code>${code}</code></pre>\n`;
expect(actual).toEqual(expected);
});
it("does not strip trailing backslashes", () => {
// See: https://github.com/wchargin/wchargin.github.io/commit/06475d4cc44a0437c911dd2d4d6275be4381142e
const code = "printf '%s' \\\n wat";
const actual = dedent`\
$ cat foo.sh
${code}
`;
const expected = `$ cat foo.sh\n${code}\n`;
expect(actual).toEqual(expected);
});
});