-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
5 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { expect } from "chai"; | ||
import { CodeBlockWriter } from "../../codeBlockWriter"; | ||
import { WriterFunction } from "../../types"; | ||
import { WriterFunctions } from "../../utils"; | ||
|
||
describe(nameof(WriterFunctions), () => { | ||
function getWriter() { | ||
return new CodeBlockWriter(); | ||
} | ||
|
||
function doWriterTest(action: (writer: CodeBlockWriter) => void, expected: string) { | ||
const writer = getWriter(); | ||
action(writer); | ||
expect(writer.toString()).to.equal(expected); | ||
} | ||
|
||
describe(nameof(WriterFunctions.object), () => { | ||
function doTest(obj: { [key: string]: string | number | WriterFunction | undefined; }, expected: string) { | ||
doWriterTest(writer => WriterFunctions.object(obj)(writer), expected); | ||
} | ||
|
||
it("should write an object with keys", () => { | ||
doTest({ | ||
key1: "'testing'", | ||
key2: 5, | ||
key3: undefined, | ||
key4: writer => writer.write("6"), | ||
key5: "undefined" | ||
}, `{ | ||
key1: 'testing', | ||
key2: 5, | ||
key3, | ||
key4: 6, | ||
key5: undefined | ||
}`); | ||
}); | ||
|
||
it("should write an object without keys on the same line", () => { | ||
doTest({}, `{}`); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { CodeBlockWriter } from "../codeBlockWriter"; | ||
import { WriterFunction } from "../types"; | ||
|
||
/** Functions for writing. */ | ||
export class WriterFunctions { | ||
private constructor() { | ||
} | ||
|
||
/** | ||
* Gets a writer function for writing the provided object as an object literal expression. | ||
* @param obj - Object to write. | ||
*/ | ||
static object(obj: { [key: string]: string | number | WriterFunction | undefined; }): WriterFunction { | ||
return (writer: CodeBlockWriter) => { | ||
const keyNames = Object.keys(obj); | ||
writer.write("{"); | ||
if (keyNames.length > 0) { | ||
writer.indentBlock(() => { | ||
writeObject(); | ||
}); | ||
} | ||
writer.write("}"); | ||
|
||
function writeObject() { | ||
for (let i = 0; i < keyNames.length; i++) { | ||
if (i > 0) | ||
writer.write(",").newLine(); | ||
|
||
const keyName = keyNames[i]; | ||
const value = obj[keyName]; | ||
writer.write(keyName); | ||
if (value != null) { | ||
writer.write(": "); | ||
if (value instanceof Function) | ||
value(writer); | ||
else | ||
writer.write(value.toString()); | ||
} | ||
} | ||
|
||
writer.newLine(); | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters