浏览代码

持续更新中······

striveDJiang 8 年之前
父节点
当前提交
e1b4982b4b
共有 7 个文件被更改,包括 145 次插入3 次删除
  1. 6 0
      README.md
  2. 40 0
      array/arrayFlattening.js
  3. 40 0
      array/getCount.js
  4. 1 0
      class/removeClass.js
  5. 38 0
      object/dataType.js
  6. 7 1
      random/randomColor.js
  7. 13 2
      random/randomNum.js

+ 6 - 0
README.md

@@ -9,6 +9,8 @@
 ####   [arraySum][arraySum]  数组求和
 ####   [arrayRandom][arrayRandom]  从数组中随机获取元素
 ####   [getEleCount][getEleCount]  返回数组或字符串中一个元素出现的次数
+####   [getCount][getCount]  返回数组(或字符串)中出现最多的元素和出现次数
+####   [arrayFlattening][arrayFlattening]  数组扁平化
 
 ### Class
 ####   [addClass][addClass]  为元素添加class  
@@ -40,6 +42,7 @@
 ### Object  
 ####   [deepClone][deepClone]  深拷贝,支持常见类型
 ####   [isEmptyObject][isEmptyObject]  判断Object是否为空
+####   [dataType][dataType]  判断Object是否为空
 
 ### Random  
 ####   [randomColor][randomColor]   随机生成颜色
@@ -79,6 +82,8 @@
 [arraySum]:https://github.com/striveDJiang/utils/blob/master/array/arraySum.js
 [arrayRandom]:https://github.com/striveDJiang/utils/blob/master/array/arrayRandom.js
 [getEleCount]:https://github.com/striveDJiang/utils/blob/master/array/getEleCount.js
+[getCount]:https://github.com/striveDJiang/utils/blob/master/array/getCount.js
+[arrayFlattening]:https://github.com/striveDJiang/utils/blob/master/array/arrayFlattening.js
 
 [addClass]:https://github.com/striveDJiang/utils/blob/master/class/addClass.js
 [hasClass]:https://github.com/striveDJiang/utils/blob/master/class/hasClass.js
@@ -103,6 +108,7 @@
 
 [deepClone]:https://github.com/striveDJiang/utils/blob/master/object/deepClone.js
 [isEmptyObject]:https://github.com/striveDJiang/utils/blob/master/object/isEmptyObject.js
+[dataType]:https://github.com/striveDJiang/utils/blob/master/object/dataType.js
 
 [randomColor]:https://github.com/striveDJiang/utils/blob/master/random/randomColor.js
 [randomNum]:https://github.com/striveDJiang/utils/blob/master/random/randomNum.js

+ 40 - 0
array/arrayFlattening.js

@@ -0,0 +1,40 @@
+/**
+ * 
+ * @desc 数组扁平化
+ * @param {Array} arr
+ * @return {Array}
+ */
+// 第一种方法:
+function arrayFlattening(arr) {
+    var newArr = [],
+        _this = this;
+    for (var i = 0; i < arr.length; i++) {
+        if (Array.isArray(arr[i])) {
+            // 如果是数组,调用(递归)arrayFlattening 将其扁平化
+            // 然后再 push 到 newArr 中
+            newArr.push.apply(newArr, _this.arrayFlattening(arr[i]));
+        } else {
+            // 不是数组直接 push 到 newArr 中
+            newArr.push(arr[i]);
+        }
+    }
+    return newArr;
+};
+
+// 第二种方法:
+// function arrayFlattening(arr) {
+//     var newArr = [];
+//     for (var i = 0; i < arr.length; i++) {
+//         if (Array.isArray(arr[i])) {
+//             // 如果是数组,调用(递归)arrayFlattening 将其扁平化
+//             // 然后再 push 到 newArr 中
+//             newArr = newArr.concat(arrayFlattening(arr[i]));
+//         } else {
+//             // 不是数组直接 push 到 newArr 中
+//             newArr.push(arr[i]);
+//         }
+//     }
+//     return newArr;
+// };
+
+module.exports = arrayFlattening;

+ 40 - 0
array/getCount.js

