Skip to content

Commit

Permalink
Release
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandru-dodon committed Nov 24, 2017
0 parents commit 4e11f06
Show file tree
Hide file tree
Showing 12 changed files with 5,090 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"presets": [["es2015", {"modules": false}]],
"env": {
"test": {
"presets": [["es2015"]]
}
}
}
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.test.js
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: 'standard'
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/.idea
/node_modules
110 changes: 110 additions & 0 deletions dist/index.js
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;

})));
21 changes: 21 additions & 0 deletions license.md
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.
Loading

0 comments on commit 4e11f06

Please sign in to comment.