Skip to content

Commit

Permalink
chore: add merge function with tests (#81)
Browse files Browse the repository at this point in the history
- Implement a `merge` function to combine two objects
- Add unit tests to `merge` function
  • Loading branch information
halvaradop authored Oct 24, 2024
1 parent eaf360e commit d2fd224
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
29 changes: 29 additions & 0 deletions packages/tailwindcss-core/src/generate-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import tailwindcss from "tailwindcss"
import minify from "@csstools/postcss-minify"
import { PluginsConfig } from "tailwindcss/types/config.js"
import { FlattenArrayType } from "@halvaradop/ts-utility-types"
import { isObject, isPrimitive } from "@halvaradop/ts-utility-types/utils"

const TAILWIND_UTILITIES_DIRECTIVE = "@tailwind utilities;"

Expand Down Expand Up @@ -32,3 +33,31 @@ export const extractClasses = (plugin: FlattenArrayType<PluginsConfig>) => {
}
return generateCSS
}

/**
* Create a new object by merging two objects which prioritize the object types
*
* @param source first object to merge
* @param target second object to merge
* @param priority true if the object type should be prioritized, false otherwise
* @returns merged two objects
*/
export const merge = <S extends Record<string, unknown>, T extends Record<string, unknown>>(
source: S,
target: T,
priority: boolean = true
) => {
if (priority && isPrimitive(source) && isObject(target)) return target
if (priority && isPrimitive(target) && isObject(source)) return source
const merged: Record<string, unknown> = { ...source }
for (const key in target) {
if (!isObject(target[key])) {
if ((priority && !isObject(source[key])) || !priority) {
merged[key] = target[key]
}
} else {
merged[key] = merge(source[key] as S, target[key] as T, priority)
}
}
return merged
}
47 changes: 46 additions & 1 deletion packages/tailwindcss-core/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, test, expect } from "vitest"
import { extractClasses } from "../src/generate-classes"
import { extractClasses, merge } from "../src/generate-classes"

/**
* Provide a mock implementation of the extractClasses function with an
Expand Down Expand Up @@ -30,3 +30,48 @@ describe("Extract classes from tailwindcss", () => {
expect(css).toMatch(".text-lg{font-size:1.125rem;line-height:1.75rem}")
})
})

describe("Merge objects", () => {
test("Merge two objects", () => {
const source = { a: 1, b: 2 }
const target = { b: 3, c: 4 }
expect(merge(source, target)).toEqual({ a: 1, b: 3, c: 4 })
})

test("Merge two objects with nested objects", () => {
const source = { a: 1, b: { c: 2 } }
const target = { b: 3, c: 4 }
expect(merge(source, target)).toEqual({ a: 1, b: { c: 2 }, c: 4 })
})

test("Merge two objects with nested objects and nested objects", () => {
const source = { a: 1, b: { c: 2 } }
const target = { b: { c: 3 }, c: 4 }
expect(merge(source, target)).toEqual({ a: 1, b: { c: 3 }, c: 4 })
})

test("Merge two objects with nested objects and nested objects with different keys", () => {
const source = { a: 1, b: { c: 2 } }
const target = { b: { d: 3 }, c: 4 }
expect(merge(source, target)).toEqual({ a: 1, b: { c: 2, d: 3 }, c: 4 })
})

test("Merge two objects with nested objects and nested objects with different keys and nested objects", () => {
const source = { a: 1, b: { c: 2, d: { e: 3 } } }
const target = { b: { d: 4 }, c: 5 }
expect(merge(source, target)).toEqual({ a: 1, b: { c: 2, d: { e: 3 } }, c: 5 })
})

test("Merge two objects with nested objects and nested objects with different keys and nested objects with different keys", () => {
const source = { a: 1, b: { c: 2, d: { e: 3 } } }
const target = { b: { d: { f: 4 } }, c: 5 }
expect(merge(source, target)).toEqual({ a: 1, b: { c: 2, d: { e: 3, f: 4 } }, c: 5 })
})

test("Merge two object with nested object and nested object without priority", () => {
const source = { a: 1, b: { c: 2, d: { e: 3 } } }
const target = { b: { d: 4 }, c: 5 }
expect(merge(source, target, false)).toEqual({ a: 1, b: { c: 2, d: 4 }, c: 5 })
})

})

0 comments on commit d2fd224

Please sign in to comment.