Visual Console Client: added a new lib function

Former-commit-id: 6bbfc18d06e36539f1b88084ecddcf8b65101e2e
This commit is contained in:
Alejandro Gallardo Escobar 2019-03-08 12:29:22 +01:00
parent 913a69e99a
commit 1151332d30
2 changed files with 24 additions and 1 deletions

View File

@ -1,4 +1,4 @@
import { parseIntOr, padLeft, prefixedCssRules } from "./lib";
import { parseIntOr, notEmptyStringOr, padLeft, prefixedCssRules } from "./lib";
describe("function parseIntOr", () => {
it("should retrieve valid int or a default value", () => {
@ -12,6 +12,18 @@ describe("function parseIntOr", () => {
});
});
describe("function notEmptyStringOr", () => {
it("should retrieve not empty string or a default value", () => {
expect(notEmptyStringOr("", null)).toBe(null);
expect(notEmptyStringOr("Foo", null)).toBe("Foo");
expect(notEmptyStringOr(1, 1)).toBe(1);
expect(notEmptyStringOr(1, 0)).toBe(0);
expect(notEmptyStringOr("", 0)).toBe(0);
expect(notEmptyStringOr("Foo", "Bar")).toBe("Foo");
expect(notEmptyStringOr(0, "Bar")).toBe("Bar");
});
});
describe("function padLeft", () => {
it("should pad properly", () => {
expect(padLeft(1, 2, 0)).toBe("01");

View File

@ -22,6 +22,17 @@ export function parseIntOr<T>(value: any, defaultValue: T): number | T {
else return defaultValue;
}
/**
* Return a not empty string or a default value from a raw value.
* @param value Raw value from which we will try to extract a non empty string.
* @param defaultValue Default value to use if we cannot extract a non empty string.
* @return A non empty string or the default value.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function notEmptyStringOr<T>(value: any, defaultValue: T): string | T {
return typeof value === "string" && value.length > 0 ? value : defaultValue;
}
/**
* Return a boolean from a raw value.
* @param value Raw value from which we will try to extract the boolean.