UNPKG

zen-utils

Version:

85 lines (79 loc) 1.52 kB
'use strict'; function queryDataByKey(obj, condition, match) { const result = []; if (obj == null) { return result; } if (!condition) { return module.exports.values(obj); } Object.keys(obj).forEach(key => { if (match && match.match && match.match(key, condition)) { result.push(obj[key]); } else if (key === condition) { result.push(obj[key]); } }); return result; } /** * Created by liguoxin on 2017/3/6. * */ function values(obj) { if (!obj) { return []; } return Object.keys(obj).map(key => { return obj[key]; }); } function isError(obj) { return _isType(obj, 'Error'); } function isArray(obj) { return _isType(obj, 'Array'); } function isFunction(obj) { return _isType(obj, 'Function'); } function isDate(obj) { return _isType(obj, 'Date'); } function isObject(obj) { if (!obj) { return false; } return typeof obj === 'object'; } function isNumber(obj) { return _isType(obj, 'Number'); } function isBoolean(obj) { return _isType(obj, 'Boolean'); } function isRegExp(obj) { return _isType(obj, 'RegExp'); } function isString(obj) { return _isType(obj, 'String'); } function isAsyncFunction(obj) { return _isType(obj, 'AsyncFunction'); } function _isType(obj, type) { return Object.prototype.toString.call(obj) === `[object ${type}]`; } module.exports = { queryDataByKey, values, isArray, isFunction, isError, isDate, isObject, isNumber, isBoolean, isRegExp, isString, isAsyncFunction };