Skip to content

Commit

Permalink
feature: ESM version exports deepMerge function
Browse files Browse the repository at this point in the history
`deepMerge` function used by the package for deep merging multiple objects is now exported.
  • Loading branch information
zipper committed Apr 11, 2023
1 parent 9549966 commit 2f967c5
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/Collapsable.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CollapsableItem } from './CollapsableItem'
import { DeepPartial, mergeDeep } from './utils'
import { DeepPartial, deepMerge } from './utils'
import { CollapsableExtLink } from './CollapsableExtLink'

export type CollapsableOptions = {
Expand Down Expand Up @@ -76,7 +76,7 @@ export class Collapsable {
}

public constructor(elements: HTMLElement | NodeListOf<HTMLElement>, options?: DeepPartial<CollapsableOptions>) {
this.options = mergeDeep({}, this.defaults, options) as CollapsableOptions
this.options = deepMerge({}, this.defaults, options) as CollapsableOptions
this.promiseOpen = false

if (elements instanceof NodeList) {
Expand Down
5 changes: 2 additions & 3 deletions src/index.esm.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
import { Collapsable } from './Collapsable'

export default Collapsable
export { deepMerge } from './utils'
export { Collapsable as default } from './Collapsable'
4 changes: 1 addition & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import { Collapsable } from './Collapsable'

export default Collapsable
export { Collapsable as default } from './Collapsable'
6 changes: 3 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ function isObject(item: unknown) {
return item && typeof item === 'object' && !Array.isArray(item)
}

export function mergeDeep(target: any, ...sources: any): Record<string, unknown> {
export function deepMerge(target: any, ...sources: any): Record<string, unknown> {
if (!sources.length) return target
const source = sources.shift()

if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isObject(source[key])) {
if (!target[key]) Object.assign(target, { [key]: {} })
mergeDeep(target[key], source[key])
deepMerge(target[key], source[key])
} else {
Object.assign(target, { [key]: source[key] })
}
}
}

return mergeDeep(target, ...sources)
return deepMerge(target, ...sources)
}

// UID for CollapsableItem
Expand Down

0 comments on commit 2f967c5

Please sign in to comment.