UNPKG

astuteweb-utils

Version:

机敏web开发常用工具函数

168 lines (143 loc) 19.4 kB
/* * ATTENTION: The "eval" devtool has been used (maybe by default in mode: "development"). * This devtool is neither made for production nor for readable output files. * It uses "eval()" calls to create a separate source file in the browser devtools. * If you are trying to read the output file, select a different devtool (https://webpack.js.org/configuration/devtool/) * or disable the default devtool with "devtool: false". * If you are looking for production-ready output files, see mode: "production" (https://webpack.js.org/configuration/mode/). */ (function webpackUniversalModuleDefinition(root, factory) { if(typeof exports === 'object' && typeof module === 'object') module.exports = factory(); else if(typeof define === 'function' && define.amd) define([], factory); else if(typeof exports === 'object') exports["astuteUtils"] = factory(); else root["astuteUtils"] = factory(); })(self, function() { return /******/ (() => { // webpackBootstrap /******/ "use strict"; /******/ var __webpack_modules__ = ({ /***/ "./src/array/index.js": /*!****************************!*\ !*** ./src/array/index.js ***! \****************************/ /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"unique\": () => (/* binding */ unique),\n/* harmony export */ \"isArray\": () => (/* binding */ isArray),\n/* harmony export */ \"arrayFrom\": () => (/* binding */ arrayFrom),\n/* harmony export */ \"includes\": () => (/* binding */ includes)\n/* harmony export */ });\n\r\n/**\r\n * 数组去重\r\n *\r\n * @export\r\n * @param {*} arr\r\n * @return {*} \r\n */\r\nfunction unique(arr) {\r\n let result = arr.filter((item, index) => {\r\n return arr.indexOf(item) === index;\r\n })\r\n return result;\r\n}\r\n\r\n// 是否为数组\r\nconst isArray = Array.isArray;\r\n\r\n\r\n/**\r\n *类数组转为数组\r\n *\r\n * @export\r\n * @param {*} arr\r\n * @return {*} \r\n */\r\nfunction arrayFrom(arr) {\r\n const ret = [];\r\n arr.forEach(a => ret.push(a));\r\n return ret;\r\n}\r\n\r\n\r\n/**\r\n *数组是否包含某个值\r\n *\r\n * @export\r\n * @param {*} arr\r\n * @param {*} item\r\n * @return {*} \r\n */\r\nfunction includes(arr, item) {\r\n return !!~arr.indexOf(item);\r\n}\n\n//# sourceURL=webpack://astuteUtils/./src/array/index.js?"); /***/ }), /***/ "./src/boolean/index.js": /*!******************************!*\ !*** ./src/boolean/index.js ***! \******************************/ /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"isBoolean\": () => (/* binding */ isBoolean)\n/* harmony export */ });\n\r\n/**\r\n *布尔校验\r\n *\r\n * @export\r\n * @param {*} val\r\n * @return {*} \r\n */\r\nfunction isBoolean(val) {\r\n return typeof val === 'boolean';\r\n}\n\n//# sourceURL=webpack://astuteUtils/./src/boolean/index.js?"); /***/ }), /***/ "./src/function/index.js": /*!*******************************!*\ !*** ./src/function/index.js ***! \*******************************/ /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"debounce\": () => (/* binding */ debounce),\n/* harmony export */ \"throttle\": () => (/* binding */ throttle),\n/* harmony export */ \"warn\": () => (/* binding */ warn),\n/* harmony export */ \"err\": () => (/* binding */ err),\n/* harmony export */ \"isFunction\": () => (/* binding */ isFunction)\n/* harmony export */ });\n\r\n/**\r\n * 防抖\r\n * 只在最后一次点击过指定时间才执行\r\n * @export\r\n * @param {*} fn\r\n * @param {*} wait\r\n * @return {*} \r\n */\r\nfunction debounce(fn, wait) {\r\n let timeout = null;\r\n return function() {\r\n if(timeout !== null) clearTimeout(timeout);\r\n timeout = setTimeout(fn, wait);\r\n }\r\n}\r\n\r\n\r\n\r\n/**\r\n * 节流\r\n * 高频触发事件时,第一次立即执行,再次触发事件时,如果定时器存在就不执行,直到延迟时间后定时器执行函数并清空定时器,这样便可设置下一个定时器。\r\n * @export\r\n * @param {*} fn\r\n * @param {*} wait\r\n * @return {*} \r\n */\r\nfunction throttle(fn, wait) {\r\n let timer = null;\r\n return function() {\r\n if(!timer) {\r\n timer = setTimeout(() => {\r\n fn.apply(this, arguments);\r\n timer = null;\r\n }, wait)\r\n }\r\n }\r\n}\r\n\r\n\r\n/**\r\n *输出警告\r\n *\r\n * @export\r\n * @param {*} msg\r\n * @param {*} err\r\n */\r\nfunction warn(msg, err) {\r\n if(typeof console != 'undefined') {\r\n console.warn('warn:' + msg);\r\n if(err) {\r\n console.warn(err.stack);\r\n }\r\n }\r\n}\r\n\r\n\r\n/**\r\n *输出错误\r\n *\r\n * @export\r\n * @param {*} msg\r\n * @param {*} err\r\n */\r\nfunction err(msg, err) {\r\n if(typeof console != 'undefined') {\r\n console.error('error:', msg);\r\n if(err) {\r\n console.err(err.stack);\r\n }\r\n }\r\n}\r\n\r\n\r\n/**\r\n *校验是否为函数\r\n *\r\n * @export\r\n * @param {*} val\r\n * @return {*} \r\n */\r\nfunction isFunction(val) {\r\n return typeof val === 'function';\r\n}\r\n\r\n\n\n//# sourceURL=webpack://astuteUtils/./src/function/index.js?"); /***/ }), /***/ "./src/index.js": /*!**********************!*\ !*** ./src/index.js ***! \**********************/ /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"shallowClone\": () => (/* reexport safe */ _object_index_js__WEBPACK_IMPORTED_MODULE_0__.shallowClone),\n/* harmony export */ \"deepClone\": () => (/* reexport safe */ _object_index_js__WEBPACK_IMPORTED_MODULE_0__.deepClone),\n/* harmony export */ \"isObject\": () => (/* reexport safe */ _object_index_js__WEBPACK_IMPORTED_MODULE_0__.isObject),\n/* harmony export */ \"isPlainObject\": () => (/* reexport safe */ _object_index_js__WEBPACK_IMPORTED_MODULE_0__.isPlainObject),\n/* harmony export */ \"parseArgs\": () => (/* reexport safe */ _object_index_js__WEBPACK_IMPORTED_MODULE_0__.parseArgs),\n/* harmony export */ \"hasOwn\": () => (/* reexport safe */ _object_index_js__WEBPACK_IMPORTED_MODULE_0__.hasOwn),\n/* harmony export */ \"merge\": () => (/* reexport safe */ _object_index_js__WEBPACK_IMPORTED_MODULE_0__.merge),\n/* harmony export */ \"unique\": () => (/* reexport safe */ _array_index_js__WEBPACK_IMPORTED_MODULE_1__.unique),\n/* harmony export */ \"isArray\": () => (/* reexport safe */ _array_index_js__WEBPACK_IMPORTED_MODULE_1__.isArray),\n/* harmony export */ \"arrayFrom\": () => (/* reexport safe */ _array_index_js__WEBPACK_IMPORTED_MODULE_1__.arrayFrom),\n/* harmony export */ \"includes\": () => (/* reexport safe */ _array_index_js__WEBPACK_IMPORTED_MODULE_1__.includes),\n/* harmony export */ \"debounce\": () => (/* reexport safe */ _function_index_js__WEBPACK_IMPORTED_MODULE_2__.debounce),\n/* harmony export */ \"throttle\": () => (/* reexport safe */ _function_index_js__WEBPACK_IMPORTED_MODULE_2__.throttle),\n/* harmony export */ \"warn\": () => (/* reexport safe */ _function_index_js__WEBPACK_IMPORTED_MODULE_2__.warn),\n/* harmony export */ \"err\": () => (/* reexport safe */ _function_index_js__WEBPACK_IMPORTED_MODULE_2__.err),\n/* harmony export */ \"isFunction\": () => (/* reexport safe */ _function_index_js__WEBPACK_IMPORTED_MODULE_2__.isFunction),\n/* harmony export */ \"isBoolean\": () => (/* reexport safe */ _boolean_index_js__WEBPACK_IMPORTED_MODULE_3__.isBoolean),\n/* harmony export */ \"isNull\": () => (/* reexport safe */ _null_index_js__WEBPACK_IMPORTED_MODULE_4__.isNull),\n/* harmony export */ \"isString\": () => (/* reexport safe */ _string_index_js__WEBPACK_IMPORTED_MODULE_5__.isString),\n/* harmony export */ \"escapeParams\": () => (/* reexport safe */ _xss_index_js__WEBPACK_IMPORTED_MODULE_6__.escapeParams)\n/* harmony export */ });\n/* harmony import */ var _object_index_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./object/index.js */ \"./src/object/index.js\");\n/* harmony import */ var _array_index_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./array/index.js */ \"./src/array/index.js\");\n/* harmony import */ var _function_index_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./function/index.js */ \"./src/function/index.js\");\n/* harmony import */ var _boolean_index_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./boolean/index.js */ \"./src/boolean/index.js\");\n/* harmony import */ var _null_index_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./null/index.js */ \"./src/null/index.js\");\n/* harmony import */ var _string_index_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ./string/index.js */ \"./src/string/index.js\");\n/* harmony import */ var _xss_index_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! ./xss/index.js */ \"./src/xss/index.js\");\n// 对象相关\r\n\r\n\r\n\r\n\r\n\r\n \r\n\r\n// 数组相关\r\n\r\n\r\n\r\n\r\n// 函数\r\n\r\n\r\n\r\n\r\n\r\n// 布尔\r\n\r\n// null\r\n\r\n// 字符串\r\n\r\n// xss攻击\r\n\r\n\r\n\n\n//# sourceURL=webpack://astuteUtils/./src/index.js?"); /***/ }), /***/ "./src/null/index.js": /*!***************************!*\ !*** ./src/null/index.js ***! \***************************/ /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"isNull\": () => (/* binding */ isNull)\n/* harmony export */ });\n\r\n/**\r\n *\r\n * @export\r\n * @param {*} val\r\n * @return {*} \r\n */\r\nfunction isNull(val) {\r\n return val === null || val === undefined;\r\n}\n\n//# sourceURL=webpack://astuteUtils/./src/null/index.js?"); /***/ }), /***/ "./src/object/index.js": /*!*****************************!*\ !*** ./src/object/index.js ***! \*****************************/ /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"shallowClone\": () => (/* binding */ shallowClone),\n/* harmony export */ \"deepClone\": () => (/* binding */ deepClone),\n/* harmony export */ \"isObject\": () => (/* binding */ isObject),\n/* harmony export */ \"isPlainObject\": () => (/* binding */ isPlainObject),\n/* harmony export */ \"parseArgs\": () => (/* binding */ parseArgs),\n/* harmony export */ \"hasOwn\": () => (/* binding */ hasOwn),\n/* harmony export */ \"merge\": () => (/* binding */ merge)\n/* harmony export */ });\n\r\n/**\r\n * 浅克隆\r\n *\r\n * @export\r\n * @param {*} origin\r\n * @param {*} target\r\n * @return {*} target\r\n */\r\nfunction shallowClone(origin, target) {\r\n for(let prop in origin) {\r\n if(origin.hasOwnProperty(prop)) {\r\n target[prop] = origin[prop];\r\n }\r\n }\r\n return target;\r\n}\r\n\r\n\r\n/**\r\n * 深克隆\r\n *\r\n * @export\r\n * @param {*} origin\r\n * @param {*} target\r\n * @return {*} target\r\n */\r\nfunction deepClone(origin, target) {\r\n let tar = target || {};\r\n const toStr = Object.prototype.toString();\r\n const arrStr = '[object Array]';\r\n for(let prop in origin) {\r\n if(origin.hasOwnProperty(prop)) {\r\n if(origin[prop] !== null && typeof(origin[prop] == 'object')) {\r\n tar[prop] = toStr.call(origin[prop]) == arrStr ? [] : {};\r\n deepClone(origin[prop], tar[prop]);\r\n } else {\r\n tar[prop] = origin[prop];\r\n }\r\n }\r\n }\r\n\r\n return tar;\r\n}\r\n\r\n\r\n/**\r\n *检验是否为对象\r\n *\r\n * @export\r\n * @param {*} obj\r\n * @return {*} \r\n */\r\nfunction isObject(obj) {\r\n return obj !== null && typeof obj === 'object';\r\n}\r\n\r\n/**\r\n *检验是否为对象\r\n *\r\n * @export\r\n * @param {*} obj\r\n * @return {*} \r\n */\r\nconst toString = Object.prototype.toString;\r\nconst OBJECT_STRING = '[object Object]';\r\nfunction isPlainObject(obj) {\r\n return toString.call(obj) === OBJECT_STRING;\r\n}\r\n\r\nfunction parseArgs(...args) {\r\n let locale = null;\r\n let params = null;\r\n if(args.length === 1) {\r\n if(isObject(args[0] || Array.isArray(args[0]))) {\r\n params = args[0];\r\n } else if(typeof args[0] === 'string') {\r\n locale = args[0];\r\n }\r\n } else if(args.length === 2) {\r\n if(typeof args[0] === 'string') {\r\n locale = args[0];\r\n }\r\n if(isObject(args[1] || Array.isArray(args[1]))) {\r\n params = args[1];\r\n }\r\n }\r\n return {\r\n locale,\r\n params\r\n }\r\n}\r\n\r\n/**\r\n * 对象是否某含某个非集成属性\r\n *\r\n * @export\r\n * @param {*} obj\r\n * @param {*} key\r\n * @return {*} \r\n */\r\nconst hasOwnProperty = Object.prototype.hasOwnProperty;\r\nfunction hasOwn(obj, key) {\r\n return hasOwnProperty.call(obj, key);\r\n}\r\n\r\n\r\n/**\r\n *合并对象\r\n *\r\n * @export\r\n * @param {*} target\r\n * @return {*} \r\n */\r\nfunction merge(target) {\r\n const output = Object(target);\r\n for(let i = 1; i < arguments.length; i++) {\r\n const source = arguments[i];\r\n if(source !== undefined && source !== null) {\r\n let key;\r\n for(key in source) {\r\n if(hasOwn(source, key)) {\r\n if(isObject(source(key))) {\r\n output[key] = merge(output[key], source[key]);\r\n } else {\r\n output[key] = source[key];\r\n }\r\n }\r\n }\r\n }\r\n }\r\n\r\n return output;\r\n}\n\n//# sourceURL=webpack://astuteUtils/./src/object/index.js?"); /***/ }), /***/ "./src/string/index.js": /*!*****************************!*\ !*** ./src/string/index.js ***! \*****************************/ /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"isString\": () => (/* binding */ isString)\n/* harmony export */ });\n\r\n/**\r\n *检验是否为字符串类型\r\n *\r\n * @export\r\n * @param {*} val\r\n * @return {*} \r\n */\r\nfunction isString(val) {\r\n return typeof val === 'string';\r\n}\n\n//# sourceURL=webpack://astuteUtils/./src/string/index.js?"); /***/ }), /***/ "./src/xss/index.js": /*!**************************!*\ !*** ./src/xss/index.js ***! \**************************/ /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"escapeParams\": () => (/* binding */ escapeParams)\n/* harmony export */ });\n\r\n/**\r\n * Sanitizes html special characters from input strings. For mitigating risk of XSS attacks.\r\n *\r\n * @param {*} rawText The raw input from the user that should be escaped.\r\n * @return {*} \r\n */\r\nfunction escapeHtml(rawText) {\r\n return rawText\r\n .replace(/</g, '&lt;')\r\n .replace(/>/g, '&gt;')\r\n .replace(/\"/g, '&quot;')\r\n .replace(/'/g, '&apos;')\r\n}\r\n\r\n\r\n/**\r\n * Escapes html tags and special symbols from all provided params which were returned from parseArgs().params.\r\n * This method performs an in-place operation on the params object.\r\n * @export\r\n * @param {*} params params Parameters as provided from `parseArgs().params`. May be either an array of strings or a string->any map.\r\n * @return {*} The manipulated `params` object.\r\n */\r\nfunction escapeParams(params) {\r\n if(params != null) {\r\n Object.keys(params).forEach(key => {\r\n if(typeof(params[key]) === 'string') {\r\n params[key] = escapeHtml(params[key]);\r\n }\r\n })\r\n }\r\n return params;\r\n}\n\n//# sourceURL=webpack://astuteUtils/./src/xss/index.js?"); /***/ }) /******/ }); /************************************************************************/ /******/ // The module cache /******/ var __webpack_module_cache__ = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ // Check if module is in cache /******/ var cachedModule = __webpack_module_cache__[moduleId]; /******/ if (cachedModule !== undefined) { /******/ return cachedModule.exports; /******/ } /******/ // Create a new module (and put it into the cache) /******/ var module = __webpack_module_cache__[moduleId] = { /******/ // no module.id needed /******/ // no module.loaded needed /******/ exports: {} /******/ }; /******/ /******/ // Execute the module function /******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__); /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /************************************************************************/ /******/ /* webpack/runtime/define property getters */ /******/ (() => { /******/ // define getter functions for harmony exports /******/ __webpack_require__.d = (exports, definition) => { /******/ for(var key in definition) { /******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) { /******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] }); /******/ } /******/ } /******/ }; /******/ })(); /******/ /******/ /* webpack/runtime/hasOwnProperty shorthand */ /******/ (() => { /******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop)) /******/ })(); /******/ /******/ /* webpack/runtime/make namespace object */ /******/ (() => { /******/ // define __esModule on exports /******/ __webpack_require__.r = (exports) => { /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); /******/ } /******/ Object.defineProperty(exports, '__esModule', { value: true }); /******/ }; /******/ })(); /******/ /************************************************************************/ /******/ /******/ // startup /******/ // Load entry module and return exports /******/ // This entry module can't be inlined because the eval devtool is used. /******/ var __webpack_exports__ = __webpack_require__("./src/index.js"); /******/ /******/ return __webpack_exports__; /******/ })() ; });