-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4e11f06
Showing
12 changed files
with
5,090 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"presets": [["es2015", {"modules": false}]], | ||
"env": { | ||
"test": { | ||
"presets": [["es2015"]] | ||
} | ||
} | ||
} |
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 @@ | ||
*.test.js |
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,3 @@ | ||
module.exports = { | ||
extends: 'standard' | ||
} |
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,2 @@ | ||
/.idea | ||
/node_modules |
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,110 @@ | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
typeof define === 'function' && define.amd ? define(factory) : | ||
(global['merge-concat'] = factory()); | ||
}(this, (function () { 'use strict'; | ||
|
||
/* | ||
|------------------------------------------------------ | ||
| Merge Concat | ||
|------------------------------------------------------ | ||
| | ||
| Merges the passed in objects recursively while | ||
| concatenating internal arrays. | ||
| | ||
| NOTE: the process is completely functional, so it | ||
| does not mutate any of the passed arguments. | ||
| | ||
*/ | ||
var isArray = Array.isArray; | ||
var isObject = function isObject(object) { | ||
return Object.prototype.toString.call(object) === '[object Object]'; | ||
}; | ||
|
||
/** | ||
* We're abstracting out Array.prototype.concat | ||
* into a separate function, so that we could | ||
* easily give default values to the args. | ||
*/ | ||
var concatArrays = function concatArrays() { | ||
var array1 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; | ||
var array2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; | ||
|
||
return array1.concat(array2); | ||
}; | ||
|
||
/** | ||
* Checks each key on the source and takes | ||
* the appropriate merging action. | ||
* | ||
* @param {Object} target - The object to merge into | ||
* @param {Object} source - The object to merge from | ||
*/ | ||
var mergeObjectAttributes = function mergeObjectAttributes(target, source) { | ||
Object.keys(source).forEach(function (key) { | ||
if (isArray(source[key])) { | ||
/** | ||
* Arrays need to be merged. | ||
*/ | ||
target[key] = concatArrays(target[key], source[key]); | ||
return; | ||
} | ||
|
||
if (isObject(source[key])) { | ||
/** | ||
* Objects need a recursive call. | ||
*/ | ||
target[key] = mergeConcat(target[key], source[key]); | ||
return; | ||
} | ||
|
||
/** | ||
* No special handling necesary in | ||
* all other scenarios. | ||
*/ | ||
target[key] = source[key]; | ||
}); | ||
}; | ||
|
||
/** | ||
* Merges the passed in objects recursively | ||
* while concatenating internal arrays. | ||
* | ||
* @param {...Object} objects - One or more objects that should be merged | ||
* with latter taking precedence | ||
* | ||
* @return {Object} Merged object with concatenated internal arrays | ||
*/ | ||
var mergeConcat = function mergeConcat() { | ||
for (var _len = arguments.length, objects = Array(_len), _key = 0; _key < _len; _key++) { | ||
objects[_key] = arguments[_key]; | ||
} | ||
|
||
return objects.reduce(function (target, source) { | ||
/** | ||
* Normalize the source into an object | ||
* if it's of a different type. | ||
*/ | ||
if (!isObject(source)) { | ||
source = {}; | ||
} | ||
|
||
/** | ||
* This procedure is not functional. It has | ||
* the side-effect of mutating the object | ||
* referenced by the accumulatorObject | ||
* argument. | ||
*/ | ||
mergeObjectAttributes(target, source); | ||
|
||
return target; | ||
}, {} /* Start with an empty object */); | ||
}; | ||
|
||
if (typeof window !== 'undefined') { | ||
window.mergeConcat = mergeConcat; | ||
} | ||
|
||
return mergeConcat; | ||
|
||
}))); |
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,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2017 Theodore Messinezis | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
Oops, something went wrong.