Permalink
Browse files

chore: use @antv/util as utility methods.

  • Loading branch information...
simaQ committed Jun 20, 2018
1 parent de26f0c commit c619b66b1dffef5cea94cb1bbd7f3eb4d36192fa
Showing with 21 additions and 201 deletions.
  1. +2 −1 package.json
  2. +1 −1 src/chart/chart.js
  3. +1 −1 src/plugin/tooltip.js
  4. +17 −198 src/util/common.js
@@ -90,6 +90,7 @@
},
"dependencies": {
"fecha": "~2.3.1",
"hammerjs": "^2.0.8"
"hammerjs": "^2.0.8",
"@antv/util": "~1.0.4"
}
}
@@ -695,7 +695,7 @@ class Chart extends Base {
Util.each(geoms, function(geom) {
const yScale = geom.getYScale();
if (Util.indexOf(rst, yScale) === -1) {
if (rst.indexOf(yScale) === -1) {
rst.push(yScale);
}
});
@@ -145,7 +145,7 @@ class TooltipController {
const shapes = [];
Util.each(geoms, geom => {
const type = geom.get('type');
if (Util.indexOf(shapes, type) === -1) {
if (shapes.indexOf(type) === -1) {
shapes.push(type);
}
});
@@ -3,203 +3,22 @@
* @author dxq613@gmail.com
*/
const DomUtil = require('./dom');
const objectPrototype = Object.prototype;
const toString = objectPrototype.toString;
const MAX_LEVEL = 5;
let Util;
function deepMix(dst, src, level) {
level = level || 0;
for (const k in src) {
if (src.hasOwnProperty(k)) {
const value = src[k];
if (value !== null && Util.isPlainObject(value)) {
if (!Util.isPlainObject(dst[k])) {
dst[k] = {};
}
if (level < MAX_LEVEL) {
deepMix(dst[k], src[k], level + 1);
} else {
dst[k] = src[k];
}
} else if (Util.isArray(value)) {
dst[k] = [];
dst[k] = dst[k].concat(value);
} else if (value !== undefined) {
dst[k] = src[k];
}
}
}
}
function _mix(dist, obj) {
for (const k in obj) {
if (obj.hasOwnProperty(k) && k !== 'constructor' && obj[k] !== undefined) {
dist[k] = obj[k];
}
}
}
/**
* @class Util
* @singleton
* 绘图的工具类
*/
Util = {
/**
* 使第一个字母变成大写
* @param {String} s 字符串
* @return {String} 首字母大写后的字符串
*/
upperFirst(s) {
s += '';
return s.charAt(0).toUpperCase() + s.substring(1);
},
lowerFirst(s) {
s += '';
return s.charAt(0).toLowerCase() + s.substring(1);
},
/**
* 判断是否是字符串
* @param {*} value 判定的值
* @return {Boolean} 是否是字符串
*/
isString(value) {
return typeof value === 'string';
},
/**
* 判断是否数字
* @param {*} value 判定的值
* @return {Boolean} 是否数字
*/
isNumber(value) {
return typeof value === 'number';
},
/**
* 是否是布尔类型
* @param {Object} value 测试的值
* @return {Boolean} 是否布尔类型
*/
isBoolean(value) {
return typeof value === 'boolean';
},
/**
* 是否为函数
* @param {*} fn 对象
* @return {Boolean} 是否函数
*/
isFunction(fn) {
return typeof (fn) === 'function';
},
/**
* 是否数组
* @method
* @param {*} value 是否数组
* @return {Boolean} 是否数组
*/
isArray: ('isArray' in Array) ? Array.isArray : function(value) {
return toString.call(value) === '[object Array]';
},
/**
* 是否日期
* @param {*} value 对象
* @return {Boolean} 是否日期
*/
isDate(value) {
return toString.call(value) === '[object Date]';
},
isNil(o) {
return o === undefined || o === null;
},
/**
* 是否是javascript对象
* @param {Object} value The value to test
* @return {Boolean} 返回判定结果
*/
isObject: (toString.call(null) === '[object Object]') ?
function(value) {
// check ownerDocument here as well to exclude DOM nodes
return value !== null && value !== undefined && toString.call(value) === '[object Object]' && value.ownerDocument === undefined;
} : function(value) {
return toString.call(value) === '[object Object]';
},
isPlainObject(o) {
if (!Util.isObject(o)) return false;
if (Object.getPrototypeOf(o) === null) {
return true;
}
let proto = o;
while (Object.getPrototypeOf(proto) !== null) {
proto = Object.getPrototypeOf(proto);
}
return Object.getPrototypeOf(o) === proto;
},
deepMix() {
const args = new Array(arguments.length);
const length = args.length;
for (let i = 0; i < length; i++) {
args[i] = arguments[i];
}
const rst = args[0];
for (let i = 1; i < length; i++) {
const source = args[i];
deepMix(rst, source);
}
return rst;
},
/**
* 合并数据, 简单的合并,仅支持最多3个对象
* @param {Object} dist 源对象
* @param {Object} obj1 待复制对象1
* @param {Object} obj2 待复制对象2
* @param {Object} obj3 待复制对象3
* @return {Object} 将数据合并到第一个
*/
mix(dist, obj1, obj2, obj3) {
if (obj1) {
_mix(dist, obj1);
}
if (obj2) {
_mix(dist, obj2);
}
if (obj3) {
_mix(dist, obj3);
}
return dist;
},
indexOf(arr, element) {
return arr.indexOf(element);
},
/**
* 遍历数组或者对象
* @param {Object|Array} elements 数组中的元素或者对象的值
* @param {Function} func 遍历的函数 function(elememt,index){} 或者 function(value,key){}
*/
each(elements, func) {
if (!elements) {
return;
}
if (elements.length) {
for (let i = 0, len = elements.length; i < len; i++) {
const rst = func(elements[i], i);
if (rst === false) {
break;
}
}
} else {
for (const k in elements) {
if (elements.hasOwnProperty(k)) {
const rst = func(elements[k], k);
if (rst === false) {
break;
}
}
}
}
},
const Util = {
upperFirst: require('@antv/util/src/string/upperFirst'),
lowerFirst: require('@antv/util/src/string/lowerFirst'),
isString: require('@antv/util/src/type/isString'),
isNumber: require('@antv/util/src/type/isNumber'),
isBoolean: require('@antv/util/src/type/isBoolean'),
isFunction: require('@antv/util/src/type/isFunction'),
isDate: require('@antv/util/src/type/isDate'),
isArray: require('@antv/util/src/type/isArray'),
isNil: require('@antv/util/src/type/isNil'),
isObject: require('@antv/util/src/type/isObject'),
isPlainObject: require('@antv/util/src/type/isPlainObject'),
deepMix: require('@antv/util/src/deepMix'),
mix: require('@antv/util/src/mix'),
each: require('@antv/util/src/each'),
// TODO: scale 模块中使用
fixedBase(v, base) {
const str = base.toString();
const index = str.indexOf('.');
@@ -212,7 +31,7 @@ Util = {
}
return parseFloat(v.toFixed(length));
},
/**
/**
* 封装事件,便于使用上下文this,和便于解除事件时使用
* @protected
* @param {Object} obj 对象

0 comments on commit c619b66

Please sign in to comment.