UNPKG

dtable-utils

Version:

dtable common utils

50 lines (45 loc) 1.42 kB
import _typeof from '@babel/runtime/helpers/typeof'; /** * Generate a random string of specified length. * @param {number} keyLength * @returns random code, string */ var generatorBase64Code = function generatorBase64Code() { var keyLength = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 4; var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz0123456789'; var key = ''; for (var i = 0; i < keyLength; i++) { key += possible.charAt(Math.floor(Math.random() * possible.length)); } return key; }; /** * Check whether the given value is empty * @param {any} val * @returns bool */ var isEmpty = function isEmpty(val) { if (val === null || val === undefined) return true; if (val.length !== undefined) return val.length === 0; if (val instanceof Date) return false; if (_typeof(val) === 'object') return Object.keys(val).length === 0; return false; }; /** * Check whether the object is empty. * The true will be returned if the "obj" is invalid. * @param {object} obj * @returns bool */ var isEmptyObject = function isEmptyObject(obj) { var name; // eslint-disable-next-line for (name in obj) { return false; } return true; }; var hasOwnProperty = function hasOwnProperty(obj, key) { return Object.prototype.hasOwnProperty.call(obj, key); }; export { generatorBase64Code, hasOwnProperty, isEmpty, isEmptyObject };