From 1151332d3085deb56364ee0c34c2354cf15f9456 Mon Sep 17 00:00:00 2001 From: Alejandro Gallardo Escobar Date: Fri, 8 Mar 2019 12:29:22 +0100 Subject: [PATCH] Visual Console Client: added a new lib function Former-commit-id: 6bbfc18d06e36539f1b88084ecddcf8b65101e2e --- visual_console/src/lib.spec.ts | 14 +++++++++++++- visual_console/src/lib.ts | 11 +++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/visual_console/src/lib.spec.ts b/visual_console/src/lib.spec.ts index 3670bc677f..4b13babfae 100644 --- a/visual_console/src/lib.spec.ts +++ b/visual_console/src/lib.spec.ts @@ -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"); diff --git a/visual_console/src/lib.ts b/visual_console/src/lib.ts index fd5db5beec..9a8a064fc2 100644 --- a/visual_console/src/lib.ts +++ b/visual_console/src/lib.ts @@ -22,6 +22,17 @@ export function parseIntOr(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(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.