Skip to content

Commit

Permalink
feat: #24 - Add indent(times).
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed May 2, 2019
1 parent 9481905 commit 0108dab
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 52 deletions.
10 changes: 5 additions & 5 deletions src/code-block-writer.ts
@@ -1,4 +1,4 @@
import { stringRepeat, escapeForWithinString, getStringFromStrOrFunc } from "./utils/stringUtils";
import { escapeForWithinString, getStringFromStrOrFunc } from "./utils/stringUtils";
import { CommentChar } from "./CommentChar";

export interface Options {
Expand Down Expand Up @@ -207,9 +207,9 @@ export default class CodeBlockWriter {
/**
* Indents the code one level for the current line.
*/
indent() {
indent(times = 1) {
this._newLineIfNewLineOnNextWrite();
return this.write(this._indentationText);
return this.write(this._indentationText.repeat(times));
}

/**
Expand Down Expand Up @@ -264,7 +264,7 @@ export default class CodeBlockWriter {
*/
space(times = 1) {
this._newLineIfNewLineOnNextWrite();
this._writeIndentingNewLines(stringRepeat(" ", times));
this._writeIndentingNewLines(" ".repeat(times));
return this;
}

Expand All @@ -286,7 +286,7 @@ export default class CodeBlockWriter {
*/
tab(times = 1) {
this._newLineIfNewLineOnNextWrite();
this._writeIndentingNewLines(stringRepeat("\t", times));
this._writeIndentingNewLines("\t".repeat(times));
return this;
}

Expand Down
8 changes: 8 additions & 0 deletions src/tests/code-block-writer-tests.ts
Expand Up @@ -437,6 +437,14 @@ test`;
writer.writeLine("test").indent().write("test");
});
});

it("should indent multiple times when specifying an argument", () => {
const expected = `test\n test`;

doTest(expected, writer => {
writer.writeLine("test").indent(2).write("test");
});
});
});

describe("#newLineIfLastNot()", () => {
Expand Down
26 changes: 1 addition & 25 deletions src/tests/utils/stringUtilsTests.ts
@@ -1,29 +1,5 @@
import * as assert from "assert";
import {es5StringRepeat, stringRepeat, escapeChar, escapeForWithinString, getStringFromStrOrFunc} from "../../utils/stringUtils";

describe("string repeat", () => {
function doTests(func: (str: string, times: number) => string) {
it("should repeat the string", () => {
assert.equal(func(" ", 2), " ");
});

it("should not repeat the string if specifying 0", () => {
assert.equal(func(" ", 0), "");
});

it("should throw if specifying a negative number", () => {
assert.throws(() => func(" ", -1));
});
}

describe("es5StringRepeat", () => {
doTests(es5StringRepeat);
});

describe("stringRepeat", () => {
doTests(stringRepeat);
});
});
import { escapeChar, escapeForWithinString, getStringFromStrOrFunc } from "../../utils/stringUtils";

describe("escapeForWithinString", () => {
function doTest(input: string, expected: string) {
Expand Down
20 changes: 0 additions & 20 deletions src/utils/stringUtils.ts
@@ -1,23 +1,3 @@
/** @internal */
export function stringRepeat(str: string, times: number) {
/* istanbul ignore else */
if (typeof (String.prototype as any).repeat === "function")
return (str as any).repeat(times);
else
return es5StringRepeat(str, times);
}

/** @internal */
export function es5StringRepeat(str: string, times: number) {
if (times < 0)
throw new Error("Invalid times value.");

let newStr = "";
for (let i = 0; i < times; i++)
newStr += str;
return newStr;
}

const newlineRegex = /(\r?\n)/g;

/** @internal */
Expand Down
3 changes: 1 addition & 2 deletions tslint.json
Expand Up @@ -9,8 +9,7 @@
"class-name": true,
"comment-format": [
true,
"check-space",
"check-lowercase"
"check-space"
],
"curly": false,
"eofline": true,
Expand Down

0 comments on commit 0108dab

Please sign in to comment.