( defaults: T, argsArray: IArguments, keys: Array<keyof T> = [], objKey?: keyof T, )
| 54 | * Convert an args array into an object. |
| 55 | */ |
| 56 | export function optionsFromArguments<T extends object>( |
| 57 | defaults: T, |
| 58 | argsArray: IArguments, |
| 59 | keys: Array<keyof T> = [], |
| 60 | objKey?: keyof T, |
| 61 | ): T { |
| 62 | const opts: Partial<T> = {}; |
| 63 | const args = Array.from(argsArray); |
| 64 | // if the first argument is an object and has an object key |
| 65 | if (isObject(args[0]) && objKey && !Reflect.has(args[0], objKey)) { |
| 66 | // if it's not part of the defaults |
| 67 | const partOfDefaults = Object.keys(args[0]).some(key => Reflect.has(defaults, key)); |
| 68 | if (!partOfDefaults) { |
| 69 | // merge that key |
| 70 | deepMerge(opts, { [objKey]: args[0] }); |
| 71 | // remove the obj key from the keys |
| 72 | keys.splice(keys.indexOf(objKey), 1); |
| 73 | // shift the first argument off |
| 74 | args.shift(); |
| 75 | } |
| 76 | } |
| 77 | if (args.length === 1 && isObject(args[0])) { |
| 78 | deepMerge(opts, args[0]); |
| 79 | } else { |
| 80 | for (let i = 0; i < keys.length; i++) { |
| 81 | if (isDefined(args[i])) { |
| 82 | opts[keys[i]] = args[i]; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | return deepMerge(defaults, opts); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Return this instances default values by calling Constructor.getDefaults() |
no test coverage detected