Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for outdenting string arguments. #8

Merged
merged 14 commits into from
Mar 17, 2018
Merged
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ function createInstance(options: Options): Outdent {
}
}

(outdent as any).string = (str: string): string => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will currently be breaking the build. I have absolutely no idea how to type this in TS. Halp.

Copy link
Contributor Author

@treshugart treshugart Mar 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries, I'll make a commit to fix the types.

type Fn = () => string;
type Test = Fn & {
  method: Fn
};

That actually works in TypeScript; it was just balking at augmenting a function without first declaring the extended interface. (outdent as Outdent).string = does the trick.

One of these days I'll learn some Flow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, that would explain my confusion then. So close!

return _outdent([str], [], outdent, options);
};

return outdent;
}

Expand Down
23 changes: 15 additions & 8 deletions test/spec/spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ function makeStrings(...strings: Array<string>): TemplateStringsArray {
}

describe('outdent', () => {

it('Removes indentation', () => {
expect(outdent`
Hello
Expand Down Expand Up @@ -35,7 +34,7 @@ describe('outdent', () => {
expect(outdent`
Hello
World

`).to.equal('Hello\nWorld\n');
});

Expand All @@ -62,15 +61,15 @@ removed
`).to.equal('Hello\n\nWorld');
expect(outdent`
Hello

World
`).to.equal('Hello\n\nWorld');
});

it('Preserves trailing spaces on blank lines', () => {
expect(outdent`
Hello

World
`).to.equal('Hello\n \nWorld');
});
Expand All @@ -94,7 +93,7 @@ removed
`).to.equal('5678');

expect(od`

${ od }
12345678
`).to.equal('5678');
Expand Down Expand Up @@ -153,7 +152,6 @@ removed
Hello
World
`).to.equal('Hello\nWorld\n');

});
it('Does not trim trailing nor leading newline when asked not to', () => {
expect(outdent({
Expand All @@ -174,14 +172,14 @@ removed
trimLeadingNewline: false,
trimTrailingNewline: false,
}) `

`).to.equal('\n\n');
});

it('Merges options objects', () => {
const customOutdent = outdent({ trimLeadingNewline: false })({ trimTrailingNewline: false });
expect(customOutdent`

`).to.equal('\n\n');

expect(customOutdent({ trimLeadingNewline: true }) `
Expand Down Expand Up @@ -215,4 +213,13 @@ removed
expect(outdent`Hello world!
`).to.equal('Hello world!');
});

it('outdent.string takes strings as input and formats them', () => {
expect(
outdent.string(`
Hello world!
Hello world!
`)
).to.equal('Hello world!\n Hello world!');
});
});
5 changes: 5 additions & 0 deletions test/ts/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ const newOutdent2 = newOutdent1({ trimLeadingNewline: false });
s = newOutdent2`
hello ${ 123 } world`;
const outdent2: Outdent = newOutdent2;
s = outdent.string(`
bar
baz
qux
`);