From f955a325bb171dbcc36133079108a14f5aebe325 Mon Sep 17 00:00:00 2001 From: MarcusNotheis Date: Mon, 25 May 2020 08:09:26 +0200 Subject: [PATCH] feat(StyleClassHelper): add putIfPresent method --- .../base/src/styling/StyleClassHelper.test.ts | 31 +++++++++++++++++++ packages/base/src/styling/StyleClassHelper.ts | 28 ++++++++++++----- 2 files changed, 51 insertions(+), 8 deletions(-) create mode 100644 packages/base/src/styling/StyleClassHelper.test.ts diff --git a/packages/base/src/styling/StyleClassHelper.test.ts b/packages/base/src/styling/StyleClassHelper.test.ts new file mode 100644 index 00000000000..f1c00729f5e --- /dev/null +++ b/packages/base/src/styling/StyleClassHelper.test.ts @@ -0,0 +1,31 @@ +import { StyleClassHelper } from './StyleClassHelper'; + +describe('StyleClassHelper', () => { + test('add class names', () => { + const helper = StyleClassHelper.of('class1', 'class2'); + expect(helper.className).toEqual(`class1 class2`); + }); + + test('put new classes', () => { + const helper = StyleClassHelper.of('firstClass'); + helper.put('secondClass'); + expect(helper.className).toEqual(`firstClass secondClass`); + }); + + test('putIfPresent', () => { + const helper = StyleClassHelper.of('firstClass'); + helper.put('secondClass'); + helper.putIfPresent('ifPresent1'); + helper.putIfPresent('ifPresent2', 'ifPresent3'); + helper.putIfPresent(true === false && 'shouldNotBeThere1', 'a' === 'a' && 'ifPresent4'); + + expect(helper.className).toEqual('firstClass secondClass ifPresent1 ifPresent2 ifPresent3 ifPresent4'); + }); + + test('className, toString and valueOf return same value', () => { + const helper = StyleClassHelper.of('class1', 'class2'); + expect(helper.toString()).toEqual(`class1 class2`); + expect(helper.valueOf()).toEqual(`class1 class2`); + expect(helper.className).toEqual(`class1 class2`); + }); +}); diff --git a/packages/base/src/styling/StyleClassHelper.ts b/packages/base/src/styling/StyleClassHelper.ts index 199ef0c0500..eb71c7b4724 100644 --- a/packages/base/src/styling/StyleClassHelper.ts +++ b/packages/base/src/styling/StyleClassHelper.ts @@ -2,28 +2,40 @@ * Created by d059190 at 16.03.18 */ -class StyleClassHelper extends String { +class StyleClassHelper { private classes: string[] = []; constructor(...classes: string[]) { - super(); this.classes = [...classes]; } - put(...clazz: string[]) { + put(...clazz: string[]): StyleClassHelper { this.classes.push(...clazz); return this; } - valueOf() { - return this.classes.join(' '); + putIfPresent(...clazzes: (string | null | undefined)[]): StyleClassHelper { + for (const clazz of clazzes) { + if (clazz) { + this.classes.push(clazz); + } + } + return this; + } + + valueOf(): string { + return this.className; } - toString() { - return this.valueOf(); + toString(): string { + return this.className; + } + + get className(): string { + return this.classes.join(' '); } - static of(...classes: string[]) { + static of(...classes: string[]): StyleClassHelper { return new StyleClassHelper().put(...classes); } }