From 981bb040c8c9aa2198c2582cfe705b51038d36a1 Mon Sep 17 00:00:00 2001 From: Daniel Castillo <31006608+dcastil@users.noreply.github.com> Date: Tue, 24 Aug 2021 21:58:23 +0200 Subject: [PATCH 1/6] gitignore node_modules in subdirectories --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index b69ec58a..7705bfda 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,6 @@ !/.yarn/sdks/ !/.yarn/versions/ /dist/ -/node_modules/ +node_modules/ .DS_Store *.local From c287e5f448601c3981df892f9b28bfc110a0891d Mon Sep 17 00:00:00 2001 From: Daniel Castillo <31006608+dcastil@users.noreply.github.com> Date: Tue, 24 Aug 2021 22:13:10 +0200 Subject: [PATCH 2/6] join classLists early and don't filter them --- src/tailwind-merge.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/tailwind-merge.ts b/src/tailwind-merge.ts index e376aacc..dbb3009d 100644 --- a/src/tailwind-merge.ts +++ b/src/tailwind-merge.ts @@ -4,7 +4,8 @@ import { Config } from './types' import { mergeClassList } from './merge-classlist' type CreateConfig = (getDefault: typeof getDefaultConfig) => Config -type ClassLists = Array +type ClassLists = ClassListElement[] +type ClassListElement = string | undefined | null type TailwindMerge = (...classLists: ClassLists) => string type ConfigUtils = ReturnType @@ -14,17 +15,16 @@ export function createTailwindMerge(createConfig: CreateConfig): TailwindMerge { let cacheSet: ConfigUtils['cache']['set'] let functionToCall = initTailwindMerge - function initTailwindMerge(classLists: ClassLists) { + function initTailwindMerge(classList: string) { configUtils = createConfigUtils(createConfig(getDefaultConfig)) cacheGet = configUtils.cache.get cacheSet = configUtils.cache.set functionToCall = tailwindMerge - return tailwindMerge(classLists) + return tailwindMerge(classList) } - function tailwindMerge(classLists: ClassLists) { - const classList = classLists.filter(Boolean).join(' ') + function tailwindMerge(classList: string) { const cachedResult = cacheGet(classList) if (cachedResult) { @@ -37,7 +37,7 @@ export function createTailwindMerge(createConfig: CreateConfig): TailwindMerge { return result } - return function callTailwindMerge(...classLists) { - return functionToCall(classLists) + return function callTailwindMerge(...classLists: ClassLists) { + return functionToCall(classLists.join(' ')) } } From 62dbdaf4f8cfdc8b37f96fd038b94a3f7a5d2e59 Mon Sep 17 00:00:00 2001 From: Daniel Castillo <31006608+dcastil@users.noreply.github.com> Date: Tue, 24 Aug 2021 22:35:56 +0200 Subject: [PATCH 3/6] fix public API test --- tests/public-api.test.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/public-api.test.ts b/tests/public-api.test.ts index 21747895..0600ce70 100644 --- a/tests/public-api.test.ts +++ b/tests/public-api.test.ts @@ -14,6 +14,7 @@ test('twMerge() has correct inputs and outputs', () => { expect.any(String) ) expect(twMerge('hello world', undefined)).toStrictEqual(expect.any(String)) + expect(twMerge('hello world', undefined, null)).toStrictEqual(expect.any(String)) // eslint-disable-next-line @typescript-eslint/no-unused-vars const noRun = () => { @@ -22,8 +23,6 @@ test('twMerge() has correct inputs and outputs', () => { // @ts-expect-error twMerge(123) // @ts-expect-error - twMerge(null) - // @ts-expect-error twMerge(true) // @ts-expect-error twMerge({}) @@ -68,6 +67,7 @@ test('createTailwindMerge() has correct inputs and outputs', () => { expect.any(String) ) expect(tailwindMerge('hello world', undefined)).toStrictEqual(expect.any(String)) + expect(tailwindMerge('hello world', undefined, null)).toStrictEqual(expect.any(String)) // eslint-disable-next-line @typescript-eslint/no-unused-vars const noRun = () => { @@ -76,8 +76,6 @@ test('createTailwindMerge() has correct inputs and outputs', () => { // @ts-expect-error tailwindMerge(123) // @ts-expect-error - tailwindMerge(null) - // @ts-expect-error tailwindMerge(true) // @ts-expect-error tailwindMerge({}) From 7f76b184df140fe64494f7e78aaf9c9b89846128 Mon Sep 17 00:00:00 2001 From: Daniel Castillo <31006608+dcastil@users.noreply.github.com> Date: Tue, 24 Aug 2021 22:48:50 +0200 Subject: [PATCH 4/6] allow passing null to `twMerge()` --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a5d267a5..ab762aa2 100644 --- a/README.md +++ b/README.md @@ -126,6 +126,12 @@ twMerge('p-5 p-2 my-non-tailwind-class p-4') // → 'my-non-tailwind-class p-4' twMerge('text-red text-secret-sauce') // → 'text-secret-sauce' ``` +### Ignores `undefined` and `null` values + +```ts +twMerge('some-class', undefined, null) // → 'some-class' +``` + ## API reference Reference to all exports of tailwind-merge. @@ -133,7 +139,7 @@ Reference to all exports of tailwind-merge. ### `twMerge` ```ts -function twMerge(...classLists: Array): string +function twMerge(...classLists: Array): string ``` Default function to use if you're using the default Tailwind config or are close enough to the default config. You can use this function if all of the following points apply to your Tailwind config: From 7569e2898dfed5c5a166d86d306ecb0a671eee56 Mon Sep 17 00:00:00 2001 From: Daniel Castillo <31006608+dcastil@users.noreply.github.com> Date: Tue, 24 Aug 2021 22:50:42 +0200 Subject: [PATCH 5/6] remove dashes from release-drafter labels --- .github/release-drafter.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/release-drafter.yml b/.github/release-drafter.yml index e75ffc1e..b7f81e54 100644 --- a/.github/release-drafter.yml +++ b/.github/release-drafter.yml @@ -8,7 +8,7 @@ change-title-escapes: '\<*_&' no-changes-template: 'No changes' categories: - title: '⚠️ Needs Changelog Edit' - label: 'needs-changelog-edit' + label: 'needs changelog edit' - title: 'Breaking Changes' label: 'breaking' - title: 'Features' @@ -18,7 +18,7 @@ categories: - title: 'Other' label: 'other' exclude-labels: - - 'skip-changelog' + - 'skip changelog' version-resolver: major: labels: From a0907db58c8e52f25ee96fe533b7aef022680305 Mon Sep 17 00:00:00 2001 From: Daniel Castillo <31006608+dcastil@users.noreply.github.com> Date: Tue, 24 Aug 2021 22:52:11 +0200 Subject: [PATCH 6/6] fix readme test --- tests/readme-examples.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/readme-examples.test.ts b/tests/readme-examples.test.ts index cd4197e6..2c5e54d8 100644 --- a/tests/readme-examples.test.ts +++ b/tests/readme-examples.test.ts @@ -6,7 +6,7 @@ const twMergeExampleRegex = /twMerge\((?[\w\s\-:[\]#(),!\n'"]+?)\)(?!.*(?.+)['"]/g test('readme examples', () => { - expect.assertions(16) + expect.assertions(17) return fs.promises .readFile(`${__dirname}/../README.md`, { encoding: 'utf-8' })