Skip to content

Commit

Permalink
refactor: remove merge-anything and implement it on our own
Browse files Browse the repository at this point in the history
  • Loading branch information
acro5piano committed Jun 9, 2023
1 parent 332f62a commit 99294d1
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ It's a hard task to debug this library on three platforms. I usually develop wit
- Cross Platform: Runs on the Web, iOS, Android with the power of React
- Type-safe: Fully written in TypeScript
- Customizable: Able to provide your own theme, and pass your component to render.
- Lightweight: ~37.4KB, dependency is `dayjs` and `calendarize` and `merge-anything`
- Lightweight: ~9KB (min + gzip), dependency is `dayjs` and `calendarize`

# Install

Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
},
"dependencies": {
"calendarize": "^1.1.1",
"dayjs": "^1.11.7",
"merge-anything": "5.1.4"
"dayjs": "^1.11.7"
},
"peerDependencies": {
"react": "*",
Expand Down
13 changes: 6 additions & 7 deletions src/components/Calendar.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import dayjs from 'dayjs'
import isBetween from 'dayjs/plugin/isBetween'
import type { merge as TMerge } from 'merge-anything'
import React from 'react'
import { deepMerge } from 'src/utils/object'

import { ICalendarEventBase } from '../interfaces'
import { defaultTheme } from '../theme/defaultTheme'
Expand All @@ -11,11 +11,6 @@ import { typedMemo } from '../utils/react'
import { DeepPartial } from '../utils/utility-types'
import { CalendarContainer, CalendarContainerProps } from './CalendarContainer'

// Since Metro Bundler does not load .cjs and .es.js files, we should require it like this.
// It is still possible to use .cjs by changing Metro config, but it forces library users to take an additional step.
// So this workaround is better.
const merge = require('merge-anything/dist/index.es').merge as typeof TMerge

export interface CalendarProps<T extends ICalendarEventBase> extends CalendarContainerProps<T> {
theme?: DeepPartial<ThemeInterface>
isRTL?: boolean
Expand All @@ -28,7 +23,11 @@ function _Calendar<T extends ICalendarEventBase>({
isRTL,
...props
}: CalendarProps<T>) {
const _theme = merge(defaultTheme, theme, { isRTL }) as ThemeInterface
// TODO: Old prop support. This should be included in custom theme itself.
if (isRTL !== undefined) {
theme.isRTL = isRTL
}
const _theme = deepMerge(defaultTheme, theme) as ThemeInterface
return (
<ThemeContext.Provider value={_theme}>
<CalendarContainer {...props} />
Expand Down
File renamed without changes.
30 changes: 30 additions & 0 deletions src/utils/__tests__/object.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { deepMerge } from '../object'

test('deepMerge', () => {
let obj1 = {
a: 1,
b: {
c: 2,
d: 3,
e: 4,
},
array: [1, 2, 3],
}
let obj2 = {
a: 2,
b: {
c: 2,
d: 3,
},
array: [4, 5],
}
expect(deepMerge(obj1, obj2)).toEqual({
a: 2,
b: {
c: 2,
d: 3,
e: 4,
},
array: [4, 5],
})
})
26 changes: 26 additions & 0 deletions src/utils/object.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
import { TextStyle, ViewStyle } from 'react-native'

import { DeepPartial } from './utility-types'

export function objHasContent(obj: ViewStyle | TextStyle): boolean {
return !!Object.keys(obj).length
}

export function stringHasContent(string: string): boolean {
return !!string.length
}

function isObject(item: any) {
return item && typeof item === 'object' && !Array.isArray(item)
}

function keys<T extends object>(obj: T): Array<keyof T> {
return Object.keys(obj) as any
}

export function deepMerge<T extends object>(target: T, source: DeepPartial<T>): T {
const output: T = Object.assign({}, target)
if (isObject(target) && isObject(source)) {
keys(source).forEach((key) => {
if (isObject(source[key])) {
if (!(key in target)) Object.assign(output, { [key]: source[key] })
// @ts-ignore
else output[key] = deepMerge(target[key], source[key])
} else {
Object.assign(output, { [key]: source[key] })
}
})
}
return output
}
12 changes: 0 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7679,11 +7679,6 @@ is-weakref@^1.0.2:
dependencies:
call-bind "^1.0.2"

is-what@^4.1.8:
version "4.1.8"
resolved "https://registry.yarnpkg.com/is-what/-/is-what-4.1.8.tgz#0e2a8807fda30980ddb2571c79db3d209b14cbe4"
integrity sha512-yq8gMao5upkPoGEU9LsB2P+K3Kt8Q3fQFCGyNCWOAnJAMzEXVV9drYb0TXr42TTliLLhKIBvulgAXgtLLnwzGA==

is-whitespace-character@^1.0.0:
version "1.0.4"
resolved "https://registry.yarnpkg.com/is-whitespace-character/-/is-whitespace-character-1.0.4.tgz#0858edd94a95594c7c9dd0b5c174ec6e45ee4aa7"
Expand Down Expand Up @@ -8985,13 +8980,6 @@ meow@^7.0.1:
type-fest "^0.13.1"
yargs-parser "^18.1.3"

[email protected]:
version "5.1.4"
resolved "https://registry.yarnpkg.com/merge-anything/-/merge-anything-5.1.4.tgz#c63132bfa5a4eddeff6f8dfa0df9d930253c8dee"
integrity sha512-7PWKwGOs5WWcpw+/OvbiFiAvEP6bv/QHiicigpqMGKIqPPAtGhBLR8LFJW+Zu6m9TXiR/a8+AiPlGG0ko1ruoQ==
dependencies:
is-what "^4.1.8"

[email protected]:
version "1.0.1"
resolved "https://registry.yarnpkg.com/merge-descriptors/-/merge-descriptors-1.0.1.tgz#b00aaa556dd8b44568150ec9d1b953f3f90cbb61"
Expand Down

0 comments on commit 99294d1

Please sign in to comment.