diff options
author | Marvin Borner | 2018-11-07 18:02:36 +0100 |
---|---|---|
committer | Marvin Borner | 2018-11-07 18:02:36 +0100 |
commit | 824a2d9f587ca017fc71b84d835e72f54f9c87c4 (patch) | |
tree | 765267ea4686f752aad1f69930cfee5680cc494a /node_modules/locutus/php/array/array_merge.js | |
parent | fe75612e86b493a4e66c4e104e22658679cc014f (diff) |
Began rewrite
Diffstat (limited to 'node_modules/locutus/php/array/array_merge.js')
-rw-r--r-- | node_modules/locutus/php/array/array_merge.js | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/node_modules/locutus/php/array/array_merge.js b/node_modules/locutus/php/array/array_merge.js new file mode 100644 index 0000000..564d536 --- /dev/null +++ b/node_modules/locutus/php/array/array_merge.js @@ -0,0 +1,67 @@ +'use strict'; + +module.exports = function array_merge() { + // eslint-disable-line camelcase + // discuss at: http://locutus.io/php/array_merge/ + // original by: Brett Zamir (http://brett-zamir.me) + // bugfixed by: Nate + // bugfixed by: Brett Zamir (http://brett-zamir.me) + // input by: josh + // example 1: var $arr1 = {"color": "red", 0: 2, 1: 4} + // example 1: var $arr2 = {0: "a", 1: "b", "color": "green", "shape": "trapezoid", 2: 4} + // example 1: array_merge($arr1, $arr2) + // returns 1: {"color": "green", 0: 2, 1: 4, 2: "a", 3: "b", "shape": "trapezoid", 4: 4} + // example 2: var $arr1 = [] + // example 2: var $arr2 = {1: "data"} + // example 2: array_merge($arr1, $arr2) + // returns 2: {0: "data"} + + var args = Array.prototype.slice.call(arguments); + var argl = args.length; + var arg; + var retObj = {}; + var k = ''; + var argil = 0; + var j = 0; + var i = 0; + var ct = 0; + var toStr = Object.prototype.toString; + var retArr = true; + + for (i = 0; i < argl; i++) { + if (toStr.call(args[i]) !== '[object Array]') { + retArr = false; + break; + } + } + + if (retArr) { + retArr = []; + for (i = 0; i < argl; i++) { + retArr = retArr.concat(args[i]); + } + return retArr; + } + + for (i = 0, ct = 0; i < argl; i++) { + arg = args[i]; + if (toStr.call(arg) === '[object Array]') { + for (j = 0, argil = arg.length; j < argil; j++) { + retObj[ct++] = arg[j]; + } + } else { + for (k in arg) { + if (arg.hasOwnProperty(k)) { + if (parseInt(k, 10) + '' === k) { + retObj[ct++] = arg[k]; + } else { + retObj[k] = arg[k]; + } + } + } + } + } + + return retObj; +}; +//# sourceMappingURL=array_merge.js.map
\ No newline at end of file |