Skip to content

Commit

Permalink
Add flow, convert to ES6 module
Browse files Browse the repository at this point in the history
If anyone was importing like `require("dedent")`, they'll now need to
use `require("dedent").default`.
  • Loading branch information
dmnd committed Jan 29, 2017
1 parent 4ec1f7f commit e13e334
Show file tree
Hide file tree
Showing 10 changed files with 526 additions and 58 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
"es2015",
"es2016",
"es2017"
],
"plugins": [
"transform-flow-strip-types"
]
}
4 changes: 4 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
"node": true,
"es6": true
},
"parser": "babel-eslint",
"parserOptions": {
"sourceType": "module"
},
"extends": "eslint:recommended",
"rules": {
"semi": [2, "always"],
Expand Down
8 changes: 8 additions & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[ignore]

[include]

[libs]

[options]
emoji=true
5 changes: 5 additions & 0 deletions __tests__/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"env": {
"jest": true
}
}
19 changes: 2 additions & 17 deletions __tests__/dedent-tests.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,30 @@
/*global jest, describe, it, expect */
// @flow

"use strict";

jest.dontMock("../dedent");
import dd from '../dedent';

describe("dedent", () => {
it("works without interpolation", () => {
const dd = require("../dedent");
const result = dd`first
second
third`;
expect(result).toBe("first\nsecond\nthird");
});

it("works with interpolation", () => {
const dd = require("../dedent");
const result = dd`first ${"line"}
${"second"}
third`;
expect(result).toBe("first line\nsecond\nthird");
});

it("works with suppressed newlines", () => {
const dd = require("../dedent");
const result = dd`first \
${"second"}
third`;
expect(result).toBe("first second\nthird");
});

it("works with blank first line", () => {
const dd = require("../dedent");
const result = dd`
Some text that I might want to indent:
* reasons
Expand All @@ -46,7 +40,6 @@ describe("dedent", () => {
});

it("works with multiple blank first lines", () => {
const dd = require("../dedent");
const result = dd`
first
Expand All @@ -56,7 +49,6 @@ describe("dedent", () => {
});

it("works with removing same number of spaces", () => {
const dd = require("../dedent");
const result = dd`
first
second
Expand All @@ -69,50 +61,43 @@ describe("dedent", () => {
const expected = "A single line of input.";

it("works with single line input", () => {
const dd = require("../dedent");
const result = dd`A single line of input.`;
expect(result).toBe(expected);
});

it("works with single line and closing backtick on newline", () => {
const dd = require("../dedent");
const result = dd`
A single line of input.
`;
expect(result).toBe(expected);
});

it("works with single line and inline closing backtick", () => {
const dd = require("../dedent");
const result = dd`
A single line of input.`;
expect(result).toBe(expected);
});
});

it("can be used as a function", () => {
const dd = require("../dedent");
const arg = `
A test argument.
`;
expect(dd(arg)).toBe("A test argument.");
});

it("escapes backticks", () => {
const dd = require("../dedent");
expect(dd`\``).toBe('`');
});

it("doesn't strip exlicit newlines", () => {
const dd = require("../dedent");
const result = dd`
<p>Hello world!</p>\n
`;
expect(result).toBe("<p>Hello world!</p>\n");
});

it("doesn't strip exlicit newlines with mindent", () => {
const dd = require("../dedent");
const result = dd`
<p>
Hello world!
Expand Down
36 changes: 15 additions & 21 deletions dedent.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
"use strict";
// @flow

function dedent(strings, ...values) {

let raw;
if (typeof strings === "string") {
// dedent can be used as a plain function
raw = [strings];
} else {
raw = strings.raw;
}
export default function dedent(
strings: string | Array<string>,
...values: Array<string>
) {
// $FlowFixMe: Flow doesn't undestand .raw
const raw = typeof strings === "string" ? [strings] : strings.raw;

// first, perform interpolation
let result = "";
Expand All @@ -27,7 +24,7 @@ function dedent(strings, ...values) {

// now strip indentation
const lines = result.split("\n");
let mindent = null;
let mindent: number | null = null;
lines.forEach(l => {
let m = l.match(/^(\s+)\S+/);
if (m) {
Expand All @@ -42,16 +39,13 @@ function dedent(strings, ...values) {
});

if (mindent !== null) {
result = lines.map(l => l[0] === " " ? l.slice(mindent) : l).join("\n");
const m = mindent; // appease Flow
result = lines.map(l => l[0] === " " ? l.slice(m) : l).join("\n");
}

// dedent eats leading and trailing whitespace too
result = result.trim();

// handle escaped newlines at the end to ensure they don't get stripped too
return result.replace(/\\n/g, "\n");
}

if (typeof module !== "undefined") {
module.exports = dedent;
return result.
// dedent eats leading and trailing whitespace too
trim().
// handle escaped newlines at the end to ensure they don't get stripped too
replace(/\\n/g, "\n");
}
33 changes: 15 additions & 18 deletions dist/dedent.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = dedent;
function dedent(strings) {

var raw = void 0;
if (typeof strings === "string") {
// dedent can be used as a plain function
raw = [strings];
} else {
raw = strings.raw;
}
// $FlowFixMe: Flow doesn't undestand .raw
var raw = typeof strings === "string" ? [strings] : strings.raw;

// first, perform interpolation
var result = "";
Expand Down Expand Up @@ -42,18 +40,17 @@ function dedent(strings) {
});

if (mindent !== null) {
result = lines.map(function (l) {
return l[0] === " " ? l.slice(mindent) : l;
}).join("\n");
(function () {
var m = mindent; // appease Flow
result = lines.map(function (l) {
return l[0] === " " ? l.slice(m) : l;
}).join("\n");
})();
}

return result.
// dedent eats leading and trailing whitespace too
result = result.trim();

trim().
// handle escaped newlines at the end to ensure they don't get stripped too
return result.replace(/\\n/g, "\n");
}

if (typeof module !== "undefined") {
module.exports = dedent;
replace(/\\n/g, "\n");
}
Loading

0 comments on commit e13e334

Please sign in to comment.