mirror of
https://github.com/Tonejs/Tone.js
synced 2024-11-16 00:27:58 +00:00
3efd706458
useful for removing redundant properties in get/set
82 lines
2 KiB
TypeScript
82 lines
2 KiB
TypeScript
// import { BaseToneOptions } from "../Tone";
|
|
import { isDefined, isObject, isUndef } from "./TypeCheck";
|
|
|
|
type BaseToneOptions = import("../Tone").BaseToneOptions;
|
|
|
|
/**
|
|
* Recursively merge an object
|
|
* @param target the object to merge into
|
|
* @param sources the source objects to merge
|
|
*/
|
|
export function deepMerge<T>(target: T, ...sources: T[]): T {
|
|
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]: {} }); }
|
|
deepMerge(target[key], source[key] as any);
|
|
} else {
|
|
Object.assign(target, { [key]: source[key] as any });
|
|
}
|
|
}
|
|
}
|
|
|
|
return deepMerge(target, ...sources);
|
|
}
|
|
|
|
/**
|
|
* Convert an args array into an object.
|
|
*/
|
|
export function optionsFromArguments<T>(defaults: T, argsArray: IArguments, keys: string[] = []): T {
|
|
const opts: any = {};
|
|
const args = Array.from(argsArray);
|
|
if (args.length === 1 && isObject(args[0])) {
|
|
deepMerge(opts, args[0]);
|
|
} else {
|
|
for (let i = 0; i < keys.length; i++) {
|
|
if (isDefined(args[i])) {
|
|
opts[keys[i]] = args[i];
|
|
}
|
|
}
|
|
}
|
|
return deepMerge(defaults, opts);
|
|
}
|
|
|
|
/**
|
|
* Return this instances default values by calling Constructor.getDefaults()
|
|
*/
|
|
export function getDefaultsFromInstance<T>(instance: T): BaseToneOptions {
|
|
type ToneClass = {
|
|
constructor: ToneClass;
|
|
getDefaults: () => BaseToneOptions;
|
|
} & T;
|
|
|
|
return (instance as ToneClass).constructor.getDefaults();
|
|
}
|
|
|
|
/**
|
|
* Returns the fallback if the given object is undefined.
|
|
* Take an array of arguments and return a formatted options object.
|
|
*/
|
|
export function defaultArg<T>(given: T, fallback: T): T {
|
|
if (isUndef(given)) {
|
|
return fallback;
|
|
} else {
|
|
return given;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove all of the properties belonging to omit from obj.
|
|
*/
|
|
export function omitFromObject<T extends object, O extends object>(obj: T, omit: O): Omit<T, keyof O> {
|
|
for (const prop in omit) {
|
|
if (Reflect.has(obj, prop)) {
|
|
// @ts-ignore
|
|
delete obj[prop];
|
|
}
|
|
}
|
|
return obj;
|
|
}
|