From 39031f80677a65717796a8b9641c3634cbc12158 Mon Sep 17 00:00:00 2001 From: Vladislav Kibenko Date: Wed, 27 Mar 2024 17:50:15 +0300 Subject: [PATCH 1/2] feat(utils): improve classNames --- .../src/classnames/__tests__/classNames.ts | 4 +++ packages/sdk/src/classnames/classNames.ts | 32 ++++++++++++------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/packages/sdk/src/classnames/__tests__/classNames.ts b/packages/sdk/src/classnames/__tests__/classNames.ts index 2c0a6d97a..24989a3d1 100644 --- a/packages/sdk/src/classnames/__tests__/classNames.ts +++ b/packages/sdk/src/classnames/__tests__/classNames.ts @@ -6,6 +6,10 @@ it('should ignore all non-empty strings and objects', () => { expect(classNames('', 2, 'b', null, undefined, false, true, [], 'a', 'c')).toBe('b a c'); }); +it('should apply classNames function to the item, if it is an array', () => { + expect(classNames(['a', 'b', 'c'])).toBe('a b c'); +}) + it('should pick only keys which values are truthy', () => { expect(classNames({ a: true, diff --git a/packages/sdk/src/classnames/classNames.ts b/packages/sdk/src/classnames/classNames.ts index 1a9dc883c..ba5fcadbb 100644 --- a/packages/sdk/src/classnames/classNames.ts +++ b/packages/sdk/src/classnames/classNames.ts @@ -1,3 +1,5 @@ +import { isRecord } from '../misc/isRecord.js'; + /** * Inserts a space between a and b in case both of them are * non-empty strings. @@ -5,30 +7,36 @@ * @param b */ function space(a: string, b: string): string { - return a + (a.length > 0 && b.length > 0 ? ` ${b}` : b); + return a + (a.length && b.length ? ` ${b}` : b); } /** * Function which joins passed values with space following these rules: * 1. If value is non-empty string, it will be added to output. * 2. If value is object, only those keys will be added, which values are truthy. - * 3. All other values are ignored. + * 3. If value is array, classNames will be called with this value spread. + * 4. All other values are ignored. * - * You can find this function to similar one from package {@link https://www.npmjs.com/package/classnames|classnames}. + * You can find this function to similar one from the package {@link https://www.npmjs.com/package/classnames|classnames}. * @param values - values array. + * @returns Final class name. */ export function classNames(...values: any[]): string { - return values.reduce((acc, value) => { - let formattedValue = ''; - + return values.reduce((acc, value) => { if (typeof value === 'string') { - formattedValue = value; - } else if (typeof value === 'object' && value !== null) { - formattedValue = Object - .entries(value) - .reduce((valueAcc, [className, enable]) => (enable ? space(valueAcc, className) : valueAcc), ''); + return space(acc, value); + } + + if (isRecord(value)) { + return space(acc, Object.entries(value).reduce((valueAcc, entry) => { + return (entry[1] ? space(valueAcc, entry[0]) : valueAcc); + }, '')); + } + + if (Array.isArray(value)) { + return space(acc, classNames(...value)); } - return space(acc, formattedValue); + return acc; }, ''); } From 1e43c94ba61362c1fe5d872f436cf1bcbdf15fea Mon Sep 17 00:00:00 2001 From: Vladislav Kibenko Date: Wed, 27 Mar 2024 17:50:41 +0300 Subject: [PATCH 2/2] docs(changeset): Better classNames util --- .changeset/chatty-melons-drum.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/chatty-melons-drum.md diff --git a/.changeset/chatty-melons-drum.md b/.changeset/chatty-melons-drum.md new file mode 100644 index 000000000..abc6a19bd --- /dev/null +++ b/.changeset/chatty-melons-drum.md @@ -0,0 +1,5 @@ +--- +"@tma.js/sdk": patch +--- + +Better classNames util