UNPKG

2.49 kBJavaScriptView Raw
1/**
2 * object extend
3 * @param {Object} objOri original object
4 * @param {Object} objTar target object
5 */
6var _fnObjExtend = function(child, parent){
7 var i,
8 toStr = Object.prototype.toString,
9 astr = "[object Array]";
10 child = child || {};
11 for (i in parent) {
12 if (parent.hasOwnProperty(i)) {
13 if (typeof parent[i] === "object") {
14 child[i] = (toStr.call(parent[i]) === astr) ? [] : {};
15 _fnObjExtend(child[i], parent[i]);
16 } else {
17 child[i] = parent[i];
18 }
19 }
20 }
21 return child;
22};
23
24module.exports = {
25 /**
26 * Setting a empty object to the undefined variable
27 * @param {Object} obj the object
28 * @param {Array} arrPro attribute to fix
29 * @return {[type]} [description]
30 */
31 fnFixEmptyPro: function(obj, arrPro){
32 arrPro.forEach(function(key){
33 var val = obj[key];
34 if (typeof val == 'undefined'){
35 obj[key] = {};
36 }
37 });
38 },
39
40 /**
41 * remove the repeat items in array
42 * @param {Array} arr
43 * @return {Array}
44 */
45 fnArrayUnique: function(arr){
46 var arrUnique = arr.filter(function(item, index){
47 if (arr.indexOf(item) === index){
48 return true;
49 }
50 });
51 return arrUnique;
52 },
53
54 /**
55 * load the grunt tasks
56 * @param {Grunt} grunt grunt object
57 * @param {Array} file paths
58 * @return {[type]} [description]
59 */
60 fnGruntPluginLoader: function(grunt, paths){
61 paths.forEach(function(path){
62 grunt.loadTasks(path + '/tasks');
63 });
64 },
65
66 /**
67 * object for in
68 * @param {object} obj
69 * @param {Function} cb callback
70 * @return {void}
71 */
72 fnObjForin: function(obj, cb){
73 for (var i in obj){
74 if (obj.hasOwnProperty(i)){
75 cb && cb(obj[i], i);
76 }
77 }
78 },
79
80 /**
81 * object for in
82 * @param {object} obj
83 * @param {Function} cb callback
84 * @return {void}
85 */
86 fnObjLength: function(obj) {
87 var index = 0;
88 for (var i in obj){
89 if (obj.hasOwnProperty(i)){
90 index++;
91 }
92 }
93 return index;
94 },
95
96 /**
97 * object extend
98 * @param {Object} objOri original object
99 * @param {Object} objTar target object
100 */
101 fnObjExtend: _fnObjExtend
102};
\No newline at end of file