set.js (1831B)
1import isFunction from '../utils/is-function'; 2import extend from '../utils/extend'; 3import isObject from '../utils/is-object'; 4import hasOwnProp from '../utils/has-own-prop'; 5 6export function set(config) { 7 var prop, i; 8 for (i in config) { 9 if (hasOwnProp(config, i)) { 10 prop = config[i]; 11 if (isFunction(prop)) { 12 this[i] = prop; 13 } else { 14 this['_' + i] = prop; 15 } 16 } 17 } 18 this._config = config; 19 // Lenient ordinal parsing accepts just a number in addition to 20 // number + (possibly) stuff coming from _dayOfMonthOrdinalParse. 21 // TODO: Remove "ordinalParse" fallback in next major release. 22 this._dayOfMonthOrdinalParseLenient = new RegExp( 23 (this._dayOfMonthOrdinalParse.source || this._ordinalParse.source) + 24 '|' + 25 /\d{1,2}/.source 26 ); 27} 28 29export function mergeConfigs(parentConfig, childConfig) { 30 var res = extend({}, parentConfig), 31 prop; 32 for (prop in childConfig) { 33 if (hasOwnProp(childConfig, prop)) { 34 if (isObject(parentConfig[prop]) && isObject(childConfig[prop])) { 35 res[prop] = {}; 36 extend(res[prop], parentConfig[prop]); 37 extend(res[prop], childConfig[prop]); 38 } else if (childConfig[prop] != null) { 39 res[prop] = childConfig[prop]; 40 } else { 41 delete res[prop]; 42 } 43 } 44 } 45 for (prop in parentConfig) { 46 if ( 47 hasOwnProp(parentConfig, prop) && 48 !hasOwnProp(childConfig, prop) && 49 isObject(parentConfig[prop]) 50 ) { 51 // make sure changes to properties don't modify parent config 52 res[prop] = extend({}, res[prop]); 53 } 54 } 55 return res; 56}