From fee8a87c06a732fcad3dd2ba4da484222d0729fe Mon Sep 17 00:00:00 2001 From: Alex Canessa Date: Mon, 2 Nov 2015 11:53:23 +0000 Subject: [PATCH] Add extend and _mergeProperties function, to be able to merge a given option with a default --- src/js/simplemde.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/js/simplemde.js b/src/js/simplemde.js index 09efa05..e0151f4 100644 --- a/src/js/simplemde.js +++ b/src/js/simplemde.js @@ -604,6 +604,50 @@ function _toggleBlock(editor, type, start_chars, end_chars) { cm.focus(); } +/** + * Merge the properties of one object into another. + * + * @param {Object} target The object where the properties will be copied + * @param {Object} source The object whose properties will be copied + * + * @returns {Object} + */ +function _mergeProperties(target, source) { + for(var property in source) { + if (source.hasOwnProperty(property)) { + if (source[property] instanceof Array) { + target[property] = source[property].concat(target[property] instanceof Array ? target[property] : []); + } else if ( + source[property] !== null && + typeof source[property] === 'object' && + source[property].constructor === Object + ) { + target[property] = mergeProperties(target[property] || {}, source[property]); + } else { + target[property] = source[property]; + } + } + } + + return target; +} + +/** + * Merge an arbitrary number of objects into one. + * This function modifies the target object but also returns it. + * + * @param {Object} target The target object of the merge + * + * @returns {Object} + */ +function extend(target) { + for(var i = 1; i < arguments.length; i++) { + target = _mergeProperties(target, arguments[i]); + } + + return target; +} + /* The right word count in respect for CJK. */ function wordCount(data) {