@@ -0,0 +1,40 @@
+/**
+ * 
+ * @desc 返回数组(或字符串)中出现最多的元素和出现次数
+ * @param {Array} arr
+ * @param {Number} rank:长度,默认为数组长度
+ * @param {Number} ranktype:排序方式,默认降序;如果ranktype为1,则为升序
+ * @return {Array[Object]} el:元素,count:次数
+ */
+function getCount(arr, rank, ranktype) {
+    var obj = {},
+        k, arr1 = [];
+    //记录每一元素出现的次数
+    for (var i = 0, len = arr.length; i < len; i++) {
+        k = arr[i];
+        if (obj[k]) {
+            obj[k]++;
+        } else {
+            obj[k] = 1;
+        }
+    }
+    //保存结果{el:'元素',count:出现次数}
+    for (var o in obj) {
+        arr1.push({
+            el: o,
+            count: obj[o]
+        });
+    }
+    //排序(降序)
+    arr1.sort(function (n1, n2) {
+        return n2.count - n1.count
+    });
+    //如果ranktype为1,则为升序,反转数组
+    if (ranktype === 1) {
+        arr1 = arr1.reverse();
+    }
+    var rank1 = rank || arr1.length;
+    return arr1.slice(0, rank1);
+};
+
+module.exports = getCount;

+ 1 - 0
class/removeClass.js

@@ -13,4 +13,5 @@ function removeClass(ele, cls) {
         ele.className = ele.className.replace(reg, ' ');
     }
 }
+
 module.exports = removeClass;

+ 38 - 0
object/dataType.js

@@ -0,0 +1,38 @@
+/**
+ * 
+ * @desc   数据类型判断
+ * @param  {Object} o
+ * @param  {Object} obj
+ * @return {Boolean}
+ */
+function dataType(o, type) {
+    if (type) {
+        var _type = type.toLowerCase();
+    }
+    switch (_type) {
+        case 'string':
+            return Object.prototype.toString.call(o) === '[object String]';
+        case 'number':
+            return Object.prototype.toString.call(o) === '[object Number]';
+        case 'boolean':
+            return Object.prototype.toString.call(o) === '[object Boolean]';
+        case 'undefined':
+            return Object.prototype.toString.call(o) === '[object Undefined]';
+        case 'null':
+            return Object.prototype.toString.call(o) === '[object Null]';
+        case 'function':
+            return Object.prototype.toString.call(o) === '[object Function]';
+        case 'array':
+            return Object.prototype.toString.call(o) === '[object Array]';
+        case 'object':
+            return Object.prototype.toString.call(o) === '[object Object]';
+        case 'nan':
+            return isNaN(o);
+        case 'elements':
+            return Object.prototype.toString.call(o).indexOf('HTML') !== -1
+        default:
+            return Object.prototype.toString.call(o)
+    }
+};
+
+module.exports = dataType

+ 7 - 1
random/randomColor.js

@@ -3,8 +3,14 @@
  * @desc 随机生成颜色
  * @return {String} 
  */
+// 第一种方法:
 function randomColor() {
     return '#' + ('00000' + (Math.random() * 0x1000000 << 0).toString(16)).slice(-6);
-}
+};
+
+// 第二种方法:
+// function randomColor() {
+//     return '#' + Math.random().toString(16).substring(2).substr(0, 6);
+// };
 
 module.exports = randomColor;

+ 13 - 2
random/randomNum.js

@@ -6,7 +6,18 @@
  * @return {Number} 
  */
 function randomNum(min, max) {
-    return Math.floor(min + Math.random() * (max - min));
-}
+    // 如果传入两个参数,返回min到max范围的随机数,包括min,max
+    if (arguments.length === 2) {
+        return Math.round(min + Math.random() * (max - min));
+    }
+    // 如果只传入一个参数,返回0到min范围的随机数,包括0,min
+    else if (arguments.length === 1) {
+        return Math.round(Math.random() * min)
+    }
+    // 如果不传入参数,返回0到255范围的随机数,包括0,255
+    else {
+        return Math.round(Math.random() * 255)
+    }
+};
 
 module.exports = randomNum;