code-block-writer
Advanced tools
Comparing version 6.13.0 to 6.14.0
@@ -5,2 +5,12 @@ # Change Log | ||
<a name="6.14.0"></a> | ||
# [6.14.0](https://github.com/dsherret/code-block-writer/compare/v6.13.0...v6.14.0) (2018-04-10) | ||
### Features | ||
* allow passing a function into conditional write methods ([d299c84](https://github.com/dsherret/code-block-writer/commit/d299c84)) | ||
Thanks a lot to [@lazarljubenovic](https://github.com/lazarljubenovic) for implementing this feature! | ||
<a name="6.13.0"></a> | ||
@@ -7,0 +17,0 @@ # [6.13.0](https://github.com/dsherret/code-block-writer/compare/v6.12.0...v6.13.0) (2018-04-08) |
@@ -68,6 +68,12 @@ export interface Options { | ||
* @param condition - Condition to evaluate. | ||
* @param str - String to write if the condition is true. | ||
* @param textFunc - A function that returns a string to write if the condition is true. | ||
*/ | ||
conditionalWriteLine(condition: boolean | undefined, str: string): this; | ||
conditionalWriteLine(condition: boolean | undefined, textFunc: () => string): this; | ||
/** | ||
* Conditionally writes a line of text. | ||
* @param condition - Condition to evaluate. | ||
* @param text - Text to write if the condition is true. | ||
*/ | ||
conditionalWriteLine(condition: boolean | undefined, text: string): this; | ||
/** | ||
* Writes a line of text. | ||
@@ -141,6 +147,12 @@ * @param str - String to write. | ||
/** | ||
* Writes the provided text if the condition is true. | ||
* Conditionally writes text. | ||
* @param condition - Condition to evaluate. | ||
* @param text - Text to write. | ||
* @param textFunc - A function that returns a string to write if the condition is true. | ||
*/ | ||
conditionalWrite(condition: boolean | undefined, textFunc: () => string): this; | ||
/** | ||
* Conditionally writes text. | ||
* @param condition - Condition to evaluate. | ||
* @param text - Text to write if the condition is true. | ||
*/ | ||
conditionalWrite(condition: boolean | undefined, text: string): this; | ||
@@ -147,0 +159,0 @@ /** |
@@ -89,10 +89,5 @@ "use strict"; | ||
}; | ||
/** | ||
* Conditionally writes a line of text. | ||
* @param condition - Condition to evaluate. | ||
* @param str - String to write if the condition is true. | ||
*/ | ||
CodeBlockWriter.prototype.conditionalWriteLine = function (condition, str) { | ||
CodeBlockWriter.prototype.conditionalWriteLine = function (condition, strOrFunc) { | ||
if (condition) | ||
this.writeLine(str); | ||
this.writeLine(stringUtils_1.getStringFromStrOrFunc(strOrFunc)); | ||
return this; | ||
@@ -213,10 +208,5 @@ }; | ||
}; | ||
/** | ||
* Writes the provided text if the condition is true. | ||
* @param condition - Condition to evaluate. | ||
* @param text - Text to write. | ||
*/ | ||
CodeBlockWriter.prototype.conditionalWrite = function (condition, text) { | ||
CodeBlockWriter.prototype.conditionalWrite = function (condition, textOrFunc) { | ||
if (condition) | ||
this.write(text); | ||
this.write(stringUtils_1.getStringFromStrOrFunc(textOrFunc)); | ||
return this; | ||
@@ -431,1 +421,2 @@ }; | ||
} | ||
//# sourceMappingURL=code-block-writer.js.map |
export {}; |
@@ -9,1 +9,2 @@ "use strict"; | ||
})(CommentChar = exports.CommentChar || (exports.CommentChar = {})); | ||
//# sourceMappingURL=CommentChar.js.map |
export {}; |
@@ -40,1 +40,7 @@ "use strict"; | ||
exports.escapeChar = escapeChar; | ||
/** @internal */ | ||
function getStringFromStrOrFunc(strOrFunc) { | ||
return strOrFunc instanceof Function ? strOrFunc() : strOrFunc; | ||
} | ||
exports.getStringFromStrOrFunc = getStringFromStrOrFunc; | ||
//# sourceMappingURL=stringUtils.js.map |
{ | ||
"name": "code-block-writer", | ||
"version": "6.13.0", | ||
"version": "6.14.0", | ||
"description": "A simple code writer that assists with formatting and visualizing blocks of code.", | ||
@@ -8,4 +8,7 @@ "main": "dist/code-block-writer.js", | ||
"scripts": { | ||
"test": "gulp test && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js", | ||
"build": "gulp typescript" | ||
"test": "nyc --reporter=lcov mocha --opts mocha.opts", | ||
"test-ci": "npm run test && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js", | ||
"build": "rimraf dist && tsc", | ||
"lint": "tslint src/**/*.ts", | ||
"dopublish": "npm run build && npm publish" | ||
}, | ||
@@ -27,2 +30,16 @@ "repository": { | ||
"homepage": "https://github.com/dsherret/code-block-writer#readme", | ||
"nyc": { | ||
"extension": [ | ||
".ts", | ||
".tsx" | ||
], | ||
"include": [ | ||
"src/**/*.ts", | ||
"!src/tests/**/*.ts" | ||
], | ||
"reporter": [ | ||
"html" | ||
], | ||
"all": true | ||
}, | ||
"typescript": { | ||
@@ -37,9 +54,7 @@ "definition": "code-block-writer.d.ts" | ||
"del": "^2.0.2", | ||
"gulp": "^3.9.1", | ||
"gulp-istanbul": "^1.1.1", | ||
"gulp-mocha": "^2.2.0", | ||
"gulp-tslint": "^8.1.1", | ||
"gulp-typescript": "^2.13.3", | ||
"merge2": "^1.2.0", | ||
"mocha": "^3.3.0", | ||
"nyc": "^11.6.0", | ||
"source-map-support": "^0.5.4", | ||
"ts-node": "^5.0.1", | ||
"tslint": "^5.6.0", | ||
@@ -46,0 +61,0 @@ "typescript": "^2.8.1" |
@@ -66,7 +66,9 @@ code-block-writer | ||
* `spaceIfLastNot()` - Writes a space if the last was not a space. | ||
* `write(str: string)` - Writes some text. | ||
* `write(text: string)` - Writes some text. | ||
* `conditionalNewLine(condition: boolean)` - Writes a newline if the condition is matched. | ||
* `conditionalBlankLine(condition: boolean)` - Writes a blank line if the condition is matched. | ||
* `conditionalWrite(condition: boolean, str: string)` - Writes if the condition is matched. | ||
* `conditionalWriteLine(condition: boolean, str: string)` - Writes some text and adds a newline if the condition is matched. | ||
* `conditionalWrite(condition: boolean, text: string)` - Writes if the condition is matched. | ||
* `conditionalWrite(condition: boolean, textFunc: () => string)` - Writes if the condition is matched. | ||
* `conditionalWriteLine(condition: boolean, text: string)` - Writes some text and adds a newline if the condition is matched. | ||
* `conditionalWriteLine(condition: boolean, textFunc: () => string)` - Writes some text and adds a newline if the condition is matched. | ||
* `setIndentationLevel(indentationLevel: number)` - Sets the current indentation level. | ||
@@ -73,0 +75,0 @@ * `getIndentationLevel()` - Gets the current indentation level. |
@@ -1,3 +0,3 @@ | ||
import {stringRepeat, escapeForWithinString} from "./utils/stringUtils"; | ||
import {CommentChar} from "./CommentChar"; | ||
import { stringRepeat, escapeForWithinString, getStringFromStrOrFunc } from "./utils/stringUtils"; | ||
import { CommentChar } from "./CommentChar"; | ||
@@ -139,7 +139,15 @@ export interface Options { | ||
* @param condition - Condition to evaluate. | ||
* @param str - String to write if the condition is true. | ||
* @param textFunc - A function that returns a string to write if the condition is true. | ||
*/ | ||
conditionalWriteLine(condition: boolean | undefined, str: string) { | ||
conditionalWriteLine(condition: boolean | undefined, textFunc: () => string): this; | ||
/** | ||
* Conditionally writes a line of text. | ||
* @param condition - Condition to evaluate. | ||
* @param text - Text to write if the condition is true. | ||
*/ | ||
conditionalWriteLine(condition: boolean | undefined, text: string): this; | ||
conditionalWriteLine(condition: boolean | undefined, strOrFunc: string | (() => string)) { | ||
if (condition) | ||
this.writeLine(str); | ||
this.writeLine(getStringFromStrOrFunc(strOrFunc)); | ||
return this; | ||
@@ -289,9 +297,16 @@ } | ||
/** | ||
* Writes the provided text if the condition is true. | ||
* Conditionally writes text. | ||
* @param condition - Condition to evaluate. | ||
* @param text - Text to write. | ||
* @param textFunc - A function that returns a string to write if the condition is true. | ||
*/ | ||
conditionalWrite(condition: boolean | undefined, text: string) { | ||
conditionalWrite(condition: boolean | undefined, textFunc: () => string): this; | ||
/** | ||
* Conditionally writes text. | ||
* @param condition - Condition to evaluate. | ||
* @param text - Text to write if the condition is true. | ||
*/ | ||
conditionalWrite(condition: boolean | undefined, text: string): this; | ||
conditionalWrite(condition: boolean | undefined, textOrFunc: string | (() => string)) { | ||
if (condition) | ||
this.write(text); | ||
this.write(getStringFromStrOrFunc(textOrFunc)); | ||
@@ -509,3 +524,3 @@ return this; | ||
const {spacesCount, tabsCount} = getSpacesAndTabsCount(countOrText); | ||
const { spacesCount, tabsCount } = getSpacesAndTabsCount(countOrText); | ||
return tabsCount + Math.round(Math.max(0, spacesCount - 1) / this._indentNumberOfSpaces); | ||
@@ -535,3 +550,3 @@ } | ||
return {spacesCount, tabsCount}; | ||
return { spacesCount, tabsCount }; | ||
} |
@@ -644,3 +644,3 @@ import * as assert from "assert"; | ||
describe("#conditionalWrite()", () => { | ||
it("should write when the condition is true", () => { | ||
it("should write the given string when the condition is true", () => { | ||
doTest("test", writer => { | ||
@@ -651,3 +651,3 @@ writer.conditionalWrite(true, "test"); | ||
it("should not write when the condition is false", () => { | ||
it("should not write the given string when the condition is false", () => { | ||
doTest("", writer => { | ||
@@ -658,3 +658,3 @@ writer.conditionalWrite(false, "test"); | ||
it("should not write when the condition is undefined", () => { | ||
it("should not write the given string when the condition is undefined", () => { | ||
doTest("", writer => { | ||
@@ -664,6 +664,25 @@ writer.conditionalWrite(undefined, "test"); | ||
}); | ||
it("should write the result of the given function when the condition is true", () => { | ||
doTest("test", writer => { | ||
writer.conditionalWrite(true, () => "test"); | ||
}); | ||
}); | ||
it("should not write the result of the given function when the condition is false", () => { | ||
doTest("", writer => { | ||
writer.conditionalWrite(false, () => "test"); | ||
}); | ||
}); | ||
it("should not evaluate the given function when the condition is false", () => { | ||
const test: any = null; | ||
doTest("", writer => { | ||
writer.conditionalWrite(false, () => test.test); | ||
}); | ||
}); | ||
}); | ||
describe("#conditionalWriteLine()", () => { | ||
it("should write when the condition is true", () => { | ||
it("should write the given string when the condition is true", () => { | ||
doTest("test\n", writer => { | ||
@@ -674,3 +693,3 @@ writer.conditionalWriteLine(true, "test"); | ||
it("should not write when the condition is false", () => { | ||
it("should not write the given string when the condition is false", () => { | ||
doTest("", writer => { | ||
@@ -681,3 +700,3 @@ writer.conditionalWriteLine(false, "test"); | ||
it("should not write when the condition is undefined", () => { | ||
it("should not write the given string when the condition is undefined", () => { | ||
doTest("", writer => { | ||
@@ -687,2 +706,21 @@ writer.conditionalWriteLine(undefined, "test"); | ||
}); | ||
it("should write the result of the given function when the condition is true", () => { | ||
doTest("test\n", writer => { | ||
writer.conditionalWriteLine(true, () => "test"); | ||
}); | ||
}); | ||
it("should not write the result of the given function when the condition is false", () => { | ||
doTest("", writer => { | ||
writer.conditionalWriteLine(false, () => "test"); | ||
}); | ||
}); | ||
it("should not evaluate the given function when the condition is false", () => { | ||
const test: any = null; | ||
doTest("", writer => { | ||
writer.conditionalWriteLine(false, () => test.test); | ||
}); | ||
}); | ||
}); | ||
@@ -689,0 +727,0 @@ } |
import * as assert from "assert"; | ||
import {es5StringRepeat, stringRepeat, escapeChar, escapeForWithinString} from "../../utils/stringUtils"; | ||
import {es5StringRepeat, stringRepeat, escapeChar, escapeForWithinString, getStringFromStrOrFunc} from "../../utils/stringUtils"; | ||
@@ -59,1 +59,11 @@ describe("string repeat", () => { | ||
}); | ||
describe("getStringFromStrOrFunc", () => { | ||
it("should return a string when given a string", () => { | ||
assert.equal(getStringFromStrOrFunc("test"), "test"); | ||
}); | ||
it("should return a string when given a function", () => { | ||
assert.equal(getStringFromStrOrFunc(() => "test"), "test"); | ||
}); | ||
}); |
@@ -39,1 +39,6 @@ /** @internal */ | ||
} | ||
/** @internal */ | ||
export function getStringFromStrOrFunc(strOrFunc: string | (() => string)) { | ||
return strOrFunc instanceof Function ? strOrFunc() : strOrFunc; | ||
} |
@@ -6,3 +6,3 @@ { | ||
"declaration": true, | ||
"sourceMap": false, | ||
"sourceMap": true, | ||
"removeComments": false, | ||
@@ -9,0 +9,0 @@ "experimentalDecorators": true, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11
2339
86
186028
21