-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new
changeSetting()
function
- for example `calendar.changeSetting('range', { min: 'today' });`
- Loading branch information
1 parent
5ba6b06
commit 74e0d09
Showing
4 changed files
with
66 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type VanillaCalendar from '@src/vanilla-calendar'; | ||
import * as T from '@package/types'; | ||
|
||
/** | ||
* Deep merge two objects. | ||
* @param target | ||
* @param ...sources | ||
*/ | ||
export function mergeDeep(target: any, ...sources: any[]): any { | ||
const isObject = (item: any) => (item && typeof item === 'object' && !Array.isArray(item)); | ||
|
||
if (!sources.length) return target; | ||
const source = sources.shift(); | ||
|
||
if (isObject(target) && isObject(source)) { | ||
Object.keys(source).forEach((key: any) => { | ||
if (isObject(source[key])) { | ||
if (!target[key]) Object.assign(target, { [key]: {} }); | ||
mergeDeep(target[key], source[key]); | ||
} else { | ||
Object.assign(target, { [key]: source[key] }); | ||
} | ||
}); | ||
} | ||
|
||
return mergeDeep(target, ...sources); | ||
} | ||
|
||
export function changeSetting<S extends keyof T.IPartialSettings, K extends T.IPartialSettings[S]>(self: VanillaCalendar, option: S, value: K) { | ||
self.settings = mergeDeep(self.settings, { [option]: value }) as T.ISettings; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters