rx-util

rxliuli 在浏览器上使用的 js 工具集

目前为 2020-08-30 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/382120/842818/rx-util.js

  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  3. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  4. (global = global || self, factory(global.rx = {}));
  5. }(this, (function (exports) { 'use strict';
  6.  
  7. /**
  8. * 在浏览器上下载二进制资源
  9. * @param blob 要下载的二进制资源
  10. * @param filename 文件名
  11. */
  12. function download(blob, filename = 'unknown') {
  13. // 创建隐藏的可下载链接
  14. const eleLink = document.createElement('a');
  15. eleLink.download = filename;
  16. eleLink.style.display = 'none';
  17. // 为 link 赋值
  18. eleLink.href = URL.createObjectURL(blob);
  19. // 触发点击
  20. document.body.appendChild(eleLink);
  21. eleLink.click();
  22. // 然后移除
  23. document.body.removeChild(eleLink);
  24. }
  25.  
  26. /*! *****************************************************************************
  27. Copyright (c) Microsoft Corporation. All rights reserved.
  28. Licensed under the Apache License, Version 2.0 (the "License"); you may not use
  29. this file except in compliance with the License. You may obtain a copy of the
  30. License at http://www.apache.org/licenses/LICENSE-2.0
  31.  
  32. THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  33. KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  34. WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  35. MERCHANTABLITY OR NON-INFRINGEMENT.
  36.  
  37. See the Apache Version 2.0 License for specific language governing permissions
  38. and limitations under the License.
  39. ***************************************************************************** */
  40.  
  41. function __awaiter(thisArg, _arguments, P, generator) {
  42. return new (P || (P = Promise))(function (resolve, reject) {
  43. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  44. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  45. function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
  46. step((generator = generator.apply(thisArg, _arguments || [])).next());
  47. });
  48. }
  49.  
  50. /**
  51. * 将字符串转化为 Blob 类型
  52. * @param str 字符串
  53. * @returns Blob 数据
  54. */
  55. function strToBlob(str) {
  56. return new Blob([str], {
  57. type: 'text/plain',
  58. });
  59. }
  60.  
  61. /**
  62. * 在浏览器上下载文本内容
  63. * @param str 字符串内容
  64. * @param filename 下载文件名,没有则默认为链接中的文件名
  65. */
  66. function downloadString(str, filename = 'unknown.txt') {
  67. return __awaiter(this, void 0, void 0, function* () {
  68. download(strToBlob(str), filename);
  69. });
  70. }
  71.  
  72. /**
  73. * 根据 url 下载二进制资源
  74. * @param url 下载请求信息
  75. * @param filename 下载文件名,没有则默认为链接中的文件名
  76. */
  77. function downloadUrl(url, filename = url.substr(url.lastIndexOf('/'))) {
  78. return __awaiter(this, void 0, void 0, function* () {
  79. // 创建隐藏的可下载链接
  80. const eleLink = document.createElement('a');
  81. eleLink.download = filename;
  82. eleLink.style.display = 'none';
  83. // 为 link 赋值
  84. eleLink.href = url;
  85. // 触发点击
  86. document.body.appendChild(eleLink);
  87. eleLink.click();
  88. // 然后移除
  89. document.body.removeChild(eleLink);
  90. });
  91. }
  92.  
  93. /**
  94. * 获取 cookie 键值映射 Map
  95. * @returns cookie 键值映射 Map
  96. */
  97. function getCookies() {
  98. return document.cookie.split(';').reduce((res, str) => {
  99. const [k, v] = str.split('=');
  100. res.set(k, v);
  101. return res;
  102. }, new Map());
  103. }
  104.  
  105. /**
  106. * 将 url 中的内容加载到元素上
  107. * 注:domSelector 必须有 src 属性用以将加载完成的资源赋值给其,加载默认是异步的
  108. * @param url url 资源
  109. * @param dom dom 元素
  110. * @param init 初始化参数, 实为 fetch() 的参数以及一些自定义的参数,默认 {}
  111. * 关于 fetch 具体可以参考 <https://developer.mozilla.org/zh-CN/docs/Web/API/Fetch_API/Using_Fetch>
  112. */
  113. function loadResource(url, dom, init = {}) {
  114. return __awaiter(this, void 0, void 0, function* () {
  115. const res = yield fetch(url, init);
  116. const blob = yield res.blob();
  117. // 生成一个本地的 url 并赋值给 src 属性
  118. dom.src = window.URL.createObjectURL(blob);
  119. });
  120. }
  121.  
  122. /**
  123. * 协议与默认端口映射表
  124. */
  125. const protocolPortMap = new Map()
  126. .set('http', 80)
  127. .set('https', 443)
  128. .set('ssh', 22)
  129. .set('ftp', 21);
  130. /**
  131. * 解析 url 字符串
  132. * @param url url 字符串,不能为空
  133. * @returns url 对象
  134. * @deprecated 请使用原生 API URL 类,可以通过 new URL(url) 将 URL 字符串转换为 URL 对象,并获取指定的信息
  135. */
  136. function parseUrl(url) {
  137. if (!url) {
  138. throw new Error('Url cannot be empty');
  139. }
  140. const regexp = new RegExp('^((\\w+)://([\\w\\.]*)(:(\\d+))?)(.*)');
  141. const temps = regexp.exec(url);
  142. if (temps === null) {
  143. return null;
  144. }
  145. const website = temps[1];
  146. const protocol = temps[2];
  147. const domain = temps[3];
  148. const portStr = temps[5];
  149. const href = temps[6];
  150. // 截取域名之后的内容
  151. const temp = url.substr(website.length);
  152. const markIndex = temp.indexOf('?');
  153. // 如果没有携带参数则直接返回
  154. if (markIndex === -1) {
  155. const accessPath = temp;
  156. return {
  157. url,
  158. website,
  159. protocol,
  160. domain,
  161. // tslint:disable-next-line:radix
  162. port: parseInt(portStr),
  163. href,
  164. accessPath,
  165. params: new Map(),
  166. };
  167. }
  168. let accessPath = temp.substr(0, markIndex);
  169. if (accessPath.endsWith('/')) {
  170. accessPath = accessPath.substring(0, accessPath.length - 1);
  171. }
  172. const port = portStr || protocolPortMap.get(protocol) || 0;
  173. // 解析参数列表
  174. const params = temp
  175. .substr(markIndex + 1)
  176. .split('&')
  177. .map(str => str.split('='))
  178. .filter(arr => arr[0] !== '')
  179. .reduce((params, arr) => {
  180. const k = decodeURIComponent(arr[0]);
  181. const v = decodeURIComponent(arr.length === 1 ? '' : arr[1]);
  182. // 如果已经存在了就认为是数组参数
  183. const vs = params.get(k);
  184. if (vs === undefined) {
  185. params.set(k, v);
  186. }
  187. else {
  188. if (!Array.isArray(vs)) {
  189. params.set(k, [vs]);
  190. }
  191. if (params.get(k).length !== undefined) {
  192. params.get(k).push(v);
  193. }
  194. }
  195. return params;
  196. }, new Map());
  197. return {
  198. url,
  199. website,
  200. protocol,
  201. domain,
  202. port,
  203. href,
  204. accessPath,
  205. params,
  206. };
  207. }
  208.  
  209. /**
  210. * 读取文件类型
  211. */
  212. var ReadType;
  213. (function (ReadType) {
  214. /**
  215. * 以 data url 读取
  216. */
  217. ReadType["DataURL"] = "readAsDataURL";
  218. /**
  219. * 以文本读取
  220. */
  221. ReadType["Text"] = "readAsText";
  222. /**
  223. * 以二进制文件读取
  224. */
  225. ReadType["BinaryString"] = "readAsBinaryString";
  226. /**
  227. * 以 ArrayBuffer 读取
  228. */
  229. ReadType["ArrayBuffer"] = "readAsArrayBuffer";
  230. })(ReadType || (ReadType = {}));
  231. /**
  232. * 读取本地浏览器选择的文件
  233. * @param file 选择的文件
  234. * @param options 读取的选项
  235. * @returns 返回了读取到的内容(异步)
  236. */
  237. function _readLocal(file, options = {}) {
  238. const { type, encoding } = Object.assign({
  239. type: ReadType.DataURL,
  240. encoding: 'UTF-8',
  241. }, options);
  242. return new Promise((resolve, reject) => {
  243. if (!file) {
  244. reject(new Error('file not exists'));
  245. }
  246. const fr = new FileReader();
  247. fr.onload = () => {
  248. resolve(fr.result);
  249. };
  250. fr.onerror = error => {
  251. reject(error);
  252. };
  253. switch (type) {
  254. case ReadType.DataURL:
  255. fr.readAsDataURL(file);
  256. break;
  257. case ReadType.Text:
  258. fr.readAsText(file, encoding);
  259. break;
  260. case ReadType.BinaryString:
  261. fr.readAsBinaryString(file);
  262. break;
  263. case ReadType.ArrayBuffer:
  264. fr.readAsArrayBuffer(file);
  265. break;
  266. }
  267. });
  268. }
  269. const readLocal = Object.assign(_readLocal, {
  270. ReadType,
  271. /**
  272. * 以 data url 读取
  273. * @deprecated 已废弃,请使用枚举类 ReadType
  274. */
  275. DataURL: ReadType.DataURL,
  276. /**
  277. * 以文本读取
  278. * @deprecated 已废弃,请使用枚举类 ReadType
  279. */
  280. Text: ReadType.Text,
  281. /**
  282. * 以二进制文件读取
  283. * @deprecated 已废弃,请使用枚举类 ReadType
  284. */
  285. BinaryString: ReadType.BinaryString,
  286. /**
  287. * 以 ArrayBuffer 读取
  288. * @deprecated 已废弃,请使用枚举类 ReadType
  289. */
  290. ArrayBuffer: ReadType.ArrayBuffer,
  291. });
  292.  
  293. /**
  294. * 为 js 中的 Date 对象原型添加 format 格式化方法
  295. * @param date 要进行格式化的日期
  296. * @param fmt 日期的格式,格式 {@code '[Y+|y+][M+][D+|d+][H+|h+][m+][s+][S+][q+]'}
  297. * @returns 格式化得到的结果
  298. */
  299. function dateFormat(date, fmt) {
  300. const timeFormatDefaults = {
  301. 'Y+|y+': date.getFullYear(),
  302. 'M+': date.getMonth() + 1,
  303. 'D+|d+': date.getDate(),
  304. 'H+|h+': date.getHours(),
  305. 'm+': date.getMinutes(),
  306. 's+': date.getSeconds(),
  307. 'q+': Math.floor((date.getMonth() + 3) / 3),
  308. 'S+': date.getMilliseconds(),
  309. };
  310. for (const k in timeFormatDefaults) {
  311. if (!new RegExp('(' + k + ')').test(fmt)) {
  312. continue;
  313. }
  314. if (k === 'Y+|y+') {
  315. fmt = fmt.replace(RegExp.$1, ('' + timeFormatDefaults[k]).substr(4 - RegExp.$1.length));
  316. }
  317. else if (k === 'S+') {
  318. let lens = RegExp.$1.length;
  319. lens = lens === 1 ? 3 : lens;
  320. fmt = fmt.replace(RegExp.$1, ('00' + timeFormatDefaults[k]).substr(('' + timeFormatDefaults[k]).length - 1, lens));
  321. }
  322. else {
  323. const v = Reflect.get(timeFormatDefaults, k);
  324. fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? v : ('00' + v).substr(('' + v).length));
  325. }
  326. }
  327. return fmt;
  328. }
  329.  
  330. /**
  331. * 默认的日期格式
  332. * 不加 Z 为本地日期时间,避免出现时区的问题
  333. */
  334. const dateFormatter = 'yyyy-MM-ddThh:mm:ss.SSS';
  335. /**
  336. * 将参数 key 与 value 进行 url 编码
  337. * @param k 参数的名字
  338. * @param v 参数的值
  339. * @returns 编码后的字符串
  340. */
  341. const encode = (k, v) => encodeURIComponent(k) + '=' + encodeURIComponent(v);
  342. /**
  343. * 拼接参数字符串
  344. * @param params 参数对象
  345. * @returns 拼接后的字符串
  346. */
  347. function spliceParams(params = {}) {
  348. return Array.from(Object.entries(params)).reduce((res, [k, v]) => {
  349. if (v === undefined || v === null) {
  350. return res;
  351. }
  352. else if (v instanceof Date) {
  353. res += encode(k, dateFormat(v, dateFormatter));
  354. }
  355. else if (v instanceof Array) {
  356. res += v
  357. .map(item => encode(k, item instanceof Date ? dateFormat(item, dateFormatter) : item))
  358. .join('&');
  359. }
  360. else {
  361. res += encode(k, v);
  362. }
  363. return (res += '&');
  364. }, '');
  365. }
  366.  
  367. /**
  368. * 等待指定的时间/等待指定表达式成立
  369. * 如果未指定等待条件则立刻执行
  370. * 注: 此实现在 nodejs 10- 会存在宏任务与微任务的问题,切记 async-await 本质上还是 Promise 的语法糖,实际上并非真正的同步函数!!!即便在浏览器,也不要依赖于这种特性。
  371. * @param param 等待时间/等待条件
  372. * @returns Promise 对象
  373. */
  374. function wait(param) {
  375. return new Promise(resolve => {
  376. if (typeof param === 'number') {
  377. setTimeout(resolve, param);
  378. }
  379. else if (typeof param === 'function') {
  380. const timer = setInterval(() => {
  381. if (param()) {
  382. clearInterval(timer);
  383. resolve();
  384. }
  385. }, 100);
  386. }
  387. else {
  388. resolve();
  389. }
  390. });
  391. }
  392.  
  393. /**
  394. * 为 fetch 请求添加超时选项
  395. * 注:超时选项并非真正意义上的超时即取消请求,请求依旧正常执行完成,但会提前返回 reject 结果
  396. * @param fetchPromise fetch 请求的 Promise
  397. * @param timeout 超时时间
  398. * @returns 如果超时就提前返回 reject, 否则正常返回 fetch 结果
  399. */
  400. function fetchTimeout(fetchPromise, timeout) {
  401. return Promise.race([
  402. fetchPromise,
  403. wait(timeout).then(() => {
  404. throw new Error('timeout');
  405. }),
  406. ]);
  407. }
  408.  
  409. /**
  410. * 将字符串转为字符流
  411. * @param str 字符串
  412. * @returns 字符流对象
  413. */
  414. function strToArrayBuffer(str) {
  415. const buf = new ArrayBuffer(str.length);
  416. const view = new Uint8Array(buf);
  417. for (let i = 0; i < str.length; ++i) {
  418. view[i] = str.charCodeAt(i) & 0xff;
  419. }
  420. return buf;
  421. }
  422.  
  423. /**
  424. * 限制并发请求数量的 fetch 封装
  425. * @class FetchLimiting
  426. * @example
  427. * const fetchLimiting = new FetchLimiting()
  428. * fetchLimiting._fetch('/')
  429. * .then(res => res.json())
  430. * .then(json => console.log(json))
  431. * @deprecated 已废弃,请使用 {@link asyncLimiting} 函数
  432. */
  433. class FetchLimiting {
  434. /**
  435. * 构造函数
  436. * @param option 可选配置项
  437. * @param option.timeout 超时毫秒数
  438. * @param option.limit 最大并发数限制
  439. */
  440. constructor({ timeout = 10000, limit = 10, } = {}) {
  441. /**
  442. * @field timeout 超时毫秒数
  443. */
  444. this.timeout = timeout;
  445. /**
  446. * @field limit 最大并发数限制
  447. */
  448. this.limit = limit;
  449. /**
  450. * @field execCount 当前正在执行异步的数量
  451. */
  452. this.execCount = 0;
  453. /**
  454. * @field waitArr 等待的队列
  455. * @type {Array.<IArguments>}
  456. */
  457. this.waitArr = [];
  458. }
  459. /**
  460. * 执行一个请求
  461. * 如果到达最大并发限制时就进行等待
  462. * @param url 请求 url 信息
  463. * @param init 请求的其他可选项,默认为 undefined
  464. * @returns 如果超时就提前返回 reject, 否则正常返回 fetch 结果
  465. */
  466. fetch(input, init) {
  467. return __awaiter(this, void 0, void 0, function* () {
  468. const _innerFetch = () => __awaiter(this, void 0, void 0, function* () {
  469. this.execCount++;
  470. const args = this.waitArr.shift();
  471. try {
  472. // 这里的 args 实际上就是 arguments 对象,即上面的 url 和 init
  473. return yield fetchTimeout(fetch(args[0], args[1]), this.timeout);
  474. }
  475. finally {
  476. this.execCount--;
  477. }
  478. });
  479. this.waitArr.push([input, init]);
  480. yield wait(() => this.execCount < this.limit);
  481. // 尝试启动等待队列
  482. return _innerFetch();
  483. });
  484. }
  485. }
  486.  
  487. /**
  488. * 将一个 Iterator 迭代器转换为一个 Array
  489. * @param iterator Iterator 迭代器
  490. * @return Iterator 中每一项元素转换而得到的 Array
  491. * @deprecated 已废弃,请使用 ES6 原生函数 {@see Array.from} 替代
  492. */
  493. function asIterator(iterator) {
  494. const arr = [];
  495. while (true) {
  496. const next = iterator.next();
  497. if (next.done) {
  498. break;
  499. }
  500. arr.push(next.value);
  501. }
  502. return arr;
  503. }
  504.  
  505. /**
  506. * 判断一个对象是否是无效的
  507. * 无效的值仅包含 null/undefined
  508. * @param object 任何一个对象
  509. * @returns 是否无效的值
  510. */
  511. function isNullOrUndefined(object) {
  512. return object === undefined || object === null;
  513. }
  514.  
  515. /**
  516. * 返回第一个参数的函数
  517. * 注: 一般可以当作返回参数自身的函数,如果你只关注第一个参数的话
  518. * @param obj 任何对象
  519. * @typeparam T 传入参数的类型
  520. * @typeparam R 返回结果的类型,默认为 T,只是为了兼容该函数当参数被传递时可能出现需要类型不一致的问题
  521. * @returns 传入的第一个参数
  522. */
  523. function returnItself(obj) {
  524. return obj;
  525. }
  526.  
  527. /**
  528. * 兼容异步函数的返回值
  529. * @param res 返回值
  530. * @param callback 同步/异步结果的回调函数
  531. * @typeparam T 处理参数的类型,如果是 Promise 类型,则取出其泛型类型
  532. * @typeparam Param 处理参数具体的类型,如果是 Promise 类型,则指定为原类型
  533. * @typeparam R 返回值具体的类型,如果是 Promise 类型,则指定为 Promise 类型,否则为原类型
  534. * @returns 处理后的结果,如果是同步的,则返回结果是同步的,否则为异步的
  535. */
  536. function compatibleAsync(res, callback) {
  537. return (res instanceof Promise
  538. ? res.then(callback)
  539. : callback(res));
  540. }
  541.  
  542. /**
  543. * 内部使用的函数
  544. * 注: 如果谓词中包含任意一个异步(返回 Promise)函数,则整个返回结果将变成异步的,否则默认为同步操作.
  545. * @param fns 谓词数组
  546. * @param args 谓词应用的参数列表
  547. * @param condition 临界条件
  548. * @returns 返回结果
  549. */
  550. function _inner(fns, args, condition) {
  551. const fn = fns[0];
  552. const res = fn(...args);
  553. function _call(res) {
  554. if (condition(res)) {
  555. return res;
  556. }
  557. const others = fns.slice(1);
  558. if (others.length === 0) {
  559. return res;
  560. }
  561. return _inner(others, args, condition);
  562. }
  563. return compatibleAsync(res, _call);
  564. }
  565. /**
  566. * 连接谓词函数
  567. */
  568. class CombinedPredicate {
  569. /**
  570. * 使用 && 进行连接
  571. * @param fns 连接任意多个谓词
  572. * @returns 连接后的新谓词
  573. */
  574. static and(...fns) {
  575. return function (...args) {
  576. return _inner(fns, args, res => !res);
  577. };
  578. }
  579. /**
  580. * 使用 || 进行连接
  581. * @param fns 连接任意多个谓词
  582. * @returns 连接后的新谓词
  583. */
  584. static or(...fns) {
  585. return function (...args) {
  586. return _inner(fns, args, res => res);
  587. };
  588. }
  589. /**
  590. * 对谓词进行取反
  591. * @param fn 谓词
  592. * @returns 取反后的谓词
  593. */
  594. static not(fn) {
  595. return new Proxy(fn, {
  596. apply(_, _this, args) {
  597. return compatibleAsync(Reflect.apply(_, this, args), res => !res);
  598. },
  599. });
  600. }
  601. }
  602. const and = CombinedPredicate.and;
  603. const or = CombinedPredicate.or;
  604. const not = CombinedPredicate.not;
  605.  
  606. /**
  607. * 操作类型
  608. */
  609. var ActionType;
  610. (function (ActionType) {
  611. ActionType["forEach"] = "forEach";
  612. ActionType["filter"] = "filter";
  613. ActionType["map"] = "map";
  614. ActionType["flatMap"] = "flatMap";
  615. ActionType["sort"] = "sort";
  616. ActionType["reduce"] = "reduce";
  617. ActionType["reduceRight"] = "reduceRight";
  618. ActionType["findIndex"] = "findIndex";
  619. ActionType["find"] = "find";
  620. ActionType["every"] = "every";
  621. ActionType["some"] = "some";
  622. ActionType["parallel"] = "parallel";
  623. ActionType["serial"] = "serial";
  624. })(ActionType || (ActionType = {}));
  625. /**
  626. * 保存高阶函数传入的异步操作
  627. * @field 异步操作的类型
  628. * @field 异步操作
  629. */
  630. class Action {
  631. constructor(type, args) {
  632. this.type = type;
  633. this.args = args;
  634. this.type = type;
  635. this.args = args;
  636. }
  637. }
  638. Action.Type = ActionType;
  639. /**
  640. * 抽象异步数组,实现了一些公共的函数
  641. */
  642. class InnerBaseAsyncArray {
  643. /**
  644. * 构造函数
  645. * @param args 数组初始元素
  646. */
  647. constructor(args = []) {
  648. this._arr = args;
  649. }
  650. /**
  651. * 将整个数组排序
  652. * @param fn 比较函数
  653. * @returns 排序后的数组
  654. */
  655. sort(fn) {
  656. return __awaiter(this, void 0, void 0, function* () {
  657. if (fn === undefined) {
  658. return new InnerAsyncArray(this._arr.sort());
  659. }
  660. // TODO 此处为了让 type-doc 能生成文档而不得不加上类型
  661. const arr = this._arr.map((v, i) => [v, i]);
  662. function _sort(arr, fn) {
  663. return __awaiter(this, void 0, void 0, function* () {
  664. // 边界条件,如果传入数组的值
  665. if (arr.length <= 1) {
  666. return arr;
  667. }
  668. // 根据中间值对数组分治为两个数组
  669. const medianIndex = Math.floor(arr.length / 2);
  670. const medianValue = arr[medianIndex];
  671. const left = [];
  672. const right = [];
  673. for (let i = 0, len = arr.length; i < len; i++) {
  674. if (i === medianIndex) {
  675. continue;
  676. }
  677. const v = arr[i];
  678. if ((yield fn(v, medianValue)) <= 0) {
  679. left.push(v);
  680. }
  681. else {
  682. right.push(v);
  683. }
  684. }
  685. return (yield _sort(left, fn))
  686. .concat([medianValue])
  687. .concat(yield _sort(right, fn));
  688. });
  689. }
  690. return new InnerAsyncArray(yield (yield _sort(arr, ([t1], [t2]) => fn(t1, t2))).map(([_v, i]) => this._arr[i]));
  691. });
  692. }
  693. /**
  694. * 异步的 find
  695. * @param fn 异步查询函数
  696. * @returns 查询到的第一个值
  697. */
  698. find(fn) {
  699. return __awaiter(this, void 0, void 0, function* () {
  700. const i = yield this.findIndex(fn);
  701. return i === -1 ? null : this._arr[i];
  702. });
  703. }
  704. /**
  705. * 异步的 every
  706. * @param fn 异步匹配函数
  707. * @returns 是否全部匹配
  708. */
  709. every(fn) {
  710. return __awaiter(this, void 0, void 0, function* () {
  711. return (yield this.findIndex(CombinedPredicate.not(fn))) === -1;
  712. });
  713. }
  714. /**
  715. * 异步的 some
  716. * @param fn 异步匹配函数
  717. * @returns 是否有任意一个匹配
  718. */
  719. some(fn) {
  720. return __awaiter(this, void 0, void 0, function* () {
  721. return (yield this.findIndex(fn)) !== -1;
  722. });
  723. }
  724. /**
  725. * 转换为并发异步数组
  726. */
  727. parallel() {
  728. return new InnerAsyncArrayParallel(this._arr);
  729. }
  730. /**
  731. * 转换为顺序异步数组
  732. */
  733. serial() {
  734. return new InnerAsyncArray(this._arr);
  735. }
  736. /**
  737. * 获取内部数组的值,将返回一个浅复制的数组
  738. */
  739. value() {
  740. return this._arr.slice();
  741. }
  742. }
  743. /**
  744. * 串行的异步数组
  745. */
  746. class InnerAsyncArray extends InnerBaseAsyncArray {
  747. constructor(args) {
  748. super(args);
  749. }
  750. forEach(fn) {
  751. return __awaiter(this, void 0, void 0, function* () {
  752. for (let i = 0, len = this._arr.length; i < len; i++) {
  753. yield fn.call(this, this._arr[i], i, this);
  754. }
  755. });
  756. }
  757. filter(fn) {
  758. return __awaiter(this, void 0, void 0, function* () {
  759. const res = new InnerAsyncArray();
  760. for (let i = 0, len = this._arr.length; i < len; i++) {
  761. if (yield fn.call(this, this._arr[i], i, this)) {
  762. res._arr.push(this._arr[i]);
  763. }
  764. }
  765. return res;
  766. });
  767. }
  768. map(fn) {
  769. return __awaiter(this, void 0, void 0, function* () {
  770. const res = new InnerAsyncArray();
  771. for (let i = 0, len = this._arr.length; i < len; i++) {
  772. res._arr.push(yield fn.call(this, this._arr[i], i, this));
  773. }
  774. return res;
  775. });
  776. }
  777. flatMap(fn) {
  778. return __awaiter(this, void 0, void 0, function* () {
  779. const res = new InnerAsyncArray();
  780. for (let i = 0, len = this._arr.length; i < len; i++) {
  781. res._arr.push(...(yield fn.call(this, this._arr[i], i, this)));
  782. }
  783. return res;
  784. });
  785. }
  786. reduce(fn, res) {
  787. return __awaiter(this, void 0, void 0, function* () {
  788. for (let i = 0, len = this._arr.length; i < len; i++) {
  789. if (res) {
  790. res = yield fn.call(this, res, this._arr[i], i, this);
  791. }
  792. else {
  793. res = this._arr[i];
  794. }
  795. }
  796. return res;
  797. });
  798. }
  799. reduceRight(fn, res) {
  800. return __awaiter(this, void 0, void 0, function* () {
  801. for (let i = this._arr.length - 1; i >= 0; i--) {
  802. if (res) {
  803. res = yield fn.apply(this, [res, this._arr[i], i, this]);
  804. }
  805. else {
  806. res = this._arr[i];
  807. }
  808. }
  809. return res;
  810. });
  811. }
  812. findIndex(fn) {
  813. return __awaiter(this, void 0, void 0, function* () {
  814. for (let i = 0, len = this._arr.length; i < len; i++) {
  815. const res = yield fn.call(this, this._arr[i], i, this);
  816. if (res) {
  817. return i;
  818. }
  819. }
  820. return -1;
  821. });
  822. }
  823. }
  824. /**
  825. * 并发的异步数组
  826. */
  827. class InnerAsyncArrayParallel extends InnerBaseAsyncArray {
  828. constructor(args) {
  829. super(args);
  830. }
  831. forEach(fn) {
  832. return __awaiter(this, void 0, void 0, function* () {
  833. yield this._all(fn);
  834. });
  835. }
  836. filter(fn) {
  837. return __awaiter(this, void 0, void 0, function* () {
  838. const res = yield this._all(fn);
  839. const result = new InnerAsyncArrayParallel();
  840. for (let i = 0, len = res.length; i < len; i++) {
  841. if (res[i]) {
  842. result._arr.push(this._arr[i]);
  843. }
  844. }
  845. return result;
  846. });
  847. }
  848. map(fn) {
  849. return __awaiter(this, void 0, void 0, function* () {
  850. return new InnerAsyncArrayParallel(yield this._all(fn));
  851. });
  852. }
  853. flatMap(fn) {
  854. return __awaiter(this, void 0, void 0, function* () {
  855. const res = yield this._all(fn);
  856. return new InnerAsyncArrayParallel(res.flat());
  857. });
  858. }
  859. sort(fn) {
  860. throw new Error('Method not implemented.');
  861. }
  862. reduce(fn, res) {
  863. return __awaiter(this, void 0, void 0, function* () {
  864. for (let i = 0, len = this._arr.length; i < len; i++) {
  865. if (res) {
  866. res = yield fn.call(this, res, this._arr[i], i, this);
  867. }
  868. else {
  869. res = this._arr[i];
  870. }
  871. }
  872. return res;
  873. });
  874. }
  875. reduceRight(fn, res) {
  876. return __awaiter(this, void 0, void 0, function* () {
  877. for (let i = this._arr.length - 1; i >= 0; i--) {
  878. if (res) {
  879. res = yield fn.apply(this, [res, this._arr[i], i, this]);
  880. }
  881. else {
  882. res = this._arr[i];
  883. }
  884. }
  885. return res;
  886. });
  887. }
  888. findIndex(fn) {
  889. return __awaiter(this, void 0, void 0, function* () {
  890. return (yield this._all(fn)).findIndex(returnItself);
  891. });
  892. }
  893. _all(fn) {
  894. return __awaiter(this, void 0, void 0, function* () {
  895. return yield Promise.all(this._arr.map((v, i) => fn.apply(this, [v, i, this])));
  896. });
  897. }
  898. }
  899. /**
  900. * 异步数组
  901. */
  902. class AsyncArray {
  903. /**
  904. * 构造函数
  905. * @param args 任意个参数
  906. */
  907. constructor(...args) {
  908. /**
  909. * 内部数组的长度,用于让 {@link AsyncArray} 的实例能作为 {@link Array.from} 的参数
  910. */
  911. this.length = 0;
  912. this._arr = Array.from(args);
  913. /**
  914. * @field 保存异步任务
  915. * @type {Action[]}
  916. */
  917. this._tasks = [];
  918. }
  919. /**
  920. * 为内置数组赋值
  921. * 此处自动重新计算 length 属性
  922. */
  923. set _arr(arr) {
  924. this.__arr = arr;
  925. this.length = this.__arr.length;
  926. }
  927. get _arr() {
  928. return this.__arr;
  929. }
  930. /**
  931. * 提供一个函数方便根据已有的数组或类数组(Set/Map)创建 {@link AsyncArray}
  932. * @param arr 一个可迭代元素
  933. * @returns 创建一个新的异步数组包装
  934. */
  935. static from(arr) {
  936. const result = new AsyncArray();
  937. if (isNullOrUndefined(arr)) {
  938. return result;
  939. }
  940. result._arr = Array.from(arr);
  941. return result;
  942. }
  943. filter(fn) {
  944. return this._addTask(new Action(Action.Type.filter, [fn]));
  945. }
  946. map(fn) {
  947. return this._addTask(new Action(Action.Type.map, [fn]));
  948. }
  949. flatMap(fn) {
  950. return this._addTask(new Action(Action.Type.flatMap, [fn]));
  951. }
  952. sort(fn) {
  953. return this._addTask(new Action(Action.Type.sort, [fn]));
  954. }
  955. parallel() {
  956. return this._addTask(new Action(Action.Type.parallel, []));
  957. }
  958. serial() {
  959. return this._addTask(new Action(Action.Type.serial, []));
  960. }
  961. forEach(fn) {
  962. return this._addTask(new Action(Action.Type.forEach, [fn])).then();
  963. }
  964. some(fn) {
  965. return this._addTask(new Action(Action.Type.some, [fn])).then();
  966. }
  967. every(fn) {
  968. return this._addTask(new Action(Action.Type.every, [fn])).then();
  969. }
  970. find(fn) {
  971. return this._addTask(new Action(Action.Type.find, [fn])).then();
  972. }
  973. findIndex(fn) {
  974. return this._addTask(new Action(Action.Type.findIndex, [fn])).then();
  975. }
  976. reduce(fn, res) {
  977. return this._addTask(new Action(Action.Type.reduce, [fn, res])).then();
  978. }
  979. reduceRight(fn, res) {
  980. return this._addTask(new Action(Action.Type.reduceRight, [fn, res])).then();
  981. }
  982. /**
  983. * 终结整个链式操作并返回结果,可以使用 await 等待当前实例开始计算
  984. */
  985. then(onfulfilled, onrejected) {
  986. return __awaiter(this, void 0, void 0, function* () {
  987. try {
  988. let asyncArray = new InnerAsyncArray(this._arr);
  989. let result = this._arr;
  990. for (const task of this._tasks) {
  991. asyncArray = yield Reflect.apply(Reflect.get(asyncArray, task.type), asyncArray, task.args);
  992. if (asyncArray instanceof InnerBaseAsyncArray) {
  993. result = asyncArray.value();
  994. }
  995. else {
  996. if (!isNullOrUndefined(onfulfilled)) {
  997. onfulfilled(result);
  998. }
  999. return asyncArray;
  1000. }
  1001. }
  1002. if (!isNullOrUndefined(onfulfilled)) {
  1003. onfulfilled(result);
  1004. }
  1005. return result;
  1006. }
  1007. catch (err) {
  1008. if (!isNullOrUndefined(onrejected)) {
  1009. onrejected(err);
  1010. }
  1011. }
  1012. });
  1013. }
  1014. /**
  1015. * @deprecated 已废弃,请直接使用 await 进行等待获取结果值即可
  1016. */
  1017. value() {
  1018. return this.then();
  1019. }
  1020. /**
  1021. * 允许使用 for-of 遍历内部的 _arr
  1022. */
  1023. *[Symbol.iterator]() {
  1024. for (const kv of this._arr) {
  1025. yield kv;
  1026. }
  1027. }
  1028. _addTask(task) {
  1029. const result = new AsyncArray(...this._arr);
  1030. result._tasks = [...this._tasks, task];
  1031. return result;
  1032. }
  1033. }
  1034.  
  1035. function asyncFlatMap(arr, fn) {
  1036. return __awaiter(this, void 0, void 0, function* () {
  1037. return new AsyncArray(...arr).flatMap(fn);
  1038. });
  1039. }
  1040.  
  1041. /**
  1042. * 判断数字是否在指定区间之中
  1043. * @param num 指定数字
  1044. * @param min 最小值
  1045. * @param max 最大值(不包含)
  1046. */
  1047. function isRange(num, min, max) {
  1048. return num >= min && num < max;
  1049. }
  1050.  
  1051. /**
  1052. * 判断是否为小数的正则表达式
  1053. */
  1054. const FloatRule = /^(-?\d+)(.\d+)?$/;
  1055. /**
  1056. * 判断是否为整数的正则表达式
  1057. */
  1058. const IntegerRule = /^-?\d+$/;
  1059. /**
  1060. * 判断是否为邮箱的正则表达式
  1061. */
  1062. const EmailRule = /^\w+((-\w+)|(\.\w+))*@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z]+$/;
  1063. /**
  1064. * 判断是否为 ipv4 地址的正则表达式
  1065. */
  1066. const Ipv4Rule = /^((25[0-5]|2[0-4]\d|1?\d?\d)\.){3}(25[0-5]|2[0-4]\d|1?\d?\d)$/;
  1067. /**
  1068. * 判断是否为固定电话的正则表达式
  1069. */
  1070. const TelephoneRule = /^0[1-9][0-9]{1,2}-[2-8][0-9]{6,7}$/;
  1071. /**
  1072. * 判断是否为移动电话的正则表达式
  1073. * 注:不在判断二三位的数字,具体参考:http://caibaojian.com/phone-regexp.html
  1074. */
  1075. const MobileRule = /^1\d{10}$/;
  1076. /**
  1077. * 判断是否为域名的正则表达式
  1078. */
  1079. const DomainRule = /^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$/;
  1080. /**
  1081. * 判断是否为邮政编码的正则表达式
  1082. */
  1083. const PostcodeRule = /^\d{6}$/;
  1084. /**
  1085. * 字符串校验
  1086. * @suppress 之后将会对类型定义进行不兼容修改,避免一直出现的两难问题
  1087. */
  1088. class StringValidator {
  1089. /**
  1090. * 判断一个字符串是否为空字符串
  1091. * @param str 字符串
  1092. * @returns 是否为空字符串
  1093. */
  1094. static isEmpty(str) {
  1095. return isNullOrUndefined(str) || str === '';
  1096. }
  1097. /**
  1098. * 判断一个字符串是否为空白的字符串
  1099. * @param str 字符串
  1100. * @returns 是否为空字符串
  1101. */
  1102. static isBlank(str) {
  1103. return StringValidator.isEmpty(str) || str.trim() === '';
  1104. }
  1105. /**
  1106. * 判断字符串是否位小数
  1107. * @param str 需要进行判断的字符串
  1108. * @returns 是否为小数
  1109. */
  1110. static isFloat(str) {
  1111. if (isNullOrUndefined(str)) {
  1112. return false;
  1113. }
  1114. return FloatRule.test(str);
  1115. }
  1116. /**
  1117. * 判断字符串是否位整数
  1118. * @param str 需要进行判断的字符串
  1119. * @returns 是否为小数
  1120. */
  1121. static isInteger(str) {
  1122. return !isNullOrUndefined(str) && IntegerRule.test(str);
  1123. }
  1124. /**
  1125. * 判断邮箱的格式是否正确
  1126. * @param str 邮箱字符串
  1127. * @returns 是否是邮箱
  1128. */
  1129. static isEmail(str) {
  1130. return !isNullOrUndefined(str) && EmailRule.test(str);
  1131. }
  1132. /**
  1133. * 判断 ipv4 地址的格式是否正确
  1134. * @param str ipv4 字符串
  1135. * @returns 是否是 ipv4 地址
  1136. */
  1137. static isIpv4(str) {
  1138. return !isNullOrUndefined(str) && Ipv4Rule.test(str);
  1139. }
  1140. /**
  1141. * 判断字符串是否为正确的端口号
  1142. * 正确的端口号是 1-65535
  1143. * @param str 字符串
  1144. * @returns 是否为端口号
  1145. */
  1146. static isPort(str) {
  1147. // tslint:disable-next-line:radix
  1148. return StringValidator.isInteger(str) && isRange(parseInt(str), 1, 65535);
  1149. }
  1150. /**
  1151. * 判断是否为固定电话
  1152. * @param str 字符串
  1153. * @returns 是否为固定电话
  1154. */
  1155. static isTelephone(str) {
  1156. return !isNullOrUndefined(str) && TelephoneRule.test(str);
  1157. }
  1158. /**
  1159. * 判断是否为移动电话
  1160. * @param str 字符串
  1161. * @returns 是否为移动电话
  1162. */
  1163. static isMobile(str) {
  1164. return !isNullOrUndefined(str) && MobileRule.test(str);
  1165. }
  1166. /**
  1167. * 判断是否为域名
  1168. * @param str 字符串
  1169. * @returns 是否为域名
  1170. */
  1171. static isDomain(str) {
  1172. return !isNullOrUndefined(str) && DomainRule.test(str);
  1173. }
  1174. /**
  1175. * 判断是否为邮政编码
  1176. * @param str 字符串
  1177. * @returns 是否为邮政编码
  1178. */
  1179. static isPostcode(str) {
  1180. return !isNullOrUndefined(str) && PostcodeRule.test(str);
  1181. }
  1182. }
  1183. /**
  1184. * 导出一个字符串校验的对象
  1185. * @deprecated 已废弃,请直接使用类的静态函数
  1186. */
  1187. const stringValidator = StringValidator;
  1188.  
  1189. /**
  1190. * 可能的类型
  1191. */
  1192. var Type;
  1193. (function (Type) {
  1194. Type[Type["String"] = 0] = "String";
  1195. Type[Type["Number"] = 1] = "Number";
  1196. Type[Type["Boolean"] = 2] = "Boolean";
  1197. Type[Type["Undefined"] = 3] = "Undefined";
  1198. Type[Type["Null"] = 4] = "Null";
  1199. Type[Type["Symbol"] = 5] = "Symbol";
  1200. Type[Type["PropertyKey"] = 6] = "PropertyKey";
  1201. Type[Type["Object"] = 7] = "Object";
  1202. Type[Type["Array"] = 8] = "Array";
  1203. Type[Type["Function"] = 9] = "Function";
  1204. Type[Type["Date"] = 10] = "Date";
  1205. Type[Type["File"] = 11] = "File";
  1206. Type[Type["Blob"] = 12] = "Blob";
  1207. Type[Type["Stream"] = 13] = "Stream";
  1208. Type[Type["ArrayBuffer"] = 14] = "ArrayBuffer";
  1209. Type[Type["ArrayBufferView"] = 15] = "ArrayBufferView";
  1210. Type[Type["URLSearchParams"] = 16] = "URLSearchParams";
  1211. Type[Type["FormData"] = 17] = "FormData";
  1212. })(Type || (Type = {}));
  1213. /**
  1214. * 校验变量的类型
  1215. */
  1216. class TypeValidator {
  1217. /**
  1218. * 获取变量的类型
  1219. * @param val 变量
  1220. * @returns 类型
  1221. * 注: 此函数依赖于 ts 的编译枚举原理与约定 {@link TypeValidator} 中所有判断函数都是以 `is` 开头并于 {@link Type} 中的保持一致
  1222. */
  1223. static getType(val) {
  1224. for (const k of Object.keys(Type)) {
  1225. if (StringValidator.isInteger(k)) {
  1226. const type = Type[k];
  1227. if (TypeValidator['is' + type](val)) {
  1228. return Type[type];
  1229. }
  1230. }
  1231. }
  1232. throw new Error('无法识别的类型');
  1233. }
  1234. /**
  1235. * 判断是否为指定类型
  1236. * @param val 需要判断的值
  1237. * @param types 需要判断的类型
  1238. */
  1239. static isType(val, ...types) {
  1240. return types.includes(TypeValidator.getType(val));
  1241. }
  1242. /**
  1243. * 判断是否为字符串
  1244. * @param val 需要判断的值
  1245. * @returns 是否为字符串
  1246. */
  1247. static isString(val) {
  1248. return typeof val === 'string';
  1249. }
  1250. /**
  1251. * 判断是否为数字
  1252. * @param val 需要判断的值
  1253. * @returns 是否为数字
  1254. */
  1255. static isNumber(val) {
  1256. return typeof val === 'number';
  1257. }
  1258. /**
  1259. * 判断是否为布尔值
  1260. * @param val 需要判断的值
  1261. * @returns 是否为布尔值
  1262. */
  1263. static isBoolean(val) {
  1264. return typeof val === 'boolean';
  1265. }
  1266. /**
  1267. * 判断是否为 Symbol
  1268. * @param val 需要判断的值
  1269. * @returns 是否为 Symbol
  1270. */
  1271. static isSymbol(val) {
  1272. return typeof val === 'symbol';
  1273. }
  1274. /**
  1275. * 判断是否为 undefined
  1276. * @param val 需要判断的值
  1277. * @returns 是否为 undefined
  1278. */
  1279. static isUndefined(val) {
  1280. return val === undefined;
  1281. }
  1282. /**
  1283. * 判断是否为 null
  1284. * @param val 需要判断的值
  1285. * @returns 是否为 null
  1286. */
  1287. static isNull(val) {
  1288. return val === null;
  1289. }
  1290. /**
  1291. * 判断是否可以作为对象的属性
  1292. * @param val 需要判断的值
  1293. * @returns 是否为对象属性
  1294. */
  1295. static isPropertyKey(val) {
  1296. return (TypeValidator.isString(val) ||
  1297. TypeValidator.isNumber(val) ||
  1298. TypeValidator.isSymbol(val));
  1299. }
  1300. /**
  1301. * 判断是否为对象
  1302. * 注: 函数(包括 ES6 箭头函数)将不被视为对象
  1303. * @param val 需要判断的值
  1304. * @returns 是否为对象
  1305. */
  1306. static isObject(val) {
  1307. return (!TypeValidator.isNull(val) &&
  1308. !TypeValidator.isUndefined(val) &&
  1309. typeof val === 'object');
  1310. }
  1311. /**
  1312. * 判断是否为数组
  1313. * @param val 需要判断的值
  1314. * @returns 是否为数组
  1315. */
  1316. static isArray(val) {
  1317. return Array.isArray(val);
  1318. }
  1319. /**
  1320. * 判断是否为数组
  1321. * @param val 需要判断的值
  1322. * @returns 是否为数组
  1323. */
  1324. static isFunction(val) {
  1325. return toString.call(val) === '[object Function]';
  1326. }
  1327. /**
  1328. * 判断是否为日期
  1329. * @param val 需要判断的值
  1330. * @returns 是否为日期
  1331. */
  1332. static isDate(val) {
  1333. return toString.call(val) === '[object Date]';
  1334. }
  1335. /**
  1336. * 判断是否为浏览器文件类型
  1337. * @param val 需要判断的值
  1338. * @returns 是否为浏览器文件类型
  1339. */
  1340. static isFile(val) {
  1341. return toString.call(val) === '[object File]';
  1342. }
  1343. /**
  1344. * 判断是否为浏览器二进制类型
  1345. * @param val 需要判断的值
  1346. * @returns 是否为浏览器二进制类型
  1347. */
  1348. static isBlob(val) {
  1349. return toString.call(val) === '[object Blob]';
  1350. }
  1351. /**
  1352. * 判断是否为浏览器流类型
  1353. * @param val 需要判断的值
  1354. * @returns 是否为浏览器流类型
  1355. */
  1356. static isStream(val) {
  1357. return TypeValidator.isObject(val) && TypeValidator.isFunction(val.pipe);
  1358. }
  1359. /**
  1360. * 判断是否为浏览器 ArrayBuffer 类型
  1361. * @param val 需要判断的值
  1362. * @returns 是否为浏览器 ArrayBuffer 类型
  1363. */
  1364. static isArrayBuffer(val) {
  1365. return toString.call(val) === '[object ArrayBuffer]';
  1366. }
  1367. /**
  1368. * 判断是否为浏览器 ArrayBufferView 类型
  1369. * @param val 需要判断的值
  1370. * @returns 是否为浏览器 ArrayBufferView 类型
  1371. */
  1372. static isArrayBufferView(val) {
  1373. return typeof ArrayBuffer !== 'undefined' && ArrayBuffer.isView
  1374. ? ArrayBuffer.isView(val)
  1375. : val && val.buffer && val.buffer instanceof ArrayBuffer;
  1376. }
  1377. /**
  1378. * 判断是否为浏览器 URLSearchParams 类型
  1379. * @param val 需要判断的值
  1380. * @returns 是否为浏览器 URLSearchParams 类型
  1381. */
  1382. static isURLSearchParams(val) {
  1383. return !TypeValidator.isUndefined(val) && val instanceof URLSearchParams;
  1384. }
  1385. /**
  1386. * 判断是否为浏览器 FormData 类型
  1387. * @param val 需要判断的值
  1388. * @returns 是否为浏览器 FormData 类型
  1389. */
  1390. static isFormData(val) {
  1391. return !TypeValidator.isUndefined(val) && val instanceof FormData;
  1392. }
  1393. }
  1394. /**
  1395. * 类型枚举类对象
  1396. */
  1397. TypeValidator.Type = Type;
  1398.  
  1399. /**
  1400. * 安全执行某个函数
  1401. * 支持异步函数
  1402. * @param fn 需要执行的函数
  1403. * @param defaultVal 发生异常后的默认返回值,默认为 null
  1404. * @param args 可选的函数参数
  1405. * @returns 函数执行的结果,或者其默认值
  1406. */
  1407. function safeExec(fn, defaultVal, ...args) {
  1408. const defRes = (defaultVal === undefined ? null : defaultVal);
  1409. try {
  1410. const res = fn(...args);
  1411. return res instanceof Promise ? res.catch(() => defRes) : res;
  1412. }
  1413. catch (err) {
  1414. return defRes;
  1415. }
  1416. }
  1417.  
  1418. /**
  1419. * 提取对象中的字段并封装为函数
  1420. * @param k 提取的字段,深度获取使用 . 分割不同的字段
  1421. * @returns 获取对象中指定字段的函数
  1422. */
  1423. function extractField(k) {
  1424. const fields = TypeValidator.isString(k) ? k.split('.') : [k];
  1425. return fields.reduceRight((fn, field) => {
  1426. return function (obj) {
  1427. return safeExec(() => fn(Reflect.get(obj, field)));
  1428. };
  1429. }, returnItself);
  1430. }
  1431.  
  1432. /**
  1433. * 获取提取对象属性的函数
  1434. * @param k 提取对象属性的函数或者是属性名(允许使用 . 进行分割)
  1435. * @returns 提取对象属性的函数
  1436. */
  1437. function getKFn(k) {
  1438. return k instanceof Function ? k : extractField(k);
  1439. }
  1440.  
  1441. /**
  1442. * 自行实现 flatMap,将数组压平一层
  1443. * @param arr 数组
  1444. * @param k 映射方法,将一个元素映射为一个数组
  1445. * @returns 压平一层的数组
  1446. */
  1447. function flatMap(arr, k = (v) => Array.from(v)) {
  1448. const fn = getKFn(k);
  1449. return arr.reduce((res, v, i, arr) => {
  1450. res.push(...fn(v, i, arr));
  1451. return res;
  1452. }, new Array());
  1453. }
  1454.  
  1455. function groupBy(arr, k,
  1456. /**
  1457. * 默认的值处理函数
  1458. * @param res 最终 V 集合
  1459. * @param item 当前迭代的元素
  1460. * @returns 将当前元素合并后的最终 V 集合
  1461. */
  1462. vFn = ((res, item) => {
  1463. res.push(item);
  1464. return res;
  1465. }), init = () => []) {
  1466. const kFn = getKFn(k);
  1467. // 将元素按照分组条件进行分组得到一个 条件 -> 数组 的对象
  1468. return arr.reduce((res, item, index, arr) => {
  1469. const k = kFn(item, index, arr);
  1470. // 如果已经有这个键了就直接追加, 否则先将之初始化再追加元素
  1471. if (!res.has(k)) {
  1472. res.set(k, init());
  1473. }
  1474. res.set(k, vFn(res.get(k), item, index, arr));
  1475. return res;
  1476. }, new Map());
  1477. }
  1478.  
  1479. /**
  1480. * 创建一个等差数列数组
  1481. * @param start 开始(包含)
  1482. * @param end 结束(不包含)
  1483. * @param sep 步长,默认为 1
  1484. * @returns 等差数列数组
  1485. */
  1486. function range(start, end, sep = 1) {
  1487. const arr = [];
  1488. for (let i = start; i < end; i += sep) {
  1489. arr.push(i);
  1490. }
  1491. return arr;
  1492. }
  1493.  
  1494. /**
  1495. * 将数组转化为一个 Object 对象
  1496. * @deprecated 已废弃,请使用更好的 {@link arrayToMap} 替代
  1497. * @param arr 需要进行转换的数组
  1498. * @param k 生成对象属性名的函数
  1499. * @param v 生成对象属性值的函数,默认为数组中的迭代元素
  1500. * @returns 转化得到的对象
  1501. */
  1502. function toObject(arr, k, v = returnItself) {
  1503. const kFn = getKFn(k);
  1504. const vFn = getKFn(v);
  1505. return arr.reduce((res, item, i, arr) => {
  1506. const k = kFn(item, i, arr);
  1507. if (!Reflect.has(res, k)) {
  1508. Reflect.set(res, k, vFn(item, i, arr));
  1509. }
  1510. return res;
  1511. }, {});
  1512. }
  1513.  
  1514. /**
  1515. * js 的数组去重方法
  1516. * @param arr 要进行去重的数组
  1517. * @param k 唯一标识元素的方法,默认使用 {@link returnItself}
  1518. * @returns 进行去重操作之后得到的新的数组 (原数组并未改变)
  1519. */
  1520. function uniqueBy(arr, k = returnItself) {
  1521. const kFn = getKFn(k);
  1522. const set = new Set();
  1523. return arr.filter((v, ...args) => {
  1524. const k = kFn(v, ...args);
  1525. if (set.has(k)) {
  1526. return false;
  1527. }
  1528. set.add(k);
  1529. return true;
  1530. });
  1531. }
  1532.  
  1533. /**
  1534. * 将数组映射为 Map
  1535. * @param arr 数组
  1536. * @param k 产生 Map 元素唯一标识的函数,或者对象元素中的一个属性名
  1537. * @param v 产生 Map 值的函数,默认为返回数组的元素,或者对象元素中的一个属性名
  1538. * @returns 映射产生的 map 集合
  1539. */
  1540. function arrayToMap(arr, k, v = returnItself) {
  1541. const kFn = getKFn(k);
  1542. const vFn = getKFn(v);
  1543. return arr.reduce((res, item, index, arr) => res.set(kFn(item, index, arr), vFn(item, index, arr)), new Map());
  1544. }
  1545.  
  1546. /**
  1547. * 日期格式化类
  1548. */
  1549. class DateFormat {
  1550. /**
  1551. * 构造函数
  1552. * @param name 日期格式的名称
  1553. * @param format 日期的格式值
  1554. * @param value 格式化得到的值
  1555. * @param index 需要替换位置的索引
  1556. */
  1557. constructor(name, format, value, index) {
  1558. this.name = name;
  1559. this.format = format;
  1560. this.value = value;
  1561. this.index = index;
  1562. }
  1563. }
  1564. /**
  1565. * 日期时间的正则表达式
  1566. */
  1567. const dateFormats = new Map()
  1568. .set('year', 'Y{4}|Y{2}|y{4}|y{2}')
  1569. .set('month', 'M{1,2}')
  1570. .set('day', 'D{1,2}|d{1,2}')
  1571. .set('hour', 'h{1,2}')
  1572. .set('minute', 'm{1,2}')
  1573. .set('second', 's{1,2}')
  1574. .set('millieSecond', 'S{1,3}');
  1575. /**
  1576. * 如果没有格式化某项的话则设置为默认时间
  1577. */
  1578. const defaultDateValues = new Map()
  1579. .set('month', '01')
  1580. .set('day', '01')
  1581. .set('hour', '00')
  1582. .set('minute', '00')
  1583. .set('second', '00')
  1584. .set('millieSecond', '000');
  1585. /**
  1586. * 月份日期校验
  1587. */
  1588. const monthDayValidate = {
  1589. 1: 31,
  1590. 3: 31,
  1591. 5: 31,
  1592. 7: 31,
  1593. 8: 31,
  1594. 10: 31,
  1595. 12: 31,
  1596. 4: 30,
  1597. 6: 30,
  1598. 9: 30,
  1599. 11: 30,
  1600. 2: 28,
  1601. };
  1602. /**
  1603. * 解析字符串为 Date 对象
  1604. * @param str 日期字符串
  1605. * @param fmt 日期字符串的格式,目前仅支持使用 y(年),M(月),d(日),h(时),m(分),s(秒),S(毫秒)
  1606. * @returns 解析得到的 Date 对象
  1607. */
  1608. function dateParse(str, fmt) {
  1609. const now = new Date();
  1610. defaultDateValues.set('year', now.getFullYear().toString());
  1611. // 保存对传入的日期字符串进行格式化的全部信息数组列表
  1612. const dateUnits = [];
  1613. for (const [fmtName, regex] of dateFormats) {
  1614. const regExp = new RegExp(regex);
  1615. if (regExp.test(fmt)) {
  1616. const matchStr = regExp.exec(fmt)[0];
  1617. const regexStr = '`'.repeat(matchStr.length);
  1618. const index = fmt.indexOf(matchStr);
  1619. fmt = fmt.replace(matchStr, regexStr);
  1620. dateUnits.push(new DateFormat(fmtName, '\\d'.repeat(matchStr.length), null, index));
  1621. }
  1622. else {
  1623. dateUnits.push(new DateFormat(fmtName, null, defaultDateValues.get(fmtName), -1));
  1624. }
  1625. }
  1626. // 进行验证是否真的是符合传入格式的字符串
  1627. fmt = fmt.replace(new RegExp('`', 'g'), '\\d');
  1628. if (!new RegExp(`^${fmt}$`).test(str)) {
  1629. return null;
  1630. }
  1631. // 进行一次排序, 依次对字符串进行截取
  1632. dateUnits
  1633. // 过滤掉没有得到格式化的对象
  1634. .filter(({ format }) => format)
  1635. // 按照字符串中日期片段的索引进行排序
  1636. .sort(function (a, b) {
  1637. return a.index - b.index;
  1638. })
  1639. // 获取到匹配的日期片段的值
  1640. .map(format => {
  1641. const matchDateUnit = new RegExp(format.format).exec(str);
  1642. if (matchDateUnit !== null && matchDateUnit.length > 0) {
  1643. str = str.replace(matchDateUnit[0], '');
  1644. format.value = matchDateUnit[0];
  1645. }
  1646. return format;
  1647. })
  1648. // 覆写到 dateStr 上面
  1649. .forEach(({ format }, i) => {
  1650. const matchDateUnit = new RegExp(format).exec(str);
  1651. if (matchDateUnit !== null && matchDateUnit.length > 0) {
  1652. str = str.replace(matchDateUnit[0], '');
  1653. dateUnits[i].value = matchDateUnit[0];
  1654. }
  1655. });
  1656. // 将截取完成的信息封装成对象并格式化标准的日期字符串
  1657. const map = arrayToMap(dateUnits, item => item.name, item => item.value);
  1658. if (map.get('year').length === 2) {
  1659. map.set('year', defaultDateValues
  1660. .get('year')
  1661. .substr(0, 2)
  1662. .concat(map.get('year')));
  1663. }
  1664. // 注意:此处使用的是本地时间而非 UTC 时间
  1665. const get = (unit) => parseInt(map.get(unit));
  1666. const year = get('year');
  1667. const month = get('month');
  1668. const day = get('day');
  1669. const hour = get('hour');
  1670. const minute = get('minute');
  1671. const second = get('second');
  1672. const millieSecond = get('millieSecond');
  1673. if (!isRange(month, 1, 12 + 1)) {
  1674. return null;
  1675. }
  1676. if (!isRange(day, 1, Reflect.get(monthDayValidate, month) +
  1677. (month === 2 && year % 4 === 0 ? 1 : 0) +
  1678. 1)) {
  1679. return null;
  1680. }
  1681. if (!isRange(hour, 0, 24 + 1) ||
  1682. !isRange(minute, 0, 60 + 1) ||
  1683. !isRange(second, 0, 60 + 1) ||
  1684. !isRange(millieSecond, 0, 999 + 1)) {
  1685. return null;
  1686. }
  1687. return new Date(year, month - 1, day, hour, minute, second, millieSecond);
  1688. }
  1689.  
  1690. /**
  1691. * 解析字符串为 Date 对象
  1692. * @deprecated 已弃用,请使用可读性更好的 {@link dateParse} 代替
  1693. * @param dateStr 日期字符串
  1694. * @param fmt 日期字符串的格式
  1695. * 目前仅支持使用 y(年),M(月),d(日),h(时),m(分),s(秒),S(毫秒)
  1696. * @returns 解析得到的 Date 对象
  1697. */
  1698. function strToDate(dateStr, fmt) {
  1699. return dateParse(dateStr, fmt);
  1700. }
  1701.  
  1702. /**
  1703. * 复制一段文本内容
  1704. * @param text 要进行复制的文本
  1705. * @returns 是否复制成功
  1706. */
  1707. function copyText(text) {
  1708. const $el = document.createElement('textarea');
  1709. $el.style.position = 'fixed';
  1710. $el.style.top = '-1000px';
  1711. document.body.appendChild($el);
  1712. $el.value = text;
  1713. $el.select();
  1714. const res = document.execCommand('copy');
  1715. document.body.removeChild($el);
  1716. return res;
  1717. }
  1718.  
  1719. /**
  1720. * 根据 html 字符串创建 Element 元素
  1721. * @param str html 字符串
  1722. * @returns 创建的 Element 元素
  1723. */
  1724. function createElByString(str) {
  1725. const root = document.createElement('div');
  1726. root.innerHTML = str;
  1727. return root.querySelector('*');
  1728. }
  1729.  
  1730. /**
  1731. * 获取输入框中光标所在位置
  1732. * @param {HTMLFormElement} el 需要获取的输入框元素
  1733. * @returns 光标所在位置的下标
  1734. */
  1735. function getCursorPosition(el) {
  1736. return el.selectionStart;
  1737. }
  1738.  
  1739. /**
  1740. * 获取输入框中光标所在位置
  1741. * @param {HTMLFormElement} el 需要获取的输入框元素
  1742. * @returns 光标所在位置的下标
  1743. * @deprecated 已废弃,请使用正确更名后的 {@link getCursorPosition} 函数
  1744. */
  1745. function getCusorPostion(el) {
  1746. return getCursorPosition(el);
  1747. }
  1748.  
  1749. /**
  1750. * 设置输入框中选中的文本/光标所在位置
  1751. * @param el 需要设置的输入框元素
  1752. * @param start 光标所在位置的下标
  1753. * @param end 结束位置,默认为输入框结束
  1754. */
  1755. function setCursorPosition(el, start, end = start) {
  1756. el.focus();
  1757. el.setSelectionRange(start, end);
  1758. }
  1759.  
  1760. /**
  1761. * 在指定位置后插入文本
  1762. * @param el 需要设置的输入框元素
  1763. * @param text 要插入的值
  1764. * @param start 开始位置,默认为当前光标处
  1765. */
  1766. function insertText(el, text, start = getCursorPosition(el)) {
  1767. const value = el.value;
  1768. el.value = value.substr(0, start) + text + value.substr(start);
  1769. setCursorPosition(el, start + text.length);
  1770. }
  1771.  
  1772. /**
  1773. * 字符串安全的转换为小写
  1774. * @param str 字符串
  1775. * @returns 转换后得到的全小写字符串
  1776. */
  1777. function toLowerCase(str) {
  1778. if (isNullOrUndefined(str) || typeof str !== 'string') {
  1779. return str;
  1780. }
  1781. return str.toLowerCase();
  1782. }
  1783.  
  1784. /**
  1785. * 判断指定元素是否是可编辑元素
  1786. * 注:可编辑元素并不一定能够进行编辑,例如只读的 input 元素
  1787. * @param el 需要进行判断的元素
  1788. * @returns 是否为可编辑元素
  1789. */
  1790. function isEditable(el) {
  1791. const inputEls = ['input', 'date', 'datetime', 'select', 'textarea'];
  1792. return (
  1793. // 此处需要判断是否存在属性 isContentEditable
  1794. // @ts-ignore
  1795. el && (el.isContentEditable || inputEls.includes(toLowerCase(el.tagName))));
  1796. }
  1797.  
  1798. let lastFocusEl;
  1799. /**
  1800. * 获取到最后一个获得焦点的元素
  1801. * @returns 最后一个获取到焦点的元素
  1802. */
  1803. function _lastFocus() {
  1804. return lastFocusEl;
  1805. }
  1806. const lastFocus = Object.assign(_lastFocus, {
  1807. init() {
  1808. document.addEventListener('focus', event => {
  1809. lastFocusEl = event.target;
  1810. }, true);
  1811. document.addEventListener('blur', () => {
  1812. lastFocusEl = null;
  1813. }, true);
  1814. },
  1815. });
  1816.  
  1817. /**
  1818. * 直接删除指定元素
  1819. * @param el 需要删除的元素
  1820. * @returns 返回被删除的元素
  1821. */
  1822. function removeEl(el) {
  1823. const parent = el.parentElement;
  1824. if (parent == null) {
  1825. return null;
  1826. }
  1827. return parent.removeChild(el);
  1828. }
  1829.  
  1830. /**
  1831. * 在指定范围内删除文本
  1832. * @param el 需要设置的输入框元素
  1833. * @param start 开始位置,默认为当前选中开始位置
  1834. * @param end 结束位置,默认为当前选中结束位置
  1835. */
  1836. function removeText(el, start = el.selectionStart, end = el.selectionEnd) {
  1837. // 删除之前必须要 [记住] 当前光标的位置
  1838. const index = getCursorPosition(el);
  1839. const value = el.value;
  1840. el.value = value.substr(0, start) + value.substr(end, value.length);
  1841. setCursorPosition(el, index);
  1842. }
  1843.  
  1844. /**
  1845. * 设置输入框中选中的文本/光标所在位置
  1846. * @param el 需要设置的输入框元素
  1847. * @param start 光标所在位置的下标
  1848. * @param end 结束位置,默认为输入框结束
  1849. * @deprecated 已废弃,请使用正确更名后的 {@link setCursorPosition} 函数
  1850. */
  1851. function setCusorPostion(el, start, end = start) {
  1852. return setCursorPosition(el, start, end);
  1853. }
  1854.  
  1855. /**
  1856. * 监听 event 的添加/删除,使 DOM 事件是可撤销的
  1857. * 注:必须及早运行,否则无法监听之前添加的事件
  1858. * @deprecated 实际上 {@link EventUtil} 已经更好的实现了这个功能,如果需要则直接修改原型即可,无需使用该函数
  1859. */
  1860. function watchEventListener() {
  1861. /**
  1862. * 用来保存监听到的事件信息
  1863. */
  1864. class Event {
  1865. constructor(el, type, listener, useCapture) {
  1866. this.el = el;
  1867. this.type = type;
  1868. this.listener = listener;
  1869. this.useCapture = useCapture;
  1870. }
  1871. }
  1872. /**
  1873. * 监听所有的 addEventListener, removeEventListener 事件
  1874. */
  1875. const documentAddEventListener = document.addEventListener;
  1876. const eventTargetAddEventListener = EventTarget.prototype.addEventListener;
  1877. const documentRemoveEventListener = document.removeEventListener;
  1878. const eventTargetRemoveEventListener = EventTarget.prototype.removeEventListener;
  1879. const events = [];
  1880. /**
  1881. * 自定义的添加事件监听函数
  1882. * @param type 事件类型
  1883. * @param listener 事件监听函数
  1884. * @param [useCapture] 是否需要捕获事件冒泡,默认为 false
  1885. */
  1886. function addEventListener(type, listener, useCapture = false) {
  1887. const $addEventListener =
  1888. // @ts-ignore
  1889. this === document ? documentAddEventListener : eventTargetAddEventListener;
  1890. // @ts-ignore
  1891. events.push(new Event(this, type, listener, useCapture));
  1892. // @ts-ignore
  1893. $addEventListener.apply(this, arguments);
  1894. }
  1895. /**
  1896. * 自定义的根据类型删除事件函数
  1897. * 该方法会删除这个类型下面全部的监听函数,不管数量
  1898. * @param type 事件类型
  1899. */
  1900. function removeEventListenerByType(type) {
  1901. const $removeEventListener =
  1902. // @ts-ignore
  1903. this === document
  1904. ? documentRemoveEventListener
  1905. : eventTargetRemoveEventListener;
  1906. const removeIndexList = events
  1907. // @ts-ignore
  1908. .map((e, i) => (e.el === this || e.type === arguments[0] ? i : -1))
  1909. .filter(i => i !== -1);
  1910. removeIndexList.forEach(i => {
  1911. const e = events[i];
  1912. $removeEventListener.apply(e.el, [e.type, e.listener, e.useCapture]);
  1913. });
  1914. removeIndexList.sort((a, b) => b - a).forEach(i => events.splice(i, 1));
  1915. }
  1916. document.addEventListener = EventTarget.prototype.addEventListener = addEventListener;
  1917. // @ts-ignore
  1918. document.removeEventListenerByType = EventTarget.prototype.removeEventListenerByType = removeEventListenerByType;
  1919. }
  1920.  
  1921. /**
  1922. * 将任意对象转换为 String
  1923. * 主要避免原生 Object toString 遇到某些空值的时候抛异常的问题
  1924. * @param object 任意对象
  1925. * @returns 字符串
  1926. */
  1927. function toString$1(object) {
  1928. if (isNullOrUndefined(object)) {
  1929. return '';
  1930. }
  1931. if (object instanceof Date) {
  1932. return object.toISOString();
  1933. }
  1934. return object.toString();
  1935. }
  1936.  
  1937. /**
  1938. * FormData 批量添加方法
  1939. * 注:该方法不会覆盖掉原本的属性
  1940. * @param fd FormData 对象
  1941. * @param obj 键值对对象
  1942. * @returns 添加完成后的 FormData 对象
  1943. */
  1944. function appends(fd, obj) {
  1945. for (const k in obj) {
  1946. const v = obj[k];
  1947. fd.append(k, toString$1(v));
  1948. }
  1949. return fd;
  1950. }
  1951.  
  1952. /**
  1953. * FormData 批量删除方法
  1954. * @param fd FormData 对象
  1955. * @param keys 删除的 key 列表
  1956. * @returns 返回删除后的 FormData 对象
  1957. */
  1958. function deletes(fd, keys) {
  1959. keys.forEach(key => fd.delete(key));
  1960. return fd;
  1961. }
  1962.  
  1963. /**
  1964. * FormData 批量设置方法
  1965. * 注:该方法会覆盖掉原本的属性
  1966. * @param fd 表单对象
  1967. * @param obj 键值对对象
  1968. * @returns 设置完成后的 FormData 对象
  1969. */
  1970. function sets(fd, obj) {
  1971. for (const k in obj) {
  1972. fd.set(k, obj[k]);
  1973. }
  1974. return fd;
  1975. }
  1976.  
  1977. /**
  1978. * FormData 转换为包含所有键值数组的二维数组函数
  1979. *
  1980. * @param fd 需要转换的 FormData 对象
  1981. * @returns 转换后的数组
  1982. * @deprecated 已被原生函数 Array.from 取代
  1983. */
  1984. function formDataToArray(fd) {
  1985. // @ts-ignore
  1986. return Array.from(fd);
  1987. }
  1988.  
  1989. /**
  1990. * 将参数对象转换为 FormData,只转换一层
  1991. * @param data 参数对象
  1992. * @return {FormData} 转换后的表单对象
  1993. */
  1994. function objToFormData(data) {
  1995. return Object.entries(data).reduce((res, [k, v]) => {
  1996. if (v instanceof Blob) {
  1997. res.append(k, v);
  1998. }
  1999. else {
  2000. res.append(k, v && v.toString());
  2001. }
  2002. return res;
  2003. }, new FormData());
  2004. }
  2005.  
  2006. /**
  2007. * 函数去抖
  2008. * 去抖 (debounce) 去抖就是对于一定时间段的连续的函数调用,只让其执行一次
  2009. * 注: 包装后的函数如果两次操作间隔小于 delay 则不会被执行, 如果一直在操作就会一直不执行, 直到操作停止的时间大于 delay 最小间隔时间才会执行一次, 不管任何时间调用都需要停止操作等待最小延迟时间
  2010. * 应用场景主要在那些连续的操作, 例如页面滚动监听, 包装后的函数只会执行最后一次
  2011. * 注: 该函数第一次调用一定不会执行,第一次一定拿不到缓存值,后面的连续调用都会拿到上一次的缓存值。如果需要在第一次调用获取到的缓存值,则需要传入第三个参数 {@param init},默认为 {@code undefined} 的可选参数
  2012. * 注: 返回函数结果的高阶函数需要使用 {@see Proxy} 实现,以避免原函数原型链上的信息丢失
  2013. *
  2014. * @param delay 最小延迟时间,单位为 ms
  2015. * @param action 真正需要执行的操作
  2016. * @param init 初始的缓存值,不填默认为 {@see undefined}
  2017. * @return 包装后有去抖功能的函数。该函数是异步的,与需要包装的函数 {@see action} 是否异步没有太大关联
  2018. */
  2019. function debounce(delay, action, init = null) {
  2020. let flag;
  2021. let result = init;
  2022. return new Proxy(action, {
  2023. apply(_, _this, args) {
  2024. return new Promise(resolve => {
  2025. if (flag)
  2026. clearTimeout(flag);
  2027. flag = setTimeout(() => resolve((result = Reflect.apply(_, _this, args))), delay);
  2028. setTimeout(() => resolve(result), delay);
  2029. });
  2030. },
  2031. });
  2032. }
  2033.  
  2034. /**
  2035. * 使用 Proxy 实现通用的单例模式
  2036. * @param clazz 需要包装为单例的类型
  2037. * @returns 包装后的单例模式类,使用 {@code new} 创建将只在第一次有效
  2038. */
  2039. function singleModel(clazz) {
  2040. let instance;
  2041. return new Proxy(clazz, {
  2042. construct(target, args, newTarget) {
  2043. if (instance === undefined) {
  2044. instance = Reflect.construct(target, args, newTarget);
  2045. }
  2046. return instance;
  2047. },
  2048. });
  2049. }
  2050.  
  2051. /**
  2052. * 状态机
  2053. * 用于避免使用 if-else 的一种方式
  2054. * @typeparam K 状态的类型,默认为 any
  2055. * @typeparam V 构造函数返回值的类型,一般为实现子类的基类,默认为 any
  2056. * @deprecated 该类将在下个大版本进行重构,使用函数而非类作为基本单元
  2057. */
  2058. class StateMachine {
  2059. constructor() {
  2060. this.classMap = new Map();
  2061. }
  2062. /**
  2063. * 获取到一个状态工厂
  2064. * @deprecated 已废弃,请直接创建一个 StateMachine 实例
  2065. */
  2066. static getFactory() {
  2067. /**
  2068. * 状态注册器
  2069. * 更好的有限状态机,分离子类与构建的关系,无论子类如何增删该都不影响基类及工厂类
  2070. */
  2071. return new StateMachine();
  2072. }
  2073. /**
  2074. * 注册一个 class,创建子类时调用,用于记录每一个 [状态 => 子类] 对应
  2075. * 注: 此处不再默认使用单例模式,如果需要,请自行对 class 进行包装
  2076. * @param state 作为键的状态
  2077. * @param clazz 对应的子类型
  2078. * @returns 返回 clazz 本身
  2079. */
  2080. register(state, clazz) {
  2081. this.classMap.set(state, clazz);
  2082. return clazz;
  2083. }
  2084. /**
  2085. * 获取一个标签子类对象
  2086. * @param state 状态索引
  2087. * @param args 构造函数的参数
  2088. * @returns 子类对象
  2089. */
  2090. getInstance(state, ...args) {
  2091. const Class = this.classMap.get(state);
  2092. if (!Class) {
  2093. return null;
  2094. }
  2095. // 构造函数的参数
  2096. return new Class(...args);
  2097. }
  2098. /**
  2099. * 允许使用 for-of 遍历整个状态机
  2100. */
  2101. *[Symbol.iterator]() {
  2102. for (const kv of this.classMap.entries()) {
  2103. yield kv;
  2104. }
  2105. }
  2106. }
  2107.  
  2108. /**
  2109. * 函数节流
  2110. * 节流 (throttle) 让一个函数不要执行的太频繁,减少执行过快的调用,叫节流
  2111. * 类似于上面而又不同于上面的函数去抖, 包装后函数在上一次操作执行过去了最小间隔时间后会直接执行, 否则会忽略该次操作
  2112. * 与上面函数去抖的明显区别在连续操作时会按照最小间隔时间循环执行操作, 而非仅执行最后一次操作
  2113. * 注: 该函数第一次调用一定会执行,不需要担心第一次拿不到缓存值,后面的连续调用都会拿到上一次的缓存值
  2114. * 注: 返回函数结果的高阶函数需要使用 {@see Proxy} 实现,以避免原函数原型链上的信息丢失
  2115. *
  2116. * @param delay 最小间隔时间,单位为 ms
  2117. * @param action 真正需要执行的操作
  2118. * @return {Function} 包装后有节流功能的函数。该函数是异步的,与需要包装的函数 {@link action} 是否异步没有太大关联
  2119. */
  2120. function throttle(delay, action) {
  2121. let last = 0;
  2122. let result;
  2123. return new Proxy(action, {
  2124. apply(target, thisArg, args) {
  2125. return new Promise(resolve => {
  2126. const curr = Date.now();
  2127. if (curr - last > delay) {
  2128. result = Reflect.apply(target, thisArg, args);
  2129. last = curr;
  2130. resolve(result);
  2131. return;
  2132. }
  2133. resolve(result);
  2134. });
  2135. },
  2136. });
  2137. }
  2138.  
  2139. /**
  2140. * 测试函数的执行时间
  2141. * 注:如果函数返回 Promise,则该函数也会返回 Promise,否则直接返回执行时间
  2142. * @param fn 需要测试的函数
  2143. * @returns 执行的毫秒数
  2144. */
  2145. function timing(fn) {
  2146. const begin = performance.now();
  2147. const res = fn();
  2148. return compatibleAsync(res, () => performance.now() - begin);
  2149. }
  2150.  
  2151. /**
  2152. * 轮询等待指定资源加载完毕再执行操作
  2153. * 使用 Promises 实现,可以使用 ES7 的 {@see async} 和 {@see await} 调用
  2154. * @param fn 判断必须的资源是否存在的方法
  2155. * @param option 可配置项
  2156. * @returns Promise 对象
  2157. */
  2158. function waitResource(fn, { interval = 100, max = 10 } = {}) {
  2159. let current = 0;
  2160. return new Promise((resolve, reject) => {
  2161. const timer = setInterval(() => {
  2162. if (fn()) {
  2163. clearInterval(timer);
  2164. resolve();
  2165. }
  2166. current++;
  2167. if (current >= max) {
  2168. clearInterval(timer);
  2169. reject(new Error('waitResource call timeout'));
  2170. }
  2171. }, interval);
  2172. });
  2173. }
  2174.  
  2175. /**
  2176. * 监视指定函数返回值的变化
  2177. * @param fn 需要监视的函数
  2178. * @param callback 回调函数
  2179. * @param interval 每次检查的间隔时间,默认为 100ms
  2180. * @returns 关闭这个监视函数
  2181. */
  2182. function watch(fn, callback, interval = 100) {
  2183. let oldVal = fn();
  2184. const timer = setInterval(() => {
  2185. const newVal = fn();
  2186. if (oldVal !== newVal) {
  2187. callback(newVal, oldVal);
  2188. oldVal = newVal;
  2189. }
  2190. }, interval);
  2191. return () => clearInterval(timer);
  2192. }
  2193.  
  2194. /**
  2195. * 深度监听指定对象属性的变化
  2196. * 注:指定对象不能是原始类型,即不可变类型,而且对象本身的引用不能改变,最好使用 const 进行声明
  2197. * @param object 需要监视的对象
  2198. * @param callback 当代理对象发生改变时的回调函数,回调函数有三个参数,分别是对象,修改的 key,修改的 v
  2199. * @returns 返回源对象的一个代理
  2200. */
  2201. function watchObject(object, callback) {
  2202. const handler = {
  2203. get(target, k) {
  2204. try {
  2205. // 注意: 这里很关键,它为对象的字段也添加了代理
  2206. return new Proxy(Reflect.get(target, k), handler);
  2207. }
  2208. catch (err) {
  2209. return Reflect.get(target, k);
  2210. }
  2211. },
  2212. set(target, k, v) {
  2213. callback(target, k, v);
  2214. return Reflect.set(target, k, v);
  2215. },
  2216. };
  2217. return new Proxy(object, handler);
  2218. }
  2219.  
  2220. /**
  2221. * 填充字符串到指定长度
  2222. * @param item 填充的字符串
  2223. * @param len 填充的长度
  2224. * @returns 填充完成的字符串
  2225. * @deprecated 已废弃,请使用 ES6 {@link String.prototype.repeat} 函数
  2226. * 具体请参考 MDN {@url(https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/repeat)}
  2227. */
  2228. function fill(item, len) {
  2229. if (len <= 0) {
  2230. return '';
  2231. }
  2232. return item + fill(item, len - 1);
  2233. }
  2234.  
  2235. /**
  2236. * 字符串格式化
  2237. *
  2238. * @param str 要进行格式化的值
  2239. * @param args 格式化参数值,替换字符串中的 {} 的值
  2240. * @returns 替换完成的字符串
  2241. * @deprecated 已废弃,请使用 ES6 模板字符串 {@url(https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/template_strings)}
  2242. */
  2243. function format(str, args) {
  2244. return Object.keys(args).reduce((res, k) => res.replace(new RegExp(`{${k}}`, 'g'), toString$1(args[k])), str);
  2245. }
  2246.  
  2247. /**
  2248. * 判断字符串是否位小数
  2249. * @param str 需要进行判断的字符串
  2250. * @returns 是否为小数
  2251. * @deprecated 已废弃,请使用 {@link stringValidator#isFloat}
  2252. */
  2253. function isFloat(str) {
  2254. return stringValidator.isFloat(str);
  2255. }
  2256.  
  2257. /**
  2258. * 判断字符串是否位整数
  2259. * @param str 需要进行判断的字符串
  2260. * @returns 是否为小数
  2261. * @deprecated 已废弃,请使用 {@link stringValidator#isInteger}
  2262. */
  2263. function isNumber(str) {
  2264. return stringValidator.isInteger(str);
  2265. }
  2266.  
  2267. /**
  2268. * 字符串安全的转换为大写
  2269. * @param str 字符串
  2270. * @returns 转换后得到的全大写字符串
  2271. */
  2272. function toUpperCase(str) {
  2273. if (isNullOrUndefined(str) || typeof str !== 'string') {
  2274. return str;
  2275. }
  2276. return str.toUpperCase();
  2277. }
  2278.  
  2279. /**
  2280. * 将空白字符串转换为 null
  2281. *
  2282. * @param str 将空字符串转换为 {@code null}
  2283. * @returns 可能为 {@code null}
  2284. */
  2285. function blankToNull(str) {
  2286. return StringValidator.isBlank(str) ? null : str;
  2287. }
  2288.  
  2289. /**
  2290. * 置空对象所有空白的属性
  2291. * @param obj 对象
  2292. * @returns 将所有的空白属性全部转换为 null 的新对象
  2293. */
  2294. function blankToNullField(obj) {
  2295. return Object.keys(obj).reduce((res, k) => {
  2296. const v = Reflect.get(obj, k);
  2297. Reflect.set(res, k, typeof v === 'string' ? blankToNull(v) : v);
  2298. return res;
  2299. }, {});
  2300. }
  2301.  
  2302. /**
  2303. * 将对象的所有属性置空
  2304. * @param obj 需要置空属性的对象
  2305. * @returns 返回一个新的对象
  2306. */
  2307. function emptyAllField(obj) {
  2308. return Object.keys(obj).reduce((res, k) => {
  2309. Reflect.set(res, k, null);
  2310. return res;
  2311. }, {});
  2312. }
  2313.  
  2314. /**
  2315. * 排除对象中的指定字段
  2316. * 注: 此处将获得一个浅拷贝对象
  2317. * @param obj 排除对象
  2318. * @param fields 要排除的多个字段
  2319. * @returns 排除完指定字段得到的新的对象
  2320. */
  2321. function excludeFields(obj, ...fields) {
  2322. const set = new Set(fields);
  2323. return Object.keys(obj).reduce((res, k) => {
  2324. if (!set.has(k)) {
  2325. Reflect.set(res, k, Reflect.get(obj, k));
  2326. }
  2327. return res;
  2328. }, {});
  2329. }
  2330.  
  2331. /**
  2332. * 将 Map 转换为 Object 对象
  2333. * @param map Map 键值表
  2334. * @returns 转换得到的 Object 对象
  2335. */
  2336. function mapToObject(map) {
  2337. const res = {};
  2338. for (const [k, v] of map) {
  2339. Reflect.set(res, k, v);
  2340. }
  2341. return res;
  2342. }
  2343.  
  2344. function randomInt(num1, num2) {
  2345. const min = num2 ? num1 : 0;
  2346. const max = num2 ? num2 : num1;
  2347. if (max <= 0) {
  2348. throw new Error('最大值不能为 0');
  2349. }
  2350. return min + Math.floor(Math.random() * (max - min));
  2351. }
  2352.  
  2353. /**
  2354. * 计算月有多少天
  2355. * @param date 日期
  2356. * @returns 月的总天数
  2357. */
  2358. function calcMonEndDay(date) {
  2359. const monthToDay = [
  2360. [new Set([1, 3, 5, 7, 8, 10, 12]), 30],
  2361. [new Set([4, 6, 9, 11]), 30],
  2362. [new Set([2]), 28],
  2363. ];
  2364. const year = date.getFullYear();
  2365. const month = date.getMonth() + 1;
  2366. const days = monthToDay.find(([monthSet]) => monthSet.has(month))[1];
  2367. return days + (month === 2 && year % 4 === 0 ? 1 : 0);
  2368. }
  2369. /**
  2370. * 日期固定时间点
  2371. */
  2372. class DateConstants {
  2373. /**
  2374. * 获取指定日期一天的开始时间
  2375. * @param date 指定的时间,默认为当前日期
  2376. * @returns 一天的开始时间
  2377. */
  2378. static dayStart(date = new Date()) {
  2379. return new Date(`${dateFormat(date, 'yyyy-MM-dd')}T00:00:00.000`);
  2380. }
  2381. /**
  2382. * 获取指定日期一天的结束时间
  2383. * @param date 指定的时间,默认为当前日期
  2384. * @returns 一天的结束时间
  2385. */
  2386. static dayEnd(date = new Date()) {
  2387. return new Date(`${dateFormat(date, 'yyyy-MM-dd')}T23:59:59.999`);
  2388. }
  2389. /**
  2390. * 获取指定日期所在月的开始时间
  2391. * @param date 指定的时间,默认为当前日期
  2392. * @returns 月的开始时间
  2393. */
  2394. static monthStart(date = new Date()) {
  2395. return new Date(`${dateFormat(date, 'yyyy-MM')}-01T00:00:00.000`);
  2396. }
  2397. /**
  2398. * 获取指定日期所在月的结束时间
  2399. * @param date 指定的时间,默认为当前日期
  2400. * @returns 月的结束时间
  2401. */
  2402. static monthEnd(date = new Date()) {
  2403. return new Date(`${dateFormat(date, 'yyyy-MM')}-${calcMonEndDay(date)}T23:59:59.999`);
  2404. }
  2405. /**
  2406. * 获取指定日期所在年份的新年开始时间
  2407. * @param date 指定的时间,默认为当前日期
  2408. * @returns 新年开始时间
  2409. */
  2410. static yearStart(date = new Date()) {
  2411. return new Date(`${date.getFullYear()}-01-01T00:00:00.000`);
  2412. }
  2413. /**
  2414. * 获取指定日期所在年份的旧年结束时间
  2415. * @param date 指定的时间,默认为当前日期
  2416. * @returns 旧年结束时间
  2417. */
  2418. static yearEnd(date = new Date()) {
  2419. return new Date(`${date.getFullYear()}-12-31T23:59:59.999`);
  2420. }
  2421. }
  2422. /**
  2423. * 导出一个日期固定时间点的对象
  2424. * @deprecated 已废弃,请直接使用类的静态函数
  2425. */
  2426. const dateConstants = DateConstants;
  2427.  
  2428. /**
  2429. * 一天标准的毫秒数
  2430. */
  2431. const DAY_UNIT_TIME = 1000 * 60 * 60 * 24;
  2432. /**
  2433. * 日期增强
  2434. */
  2435. class DateEnhance {
  2436. /**
  2437. * 构造函数
  2438. * @param date 要增强的日期
  2439. */
  2440. constructor(date) {
  2441. this.date = date;
  2442. }
  2443. /**
  2444. * 获取到年份
  2445. * @returns
  2446. */
  2447. year() {
  2448. return this.date.getFullYear();
  2449. }
  2450. /**
  2451. * 获取月份
  2452. * @returns
  2453. * @deprecated 已废弃,请使用 {@link this#monthOfYear} 函数
  2454. */
  2455. month() {
  2456. return this.date.getMonth();
  2457. }
  2458. /**
  2459. * 获取今年的第几个月份
  2460. * 和 {@link this#month} 不同的是不再从 0 计算月份
  2461. */
  2462. monthOfYear() {
  2463. return this.date.getMonth() + 1;
  2464. }
  2465. /**
  2466. * 获取一年内的第多少天
  2467. * 注: 这个天数指定的在第几天而非过去了多少天,例如 2018-01-10 的结果会是 10
  2468. * @returns
  2469. */
  2470. dayOfYear() {
  2471. return Math.ceil((this.date.getTime() - dateConstants.yearStart(this.date).getTime()) /
  2472. DAY_UNIT_TIME);
  2473. }
  2474. /**
  2475. * 获取一个月内的第多少天
  2476. * 注: 这个天数指的是在第几天而非过去了多少天,例如 2018-01-10 的结果会是 10
  2477. * @returns
  2478. */
  2479. dayOfMonth() {
  2480. return this.date.getDate();
  2481. }
  2482. /**
  2483. * 获取一个星期内的第多少天
  2484. * @returns
  2485. */
  2486. dayOfWeek() {
  2487. return this.date.getDay();
  2488. }
  2489. /**
  2490. * 获取一年内的第多少星期
  2491. * 注: 这个星期指定的在第几天而非过去了多少天,例如 2018-01-10 的结果会是 10
  2492. * @returns
  2493. */
  2494. weekOfYear() {
  2495. return Math.ceil(this.dayOfYear() / 7);
  2496. }
  2497. /**
  2498. * 获取一个月内的第多少星期
  2499. * @returns
  2500. */
  2501. weekOfMonth() {
  2502. return Math.ceil(this.dayOfMonth() / 7);
  2503. }
  2504. /**
  2505. * 获取季度
  2506. * @returns
  2507. */
  2508. quarter() {
  2509. const month = this.month();
  2510. if (isRange(month, 0, 3)) {
  2511. return 1;
  2512. }
  2513. else if (isRange(month, 3, 6)) {
  2514. return 2;
  2515. }
  2516. else if (isRange(month, 6, 9)) {
  2517. return 3;
  2518. }
  2519. else {
  2520. return 4;
  2521. }
  2522. }
  2523. /**
  2524. * 获取小时
  2525. * @returns
  2526. */
  2527. hour() {
  2528. return this.date.getHours();
  2529. }
  2530. /**
  2531. * 获取分钟
  2532. * @returns
  2533. */
  2534. minute() {
  2535. return this.date.getMinutes();
  2536. }
  2537. /**
  2538. * 获取秒
  2539. * @returns
  2540. */
  2541. second() {
  2542. return this.date.getSeconds();
  2543. }
  2544. /**
  2545. * 获取毫秒
  2546. * @returns
  2547. */
  2548. milliSecond() {
  2549. return this.date.getMilliseconds();
  2550. }
  2551. }
  2552. /**
  2553. * 获取一个增强的日期
  2554. * @param date 要增强的日期
  2555. * @returns 增强日期
  2556. */
  2557. function dateEnhance(date) {
  2558. return new DateEnhance(date);
  2559. }
  2560.  
  2561. /**
  2562. * 获取一年内的第多少星期
  2563. * @param date 日期
  2564. * @returns 这个日期第多少个星期
  2565. * @deprecated 不推荐使用,请使用 {@see dateEnhance} 代替
  2566. */
  2567. function getYearWeek(date) {
  2568. return dateEnhance(date).weekOfYear();
  2569. }
  2570.  
  2571. /**
  2572. * 时间日期间隔
  2573. */
  2574. class DateBetween {
  2575. /**
  2576. * 构造函数
  2577. * @param start 开始时间
  2578. * @param end 结束时间
  2579. */
  2580. constructor(start, end) {
  2581. this.start = start;
  2582. this.end = end;
  2583. }
  2584. /**
  2585. * 获取毫秒差值
  2586. * @returns 毫秒差值
  2587. */
  2588. milliSecond() {
  2589. return this.end.getTime() - this.start.getTime();
  2590. }
  2591. /**
  2592. * 获取秒差值
  2593. * @returns 秒差值
  2594. */
  2595. second() {
  2596. return Math.floor(this.milliSecond() / 1000);
  2597. }
  2598. /**
  2599. * 获取分钟差值
  2600. * @returns 分钟差值
  2601. */
  2602. minute() {
  2603. return Math.floor(this.second() / 60);
  2604. }
  2605. /**
  2606. * 获取小时差值
  2607. * @returns 小时差值
  2608. */
  2609. hour() {
  2610. return Math.floor(this.minute() / 60);
  2611. }
  2612. /**
  2613. * 获取天数差值
  2614. * @returns 天数差值
  2615. */
  2616. day() {
  2617. return Math.floor(this.hour() / 24);
  2618. }
  2619. /**
  2620. * 获取月份差值
  2621. * 注: 此处获取的差值是按月计算的,即 2018-12-31 => 2019-01-01 也被认为相差一个月
  2622. * @returns 月份差值
  2623. */
  2624. month() {
  2625. const year = this.year();
  2626. const month = this.end.getMonth() - this.start.getMonth();
  2627. return year * 12 + month;
  2628. }
  2629. /**
  2630. * 获取年份差值
  2631. * 注: 此处获取的差值是按年计算的,即 2018-12-31 => 2019-01-01 也被认为相差一年
  2632. * @returns 年份差值
  2633. */
  2634. year() {
  2635. return this.end.getFullYear() - this.start.getFullYear();
  2636. }
  2637. }
  2638. /**
  2639. * 获取两个时间的差值
  2640. * @param start 开始时间
  2641. * @param end 结束时间
  2642. * @returns 差值对象
  2643. */
  2644. function dateBetween(start, end) {
  2645. return new DateBetween(start, end);
  2646. }
  2647.  
  2648. /**
  2649. * 返回合理参数本身的函数
  2650. * 1. 如果没有参数则返回 undefined
  2651. * 2. 如果只有一个参数则返回参数本身
  2652. * 3. 如果有两个以上的参数则返回参数列表
  2653. * @param args 任何对象
  2654. * @returns 传入的参数
  2655. * @deprecated 已废弃,貌似没有太多的使用场景
  2656. */
  2657. function returnReasonableItself(...args) {
  2658. const len = args.length;
  2659. if (len === 0) {
  2660. return null;
  2661. }
  2662. if (len === 1) {
  2663. return args[0];
  2664. }
  2665. return args;
  2666. }
  2667.  
  2668. /**
  2669. * 从数组中移除指定的元素
  2670. * 注: 时间复杂度为 1~3On
  2671. * @param arr 需要被过滤的数组
  2672. * @param deleteItems 要过滤的元素数组
  2673. * @param k 每个元素的唯一键函数
  2674. */
  2675. function filterItems(arr, deleteItems, k = returnItself) {
  2676. const kFn = getKFn(k);
  2677. const kSet = new Set(deleteItems.map(kFn));
  2678. return arr.filter((v, i, arr) => !kSet.has(kFn(v, i, arr)));
  2679. }
  2680.  
  2681. /**
  2682. * 比较两个数组的差异
  2683. * @param left 第一个数组
  2684. * @param right 第二个数组
  2685. * @param k 每个元素的唯一标识产生函数
  2686. * @returns 比较的差异结果
  2687. */
  2688. function diffBy(left, right, k = returnItself) {
  2689. const kFn = getKFn(k);
  2690. // 首先得到两个 kSet 集合用于过滤
  2691. const kThanSet = new Set(left.map(kFn));
  2692. const kThatSet = new Set(right.map(kFn));
  2693. const leftUnique = left.filter((v, ...args) => !kThatSet.has(kFn(v, ...args)));
  2694. const rightUnique = right.filter((v, ...args) => !kThanSet.has(kFn(v, ...args)));
  2695. const kLeftSet = new Set(leftUnique.map(kFn));
  2696. const common = left.filter((v, ...args) => !kLeftSet.has(kFn(v, ...args)));
  2697. return { left: leftUnique, right: rightUnique, common };
  2698. }
  2699.  
  2700. /**
  2701. * 比较两个数组的差异
  2702. * @deprecated 已废弃,请使用更简洁的 {@link diffBy}
  2703. */
  2704. const arrayDiffBy = diffBy;
  2705.  
  2706. /**
  2707. * 使用 Generator 实现一个从 0 开始的无限自增序列
  2708. */
  2709. function* autoIncrementGenerator() {
  2710. for (let i = 0;; i++) {
  2711. /**
  2712. * @returns 每次获取都返回循环中的当前迭代变量,然后暂停于此处
  2713. */
  2714. yield i;
  2715. }
  2716. }
  2717. /**
  2718. * 生成器对象
  2719. */
  2720. const generator = autoIncrementGenerator();
  2721. /**
  2722. * 获取自增长序列的最新值
  2723. * @returns 最新值
  2724. */
  2725. function autoIncrement() {
  2726. return generator.next().value;
  2727. }
  2728.  
  2729. /**
  2730. * 转换接口
  2731. * @interface
  2732. */
  2733. class IConverter {
  2734. /**
  2735. * 将字符串解析为字符串列表
  2736. *
  2737. * @param str 字符串
  2738. * @return {Array.<String>} 字符串列表
  2739. * @abstract
  2740. */
  2741. from(str) {
  2742. throw new Error('子类必须重写 from 函数');
  2743. }
  2744. /**
  2745. * 将字符串列表构造为字符串
  2746. *
  2747. * @param list 字符串列表
  2748. * @return {String} 字符串
  2749. * @abstract
  2750. */
  2751. to(list) {
  2752. throw new Error('子类必须重写 to 函数');
  2753. }
  2754. }
  2755.  
  2756. /**
  2757. * 驼峰风格解析
  2758. */
  2759. class CamelOrPascalFrom extends IConverter {
  2760. /**
  2761. * 将字符串解析为字符串列表
  2762. *
  2763. * @param str 字符串
  2764. * @return {Array.<String>} 字符串列表
  2765. * @override
  2766. */
  2767. from(str) {
  2768. const result = [];
  2769. const len = str.length;
  2770. let old = 0;
  2771. for (let i = 0; i < len; i++) {
  2772. const c = str.charAt(i);
  2773. if (c >= 'A' && c <= 'Z') {
  2774. if (i !== 0) {
  2775. result.push(str.substring(old, i));
  2776. }
  2777. old = i;
  2778. }
  2779. }
  2780. if (old !== str.length) {
  2781. result.push(str.substring(old, str.length));
  2782. }
  2783. return result;
  2784. }
  2785. }
  2786.  
  2787. /**
  2788. * 小写开头的驼峰转换器
  2789. *
  2790. */
  2791. class CamelConverter extends CamelOrPascalFrom {
  2792. /**
  2793. * 将字符串列表构造为字符串
  2794. *
  2795. * @param list 字符串列表
  2796. * @return {String} 字符串
  2797. * @override
  2798. */
  2799. to(list) {
  2800. return list.reduce((res, s, i) => {
  2801. const str = toLowerCase(s);
  2802. return (res +=
  2803. (i === 0 ? toLowerCase : toUpperCase)(str.substring(0, 1)) +
  2804. str.substring(1));
  2805. }, '');
  2806. }
  2807. }
  2808.  
  2809. /**
  2810. * 大写开头的驼峰转换器
  2811. */
  2812. class PascalConverter extends CamelOrPascalFrom {
  2813. /**
  2814. * 将字符串列表构造为字符串
  2815. *
  2816. * @param list 字符串列表
  2817. * @return {String} 字符串
  2818. * @override
  2819. */
  2820. to(list) {
  2821. return list.reduce((res, s) => {
  2822. const str = toLowerCase(s);
  2823. return (res += toUpperCase(str.substring(0, 1)) + str.substring(1));
  2824. }, '');
  2825. }
  2826. }
  2827.  
  2828. /**
  2829. * 下划线风格解析
  2830. */
  2831. class SnakeOrScreamingSnakeFrom extends IConverter {
  2832. /**
  2833. * 将字符串解析为字符串列表
  2834. *
  2835. * @param str 字符串
  2836. * @return {Array.<String>} 字符串列表
  2837. * @override
  2838. */
  2839. from(str) {
  2840. return str.split('_');
  2841. }
  2842. }
  2843.  
  2844. /**
  2845. * 小写下划线的转换器
  2846. */
  2847. class SnakeConverter extends SnakeOrScreamingSnakeFrom {
  2848. /**
  2849. * 将字符串列表构造为字符串
  2850. *
  2851. * @param list 字符串列表
  2852. * @return {String} 字符串
  2853. * @override
  2854. */
  2855. to(list) {
  2856. return list.map(toLowerCase).join('_');
  2857. }
  2858. }
  2859.  
  2860. /**
  2861. * 大写下划线的转换器
  2862. */
  2863. class ScreamingSnakeConverter extends SnakeOrScreamingSnakeFrom {
  2864. /**
  2865. * 将字符串列表构造为字符串
  2866. *
  2867. * @param list 字符串列表
  2868. * @return {String} 字符串
  2869. * @override
  2870. */
  2871. to(list) {
  2872. return list.map(toUpperCase).join('_');
  2873. }
  2874. }
  2875.  
  2876. /**
  2877. * @enum {Symbol} 字符串风格常量对象
  2878. */
  2879. (function (StringStyleType) {
  2880. /**
  2881. * 小写驼峰
  2882. */
  2883. StringStyleType[StringStyleType["Camel"] = 1] = "Camel";
  2884. /**
  2885. * 大写驼峰
  2886. */
  2887. StringStyleType[StringStyleType["Pascal"] = 2] = "Pascal";
  2888. /**
  2889. * 小写下划线
  2890. */
  2891. StringStyleType[StringStyleType["Snake"] = 3] = "Snake";
  2892. /**
  2893. * 大写下划线
  2894. */
  2895. StringStyleType[StringStyleType["ScreamingSnake"] = 4] = "ScreamingSnake";
  2896. })(exports.StringStyleType || (exports.StringStyleType = {}));
  2897.  
  2898. /**
  2899. * 转换器工厂
  2900. */
  2901. class ConverterFactory {
  2902. /**
  2903. * 获取一个转换器实例
  2904. *
  2905. * @param styleType 转换风格,使用了 {@link stringStyleType} 定义的常量对象
  2906. * @return {IConverter} 转换器对象
  2907. * @throws 如果获取未定义过的转换器,则会抛出异常
  2908. */
  2909. static getInstance(styleType) {
  2910. switch (styleType) {
  2911. case exports.StringStyleType.Camel:
  2912. return new CamelConverter();
  2913. case exports.StringStyleType.Pascal:
  2914. return new PascalConverter();
  2915. case exports.StringStyleType.Snake:
  2916. return new SnakeConverter();
  2917. case exports.StringStyleType.ScreamingSnake:
  2918. return new ScreamingSnakeConverter();
  2919. default:
  2920. throw new Error('No corresponding converter found');
  2921. }
  2922. }
  2923. }
  2924.  
  2925. /**
  2926. * 字符串风格转换器
  2927. * 请不要直接使用构造函数创建,而是用 {@link StringStyleUtil.getConverter} 来获得一个转换器
  2928. * @private
  2929. */
  2930. class StringStyleConverter {
  2931. /**
  2932. * 构造一个字符串任意风格转换器
  2933. * @param from 转换字符串的风格
  2934. * @param to 需要转换的风格
  2935. * @private
  2936. */
  2937. constructor(from, to) {
  2938. /**
  2939. * @field 解析字符串风格的转换器
  2940. * @type {IConverter}
  2941. * @private
  2942. */
  2943. this.fromConverter = ConverterFactory.getInstance(from);
  2944. /**
  2945. * @field 构造字符串风格的转换器
  2946. * @type {IConverter}
  2947. * @private
  2948. */
  2949. this.toConverter = ConverterFactory.getInstance(to);
  2950. }
  2951. /**
  2952. * 转换字符串的风格
  2953. *
  2954. * @param str 要转换的字符串
  2955. * @return {String} 转换得到的字符串
  2956. */
  2957. convert(str) {
  2958. if (stringValidator.isEmpty(str)) {
  2959. return str;
  2960. }
  2961. return this.toConverter.to(this.fromConverter.from(str));
  2962. }
  2963. }
  2964.  
  2965. /**
  2966. * 基本缓存实现
  2967. * 主要封装通用的 delete/size 函数
  2968. */
  2969. class BasicMemoryCache {
  2970. constructor({ limit = Infinity } = {}) {
  2971. this.cache = new Map();
  2972. if (limit <= 0) {
  2973. throw new Error('缓存的最大容量至少为 1');
  2974. }
  2975. this.limit = limit;
  2976. }
  2977. delete(key) {
  2978. this.cache.delete(key);
  2979. }
  2980. clear() {
  2981. this.cache.clear();
  2982. }
  2983. get size() {
  2984. return this.cache.size;
  2985. }
  2986. }
  2987. /**
  2988. * FIFO 算法
  2989. */
  2990. class MemoryCacheFIFO extends BasicMemoryCache {
  2991. add(key, val) {
  2992. const diff = this.cache.size + 1 - this.limit;
  2993. if (diff > 0) {
  2994. const keys = [...this.cache.keys()].slice(0, diff);
  2995. keys.forEach(k => this.delete(k));
  2996. }
  2997. this.cache.set(key, val);
  2998. }
  2999. delete(key) {
  3000. this.cache.delete(key);
  3001. }
  3002. get(key) {
  3003. return this.cache.get(key);
  3004. }
  3005. get size() {
  3006. return this.cache.size;
  3007. }
  3008. has(key) {
  3009. return this.cache.has(key);
  3010. }
  3011. }
  3012. /**
  3013. * IFU 算法
  3014. */
  3015. class MemoryCacheLFU extends BasicMemoryCache {
  3016. constructor() {
  3017. super(...arguments);
  3018. this.lfuMap = new Map();
  3019. }
  3020. add(key, val) {
  3021. const diff = this.cache.size + 1 - this.limit;
  3022. if (diff > 0) {
  3023. const keys = [...this.cache.keys()]
  3024. .sort((k1, k2) => this.lfuMap.get(k1) - this.lfuMap.get(k2))
  3025. .slice(0, diff);
  3026. keys.forEach(k => this.delete(k));
  3027. }
  3028. this.cache.set(key, val);
  3029. this.lfuMap.set(key, 0);
  3030. }
  3031. get(key) {
  3032. this.lfuMap.set(key, this.lfuMap.get(key) + 1);
  3033. return this.cache.get(key);
  3034. }
  3035. has(key) {
  3036. this.lfuMap.set(key, this.lfuMap.get(key) + 1);
  3037. return this.cache.has(key);
  3038. }
  3039. delete(key) {
  3040. super.delete(key);
  3041. this.lfuMap.delete(key);
  3042. }
  3043. clear() {
  3044. super.clear();
  3045. this.lfuMap.clear();
  3046. }
  3047. }
  3048. /**
  3049. * LRU 算法
  3050. */
  3051. class MemoryCacheLRU extends BasicMemoryCache {
  3052. constructor() {
  3053. super(...arguments);
  3054. this.i = 0;
  3055. this.lruMap = new Map();
  3056. }
  3057. get idx() {
  3058. return this.i++;
  3059. }
  3060. add(key, val) {
  3061. const diff = this.cache.size + 1 - this.limit;
  3062. if (diff > 0) {
  3063. const keys = [...this.cache.keys()]
  3064. .sort((k1, k2) => this.lruMap.get(k1) - this.lruMap.get(k2))
  3065. .slice(0, diff);
  3066. console.log(keys, this.lruMap);
  3067. keys.forEach(k => this.delete(k));
  3068. }
  3069. this.cache.set(key, val);
  3070. this.lruMap.set(key, this.idx);
  3071. }
  3072. get(key) {
  3073. this.lruMap.set(key, this.idx);
  3074. return this.cache.get(key);
  3075. }
  3076. has(key) {
  3077. this.lruMap.set(key, this.idx);
  3078. return this.cache.has(key);
  3079. }
  3080. delete(key) {
  3081. super.delete(key);
  3082. this.lruMap.delete(key);
  3083. }
  3084. clear() {
  3085. super.clear();
  3086. this.lruMap.clear();
  3087. }
  3088. }
  3089. (function (MemoryCacheEnum) {
  3090. //先进先出
  3091. MemoryCacheEnum[MemoryCacheEnum["Fifo"] = 0] = "Fifo";
  3092. //最少使用
  3093. MemoryCacheEnum[MemoryCacheEnum["Lfu"] = 1] = "Lfu";
  3094. //最近使用
  3095. MemoryCacheEnum[MemoryCacheEnum["Lru"] = 2] = "Lru";
  3096. })(exports.MemoryCacheEnum || (exports.MemoryCacheEnum = {}));
  3097. /**
  3098. * 缓存工厂类
  3099. */
  3100. class MemoryCacheFactory {
  3101. static create(type, config) {
  3102. switch (type) {
  3103. case exports.MemoryCacheEnum.Fifo:
  3104. return new MemoryCacheFIFO(config);
  3105. case exports.MemoryCacheEnum.Lfu:
  3106. return new MemoryCacheLFU(config);
  3107. case exports.MemoryCacheEnum.Lru:
  3108. return new MemoryCacheLRU(config);
  3109. }
  3110. }
  3111. }
  3112.  
  3113. const onceOfSameParamIdentity = (fn, args) => `onceOfSameParam-${fn.toString()}-${JSON.stringify(args)}`;
  3114. /**
  3115. * 包装一个函数为指定参数只执行一次的函数
  3116. * @param fn 需要包装的函数
  3117. * @param identity 参数转换的函数,参数为需要包装函数的参数
  3118. * @param memoryCache
  3119. * @returns 需要被包装的函数
  3120. */
  3121. function _onceOfSameParam(fn, identity = onceOfSameParamIdentity, memoryCache = MemoryCacheFactory.create(exports.MemoryCacheEnum.Fifo)) {
  3122. const res = new Proxy(fn, {
  3123. apply(_, _this, args) {
  3124. const key = identity(fn, args);
  3125. const old = memoryCache.get(key);
  3126. if (old !== undefined) {
  3127. return old;
  3128. }
  3129. const res = Reflect.apply(_, _this, args);
  3130. return compatibleAsync(res, res => {
  3131. memoryCache.add(key, res);
  3132. return res;
  3133. });
  3134. },
  3135. });
  3136. return Object.assign(res, {
  3137. origin: fn,
  3138. clear(...keys) {
  3139. if (keys.length) {
  3140. memoryCache.clear();
  3141. }
  3142. else {
  3143. keys.forEach(key => memoryCache.delete(key));
  3144. }
  3145. },
  3146. });
  3147. }
  3148. const onceOfSameParam = Object.assign(_onceOfSameParam, {
  3149. identity: onceOfSameParamIdentity,
  3150. });
  3151.  
  3152. /**
  3153. * 包装获取字符串风格转换器
  3154. * 此处采用了单例模式,每种转换器只会有一个
  3155. *
  3156. * @param from 解析风格
  3157. * @param to 转换风格
  3158. * @return {StringStyleConverter} 转换器的实例
  3159. */
  3160. const _getConverter = onceOfSameParam(
  3161. /**
  3162. * @param from 解析风格
  3163. * @param to 转换风格
  3164. * @return {StringStyleConverter} 转换器的实例
  3165. */
  3166. (from, to) => new StringStyleConverter(from, to));
  3167. /**
  3168. * 字符串风格转换工具类
  3169. */
  3170. class StringStyleUtil {
  3171. /**
  3172. * 获取一个转换器的实例
  3173. * 该函数获取的转换器可以任意复用,请优先使用函数
  3174. * @param from 解析风格
  3175. * @param to 转换风格
  3176. * @return {StringStyleConverter} 转换器的实例
  3177. */
  3178. static getConverter(from, to) {
  3179. return _getConverter(from, to);
  3180. }
  3181. /**
  3182. * 直接转换字符串的风格
  3183. * 请优先使用可以复用的 {@link StringStyleUtil.getConverter} 函数
  3184. * @param from 解析风格
  3185. * @param to 转换风格
  3186. * @param str 要转换的字符串
  3187. * @return {String} 转换得到的字符串
  3188. */
  3189. static convert(from, to, str) {
  3190. return StringStyleUtil.getConverter(from, to).convert(str);
  3191. }
  3192. }
  3193.  
  3194. /**
  3195. * 递归使对象不可变
  3196. * @param obj 任何非空对象
  3197. * @returns 新的不可变对象
  3198. */
  3199. function deepFreeze(obj) {
  3200. const freeze = (v) => {
  3201. if (TypeValidator.isObject(v)) {
  3202. deepFreeze(v);
  3203. }
  3204. };
  3205. // 数组和对象分别处理
  3206. if (TypeValidator.isArray(obj)) {
  3207. obj.forEach(freeze);
  3208. }
  3209. else if (TypeValidator.isObject(obj)) {
  3210. Object.keys(obj)
  3211. .map(k => Reflect.get(obj, k))
  3212. .forEach(freeze);
  3213. }
  3214. return Object.freeze(obj);
  3215. }
  3216.  
  3217. // noinspection JSPrimitiveTypeWrapperUsage
  3218. /**
  3219. * 包装对象,使其成为可以任意深度调用而不会出现 undefined 调用的问题
  3220. * 注意: 该函数不能进行递归调用({@link JSON.stringfy}),一定会造成堆栈溢出的问题(RangeError: Maximum call stack size exceeded)
  3221. * @param obj 任意一个 Object 对象
  3222. * @param [defaultValue] 默认值,默认为 {}
  3223. * @returns 包装后的对象
  3224. */
  3225. function deepProxy(obj = {}, defaultValue = new String()) {
  3226. const handler = {
  3227. get(target, k) {
  3228. let v = Reflect.get(target, k);
  3229. if (isNullOrUndefined(v)) {
  3230. v = defaultValue;
  3231. }
  3232. if (TypeValidator.isFunction(v)) {
  3233. return v.bind(target);
  3234. }
  3235. if (!TypeValidator.isObject(v)) {
  3236. return v;
  3237. }
  3238. return new Proxy(v, handler);
  3239. },
  3240. };
  3241. return new Proxy(obj, handler);
  3242. }
  3243.  
  3244. /**
  3245. * 将函数包装为柯里化函数
  3246. * 注: 该函数模仿了 Lodash 的 curry 函数
  3247. * @param fn 需要包装的函数
  3248. * @param {...any} args 应用的部分参数
  3249. * @returns 包装后的函数
  3250. * @deprecated 由于之前的理解错误,该函数在下个大版本将会被废弃,请使用命名更合适的 {@link partial}
  3251. */
  3252. function curry(fn, ...args) {
  3253. const realArgs = args.filter(arg => arg !== curry._);
  3254. // 如果函数参数足够则调用传入的函数
  3255. if (realArgs.length >= fn.length) {
  3256. return fn(...realArgs);
  3257. }
  3258. /**
  3259. * 最终返回的函数
  3260. * @param otherArgs 接受任意参数
  3261. * @returns 返回一个函数,或者函数调用完成返回结果
  3262. */
  3263. function innerFn(...otherArgs) {
  3264. // 记录需要移除补到前面的参数
  3265. const removeIndexSet = new Set();
  3266. let i = 0;
  3267. const newArgs = args.map(arg => {
  3268. if (arg !== curry._ ||
  3269. otherArgs[i] === undefined ||
  3270. otherArgs[i] === curry._) {
  3271. return arg;
  3272. }
  3273. removeIndexSet.add(i);
  3274. // 每次补偿前面的 curry._ 参数计数器 +1
  3275. return otherArgs[i++];
  3276. });
  3277. const newOtherArgs = otherArgs.filter((_v, i) => !removeIndexSet.has(i));
  3278. return curry(fn, ...newArgs, ...newOtherArgs);
  3279. }
  3280. // 定义柯里化函数的剩余参数长度,便于在其他地方进行部分参数应用
  3281. // 注: 不使用 length 属性的原因是 length 属性
  3282. innerFn._length = fn.length - args.filter(arg => arg !== curry._).length;
  3283. // 自定义 toString 函数便于调试
  3284. innerFn.toString = () => `name: ${fn.name}, args: [${args.map(o => o.toString()).join(', ')}]`;
  3285. innerFn._curry = true;
  3286. return innerFn;
  3287. }
  3288. /**
  3289. * 柯里化的占位符,需要应用后面的参数时使用
  3290. * 例如 {@link curry(fn)(curry._, 1)} 意味着函数 fn 的第二个参数将被确定为 1
  3291. */
  3292. curry._ = Symbol('_');
  3293.  
  3294. /**
  3295. * 快速根据指定函数对数组进行排序
  3296. * TODO 此处有 bug,会改变原数组的顺序(在计算的 key 值相同的情况下)
  3297. * 注: 使用递归实现,对于超大数组(其实前端的数组不可能特别大吧?#笑)可能造成堆栈溢出
  3298. * @param arr 需要排序的数组
  3299. * @param k 对数组中每个元素都产生可比较的值的函数,默认返回自身进行比较
  3300. * @returns 排序后的新数组
  3301. */
  3302. function sortBy(arr, k = returnItself) {
  3303. const kFn = getKFn(k);
  3304. // 此处为了让 typedoc 能生成文档而不得不加上类型
  3305. const newArr = arr.map((v, i) => [v, i]);
  3306. function _sort(arr, fn) {
  3307. // 边界条件,如果传入数组的值
  3308. if (arr.length <= 1) {
  3309. return arr;
  3310. }
  3311. // 根据中间值对数组分治为两个数组
  3312. const medianIndex = Math.floor(arr.length / 2);
  3313. const medianValue = arr[medianIndex];
  3314. const left = [];
  3315. const right = [];
  3316. for (let i = 0, len = arr.length; i < len; i++) {
  3317. if (i === medianIndex) {
  3318. continue;
  3319. }
  3320. const v = arr[i];
  3321. if (fn(v, medianValue) <= 0) {
  3322. left.push(v);
  3323. }
  3324. else {
  3325. right.push(v);
  3326. }
  3327. }
  3328. return _sort(left, fn)
  3329. .concat([medianValue])
  3330. .concat(_sort(right, fn));
  3331. }
  3332. return _sort(newArr, ([t1, i1], [t2, i2]) => {
  3333. const k1 = kFn(t1, i1, arr);
  3334. const k2 = kFn(t2, i2, arr);
  3335. if (k1 === k2) {
  3336. return 0;
  3337. }
  3338. else if (k1 < k2) {
  3339. return -1;
  3340. }
  3341. else {
  3342. return 1;
  3343. }
  3344. }).map(([_v, i]) => arr[i]);
  3345. }
  3346.  
  3347. /**
  3348. * 日期格式化器
  3349. * 包含格式化为字符串和解析字符串为日期的函数
  3350. */
  3351. class DateFormatter {
  3352. /**
  3353. * 构造函数
  3354. * @param fmt 日期时间格式
  3355. */
  3356. constructor(fmt) {
  3357. this.fmt = fmt;
  3358. }
  3359. /**
  3360. * 格式化
  3361. * @param date 需要格式化的日期
  3362. * @returns 格式化的字符串
  3363. */
  3364. format(date) {
  3365. if (isNullOrUndefined(date)) {
  3366. return '';
  3367. }
  3368. return dateFormat(date, this.fmt);
  3369. }
  3370. /**
  3371. * 解析字符串为日期对象
  3372. * @param str 字符串
  3373. * @returns 解析得到的日期
  3374. */
  3375. parse(str) {
  3376. if (stringValidator.isEmpty(str)) {
  3377. return null;
  3378. }
  3379. return dateParse(str, this.fmt);
  3380. }
  3381. /**
  3382. * 将日期时间字符串转换为前端指定格式的字符串
  3383. * 主要适用场景是前端接收到后端的日期时间一般是一个字符串,然而需要自定义格式的时候还必须先创建 {@link Date} 对象才能格式化,略微繁琐,故使用该函数
  3384. * @param str 字符串
  3385. * @param parseFmt 解析的日期时间格式。默认直接使用 {@link new Date()} 创建
  3386. * @returns 转换后得到的字符串
  3387. */
  3388. strFormat(str, parseFmt) {
  3389. if (stringValidator.isEmpty(str)) {
  3390. return '';
  3391. }
  3392. const date = parseFmt ? dateParse(str, parseFmt) : new Date(str);
  3393. return dateFormat(date, this.fmt);
  3394. }
  3395. }
  3396. /**
  3397. * 日期格式化器
  3398. */
  3399. DateFormatter.dateFormatter = new DateFormatter('yyyy-MM-dd');
  3400. /**
  3401. * 时间格式化器
  3402. */
  3403. DateFormatter.timeFormatter = new DateFormatter('hh:mm:ss');
  3404. /**
  3405. * 日期时间格式化器
  3406. */
  3407. DateFormatter.dateTimeFormatter = new DateFormatter('yyyy-MM-dd hh:mm:ss');
  3408.  
  3409. /**
  3410. * 查询符合条件的元素的下标
  3411. * @param arr 查询的数组
  3412. * @param fn 谓词
  3413. * @param num 查询的第几个符合条件的元素,默认为 1,和默认的 findIndex 行为保持一致
  3414. * @returns 符合条件的元素的下标,如果没有则返回 -1
  3415. */
  3416. function findIndex(arr, fn, num = 1) {
  3417. let k = 0;
  3418. for (let i = 0, len = arr.length; i < len; i++) {
  3419. if (fn.call(arr, arr[i], i, arr) && ++k >= num) {
  3420. return i;
  3421. }
  3422. }
  3423. return -1;
  3424. }
  3425.  
  3426. /**
  3427. * 连接两个函数并自动柯里化
  3428. * 注: 该函数依赖于 length,所以不支持默认参数以及不定参数
  3429. * @param fn1 第一个函数
  3430. * @param fn2 第二个函数
  3431. * @returns 连接后的函数
  3432. */
  3433. const _compose = (fn1, fn2) => {
  3434. return function (...args) {
  3435. const i = findIndex(args, v => v !== curry._, fn1._length || fn1.length);
  3436. const res = curry(fn1, ...args);
  3437. // 如果这个函数的参数不足,则返回它
  3438. if (i === -1) {
  3439. return _compose(res, fn2);
  3440. }
  3441. // 否则将结果以及多余的参数应用到下一个函数上
  3442. return curry(fn2, res, ...args.slice(i + 1));
  3443. };
  3444. };
  3445. /**
  3446. * 将多个函数组合起来
  3447. * 前面函数的返回值将变成后面函数的第一个参数,如果到了最后一个函数执行完成,则直接返回
  3448. * 注: 该函数是自动柯里化,将对所有传入的函数进行柯里化处理
  3449. * 注: 该函数支持一次调用传入全部函数的参数
  3450. * @param fns 多个需要连接函数
  3451. * @returns 连接后的柯里化函数
  3452. * TODO 这里需要进行类型优化
  3453. */
  3454. function compose(...fns) {
  3455. return fns.reduceRight((fn1, fn2) => _compose(fn2, fn1));
  3456. }
  3457.  
  3458. /**
  3459. * 递归排除对象中的指定字段
  3460. * @param obj 需要排除的对象
  3461. * @param {...obj} fields 需要排除的字段
  3462. */
  3463. function deepExcludeFields(obj, ...fields) {
  3464. if (TypeValidator.isArray(obj)) {
  3465. return obj.map(o => deepExcludeFields(o, ...fields));
  3466. }
  3467. else if (TypeValidator.isDate(obj)) {
  3468. return obj;
  3469. }
  3470. else if (TypeValidator.isObject(obj)) {
  3471. const temp = excludeFields(obj, ...fields);
  3472. return Object.keys(temp).reduce((res, k) => {
  3473. const v = Reflect.get(res, k);
  3474. Reflect.set(res, k, deepExcludeFields(v, ...fields));
  3475. return res;
  3476. }, temp);
  3477. }
  3478. else {
  3479. return obj;
  3480. }
  3481. }
  3482.  
  3483. /**
  3484. * 递归排除对象中的指定字段
  3485. * @param obj 需要排除的对象
  3486. * @param {...obj} fields 需要排除的字段
  3487. * @deprecated 已废弃,请使用统一使用 `deep` 开头的 {@link deepExcludeFields} 函数
  3488. */
  3489. function excludeFieldsDeep(obj, ...fields) {
  3490. return deepExcludeFields(obj, ...fields);
  3491. }
  3492.  
  3493. /**
  3494. * 缓存的值
  3495. */
  3496. class CacheVal {
  3497. /**
  3498. * 构造函数
  3499. * @param options 缓存值对象
  3500. * @param options.key 缓存的键原始值
  3501. * @param options.val 缓存的值
  3502. * @param options.cacheOption 缓存的选项
  3503. */
  3504. constructor(options = {}) {
  3505. Object.assign(this, options);
  3506. }
  3507. }
  3508.  
  3509. /**
  3510. * 无限的超时时间
  3511. * TODO 此处暂时使用字符串作为一种折衷方法,因为 Symbol 无法被序列化为 JSON,反向序列化也是不可能的
  3512. */
  3513. const TimeoutInfinite = 'TimeoutInfinite';
  3514.  
  3515. /**
  3516. * 使用 LocalStorage 实现的缓存
  3517. * 1. get: 根据 key 获取
  3518. * 2. set: 根据 key value 设置,会覆盖
  3519. * 3. touch: 获取并刷新超时时间
  3520. * 4. add: 根据 key value 添加,不会覆盖
  3521. * 5. del: 根据 key 删除
  3522. * 6. clearExpired: 清除所有过期的缓存
  3523. */
  3524. class LocalStorageCache {
  3525. /**
  3526. * 构造函数
  3527. * @param cacheOption 全局缓存选项
  3528. */
  3529. constructor({ timeout = TimeoutInfinite, serialize = JSON.stringify, deserialize = JSON.parse, } = {}) {
  3530. // 这里必须强制转换,因为 timeStart 在全局选项中是不可能存在的
  3531. this.cacheOption = {
  3532. timeout,
  3533. serialize,
  3534. deserialize,
  3535. };
  3536. /**
  3537. * 缓存对象,默认使用 localStorage
  3538. */
  3539. this.localStorage = window.localStorage;
  3540. // 创建后将异步清空所有过期的缓存
  3541. this.clearExpired();
  3542. }
  3543. /**
  3544. * 清空所有过期的 key
  3545. * 注: 该函数是异步执行的
  3546. */
  3547. clearExpired() {
  3548. return __awaiter(this, void 0, void 0, function* () {
  3549. const local = this.localStorage;
  3550. const getKeys = () => {
  3551. const len = local.length;
  3552. const res = [];
  3553. for (let i = 0; i < len; i++) {
  3554. res.push(local.key(i));
  3555. }
  3556. return res;
  3557. };
  3558. getKeys()
  3559. .filter(not(isNullOrUndefined))
  3560. .map(key => safeExec(() => JSON.parse(local.getItem(key))))
  3561. .filter(cacheVal => !isNullOrUndefined(cacheVal) &&
  3562. isNullOrUndefined(cacheVal.cacheOption))
  3563. // TODO 这里暂时加个补丁,过滤掉 timeStart,timeout 为 undefined 的缓存
  3564. .filter(({ cacheOption = {} }) => {
  3565. const { timeStart, timeout } = cacheOption;
  3566. if (isNullOrUndefined(timeStart) || isNullOrUndefined(timeout)) {
  3567. return false;
  3568. }
  3569. return timeout !== TimeoutInfinite && Date.now() - timeStart > timeout;
  3570. })
  3571. .forEach(({ key }) => local.removeItem(key));
  3572. });
  3573. }
  3574. /**
  3575. * 根据 key + value 添加
  3576. * 如果不存在则添加,否则忽略
  3577. * @param key 缓存的 key
  3578. * @param val 缓存的 value
  3579. * @param cacheOption 缓存的选项,默认为无限时间
  3580. * @override
  3581. */
  3582. add(key, val, timeout) {
  3583. const result = this.get(key);
  3584. if (result !== null) {
  3585. return;
  3586. }
  3587. this.set(key, val, timeout);
  3588. }
  3589. /**
  3590. * 根据指定的 key 删除
  3591. * 如果存在则删除,否则忽略
  3592. * @param key 删除的 key
  3593. * @override
  3594. */
  3595. del(key) {
  3596. this.localStorage.removeItem(key);
  3597. }
  3598. /**
  3599. * 根据指定的 key 修改
  3600. * 不管是否存在都会设置
  3601. * @param key 修改的 key
  3602. * @param val 修改的 value
  3603. * @param timeout 修改的选项
  3604. * @override
  3605. */
  3606. set(key, val, timeout) {
  3607. this.localStorage.setItem(key, JSON.stringify(new CacheVal({
  3608. key,
  3609. val: this.cacheOption.serialize(val),
  3610. // 我们不需要缓存序列化/反序列化策略(实际上也无法缓存)
  3611. cacheOption: {
  3612. timeStart: Date.now(),
  3613. timeout: timeout || this.cacheOption.timeout,
  3614. },
  3615. })));
  3616. }
  3617. /**
  3618. * 根据 key 获取
  3619. * 如果存在则获取,否则忽略
  3620. * @param key 指定的 key
  3621. * @param timeout 获取的选项
  3622. * @returns 获取到的缓存值
  3623. * @override
  3624. */
  3625. get(key) {
  3626. const str = this.localStorage.getItem(key);
  3627. const cacheVal = safeExec(() => JSON.parse(str));
  3628. if (isNullOrUndefined(cacheVal) ||
  3629. isNullOrUndefined(cacheVal.cacheOption)) {
  3630. return null;
  3631. }
  3632. const [timeStart, timeout, deserialize] = [
  3633. cacheVal.cacheOption.timeStart,
  3634. cacheVal.cacheOption.timeout,
  3635. this.cacheOption.deserialize,
  3636. ];
  3637. // 如果超时则删除并返回 null
  3638. if (timeout !== TimeoutInfinite && Date.now() - timeStart > timeout) {
  3639. this.del(key);
  3640. return null;
  3641. }
  3642. try {
  3643. return deserialize(cacheVal.val);
  3644. }
  3645. catch (e) {
  3646. this.del(key);
  3647. return null;
  3648. }
  3649. }
  3650. /**
  3651. * 根据 key 获取并刷新超时时间
  3652. * @param key 指定的 key
  3653. * @param cacheOption 获取的选项
  3654. * @returns 获取到的缓存值
  3655. * @override
  3656. */
  3657. touch(key) {
  3658. const str = this.localStorage.getItem(key);
  3659. /**
  3660. * @type {CacheVal}
  3661. */
  3662. const cacheVal = safeExec(() => JSON.parse(str));
  3663. if (isNullOrUndefined(cacheVal) ||
  3664. isNullOrUndefined(cacheVal.cacheOption)) {
  3665. return null;
  3666. }
  3667. const [timeStart, timeout, deserialize] = [
  3668. cacheVal.cacheOption.timeStart,
  3669. cacheVal.cacheOption.timeout,
  3670. this.cacheOption.deserialize,
  3671. ];
  3672. // 如果超时则删除并返回 null
  3673. if (timeout !== TimeoutInfinite && Date.now() - timeStart > timeout) {
  3674. this.del(key);
  3675. return null;
  3676. }
  3677. try {
  3678. const result = deserialize(cacheVal.val);
  3679. this.set(key, result, { timeStart: Date.now(), timeout });
  3680. return result;
  3681. }
  3682. catch (e) {
  3683. this.del(key);
  3684. return null;
  3685. }
  3686. }
  3687. }
  3688.  
  3689. /**
  3690. * 默认使用的 {@link ICache} 接口的缓存实现
  3691. */
  3692. const cache = new LocalStorageCache();
  3693. /**
  3694. * 缓存工具类
  3695. * 主要实现缓存高阶函数的封装
  3696. */
  3697. class CacheUtil {
  3698. /**
  3699. * 将指定函数包装为只调用一次为缓存函数
  3700. * @param fn 需要包装的函数
  3701. * @param options 缓存选项对象。可选项
  3702. * @param options.identity 缓存标识。默认为函数 {@link toString},但有时候不太可行(继承自基类的函数)
  3703. * @param options.timeout 缓存时间。默认为无限
  3704. * @returns 包装后的函数
  3705. */
  3706. static once(fn, { identity = fn.toString(), timeout } = {}) {
  3707. const generateKey = () => `CacheUtil.onceOfSameParam-${identity}`;
  3708. const innerFn = new Proxy(fn, {
  3709. apply(_, _this, args) {
  3710. const key = generateKey();
  3711. const val = cache.get(key);
  3712. if (val !== null) {
  3713. return val;
  3714. }
  3715. return compatibleAsync(Reflect.apply(_, _this, args), res => {
  3716. cache.set(key, res, timeout);
  3717. return res;
  3718. });
  3719. },
  3720. });
  3721. return Object.assign(innerFn, {
  3722. origin: fn,
  3723. clear() {
  3724. cache.del(generateKey());
  3725. },
  3726. });
  3727. }
  3728. /**
  3729. * 包裹函数为缓存函数
  3730. * @param fn 一个接受一些参数并返回结果的函数
  3731. * @param options 缓存选项对象。可选项
  3732. * @param options.identity 缓存标识。默认为函数 {@link toString},但有时候不太可行(继承自基类的函数)
  3733. * @param options.timeout 缓存时间。默认为无限
  3734. * @returns 带有缓存功能的函数
  3735. */
  3736. static onceOfSameParam(fn, { identity = fn.toString(), timeout } = {}) {
  3737. const generateKey = (args) => `CacheUtil.onceOfSameParam-${identity}-${JSON.stringify(args)}`;
  3738. const innerFn = new Proxy(fn, {
  3739. apply(_, _this, args) {
  3740. const key = generateKey(args);
  3741. const val = cache.get(key);
  3742. if (val !== null) {
  3743. return val;
  3744. }
  3745. return compatibleAsync(Reflect.apply(_, _this, args), res => {
  3746. cache.set(key, res, timeout);
  3747. return res;
  3748. });
  3749. },
  3750. });
  3751. return Object.assign(innerFn, {
  3752. origin: fn,
  3753. clear(...args) {
  3754. cache.del(generateKey(args));
  3755. },
  3756. });
  3757. }
  3758. }
  3759. /**
  3760. * 导出一个默认的缓存工具对象
  3761. * @deprecated 已废弃,请直接使用类的静态函数
  3762. */
  3763. const cacheUtil = CacheUtil;
  3764.  
  3765. /**
  3766. * 一个空的函数
  3767. * @param args 接受任何参数
  3768. */
  3769. function emptyFunc(...args) { }
  3770.  
  3771. /**
  3772. * 禁止他人调试网站相关方法的集合对象
  3773. */
  3774. class AntiDebug {
  3775. /**
  3776. * 不停循环 debugger 防止有人调试代码
  3777. * @returns 取消函数
  3778. */
  3779. static cyclingDebugger() {
  3780. const res = setInterval(() => {
  3781. debugger;
  3782. }, 100);
  3783. return () => clearInterval(res);
  3784. }
  3785. /**
  3786. * 检查是否正在 debugger 并调用回调函数
  3787. * @param fn 回调函数,默认为重载页面
  3788. * @returns 取消函数
  3789. */
  3790. static checkDebug(fn = () => window.location.reload()) {
  3791. const res = setInterval(() => {
  3792. const diff = timing(() => {
  3793. debugger;
  3794. });
  3795. if (diff > 500) {
  3796. console.log(diff);
  3797. fn();
  3798. }
  3799. }, 1000);
  3800. return () => clearInterval(res);
  3801. }
  3802. /**
  3803. * 禁用控制台调试输出
  3804. * @returns 取消函数
  3805. */
  3806. static disableConsoleOutput() {
  3807. if (!window.console) {
  3808. return emptyFunc;
  3809. }
  3810. const map = arrayToMap(Object.keys(console), returnItself, k => {
  3811. // @ts-ignore
  3812. const temp = console[k];
  3813. // @ts-ignore
  3814. console[k] = emptyFunc;
  3815. return temp;
  3816. });
  3817. return () => {
  3818. for (const [k, v] of map) {
  3819. // @ts-ignore
  3820. console[k] = v;
  3821. }
  3822. };
  3823. }
  3824. }
  3825. /**
  3826. * 禁止他人调试网站相关方法的集合对象
  3827. * @deprecated 已废弃,请直接使用类的静态函数
  3828. */
  3829. const antiDebug = AntiDebug;
  3830.  
  3831. /**
  3832. * 判断一个字符串是否为空白的字符串
  3833. * @param str 字符串
  3834. * @returns 是否为空字符串
  3835. * @deprecated 已废弃,请使用 {@link stringValidator#isBlank}
  3836. */
  3837. function isBlank(str) {
  3838. return stringValidator.isBlank(str);
  3839. }
  3840.  
  3841. /**
  3842. * 判断一个字符串是否为空字符串
  3843. * @param str 字符串
  3844. * @returns 是否为空字符串
  3845. * @deprecated 已废弃,请使用 {@link stringValidator#isEmpty}
  3846. */
  3847. function isEmpty(str) {
  3848. return stringValidator.isEmpty(str);
  3849. }
  3850.  
  3851. /**
  3852. * 加载一个远程脚本文件
  3853. * @param src 远程脚本路径
  3854. * @returns 等待异步加载脚本完成
  3855. */
  3856. function loadScript(src) {
  3857. return new Promise((resolve, reject) => {
  3858. const script = document.createElement('script');
  3859. script.async = false;
  3860. script.src = src;
  3861. script.addEventListener('load', () => resolve());
  3862. script.addEventListener('error', reject);
  3863. document.body.appendChild(script);
  3864. });
  3865. }
  3866.  
  3867. /**
  3868. * 将一个谓词函数取反
  3869. * 如果是同步函数,则返回的函数也是同步的,否则返回的是取反后的异步函数
  3870. * @param fn 要取反的函数
  3871. * @returns 取反得到的函数
  3872. * @deprecated 已废弃,请使用 {@link CombinedPredicate.not} 进行为此取反
  3873. */
  3874. function deny(fn) {
  3875. return CombinedPredicate.not(fn);
  3876. }
  3877.  
  3878. /**
  3879. * 数组校验器
  3880. */
  3881. class ArrayValidator {
  3882. /**
  3883. * 是否为空数组
  3884. * @param array 空数组
  3885. * @returns 是否为空数组
  3886. */
  3887. static isEmpty(array) {
  3888. return (isNullOrUndefined(array) ||
  3889. !(array instanceof Array) ||
  3890. array.length === 0);
  3891. }
  3892. }
  3893. /**
  3894. * 导出一个默认的数组校验对象
  3895. * @deprecated 已废弃,请直接使用类的静态函数
  3896. */
  3897. const arrayValidator = ArrayValidator;
  3898.  
  3899. /**
  3900. * 路径工具
  3901. */
  3902. class PathUtil {
  3903. /**
  3904. * 拼接多个路径
  3905. *
  3906. * @param paths 路径数组
  3907. * @return {String} 拼接完成的路径
  3908. */
  3909. static join(...paths) {
  3910. return paths.reduce(PathUtil._join);
  3911. }
  3912. /**
  3913. * 拼接两个路径
  3914. *
  3915. * @param pathStart 开始路径
  3916. * @param pathEnd 结束路径
  3917. * @return {String} 拼接完成的两个路径
  3918. */
  3919. static _join(pathStart, pathEnd) {
  3920. if (pathStart.endsWith(PathUtil.Separator)) {
  3921. return (pathStart + pathEnd).replace(PathUtil.Separator + PathUtil.Separator, PathUtil.Separator);
  3922. }
  3923. if (pathEnd.startsWith(PathUtil.Separator)) {
  3924. return pathStart + pathEnd;
  3925. }
  3926. return pathStart + PathUtil.Separator + pathEnd;
  3927. }
  3928. }
  3929. /**
  3930. * 路径分隔符
  3931. */
  3932. PathUtil.Separator = '/';
  3933. /**
  3934. * 导出一个路径工具类
  3935. * @deprecated 已废弃,请直接使用类的静态函数
  3936. */
  3937. const pathUtil = PathUtil;
  3938.  
  3939. var LoggerLevelEnum;
  3940. (function (LoggerLevelEnum) {
  3941. LoggerLevelEnum[LoggerLevelEnum["Debug"] = 0] = "Debug";
  3942. LoggerLevelEnum[LoggerLevelEnum["Log"] = 1] = "Log";
  3943. LoggerLevelEnum[LoggerLevelEnum["Info"] = 2] = "Info";
  3944. LoggerLevelEnum[LoggerLevelEnum["Warn"] = 3] = "Warn";
  3945. LoggerLevelEnum[LoggerLevelEnum["Error"] = 4] = "Error";
  3946. })(LoggerLevelEnum || (LoggerLevelEnum = {}));
  3947. const enumMap = {
  3948. debug: LoggerLevelEnum.Debug,
  3949. log: LoggerLevelEnum.Log,
  3950. info: LoggerLevelEnum.Info,
  3951. warn: LoggerLevelEnum.Warn,
  3952. error: LoggerLevelEnum.Error,
  3953. };
  3954. /**
  3955. * 自定义的日志类
  3956. * 主要便于在开发环境下正常显示调试信息,在生产环境则默认关闭它
  3957. */
  3958. class Logger {
  3959. /**
  3960. * 构造函数
  3961. * @param options 可选项
  3962. * @param options.enable 是否开启日志
  3963. */
  3964. constructor({ enable = true, level = LoggerLevelEnum.Log, } = {}) {
  3965. /**
  3966. * 开发日志:业务强相关调试日志,希望其他人开发时默认隐藏起来的日志(例如第三方服务的回调日志很多,但对于服务接入层的使用者并不关心)
  3967. */
  3968. this.debug = console.debug;
  3969. /**
  3970. * 开发日志:业务相关调试日志,希望其他开发时也能看到的日志
  3971. */
  3972. this.log = console.log;
  3973. /**
  3974. * 生产日志:开发环境也会打印的日志,希望在生产环境打印并且方便调试的日志
  3975. */
  3976. this.info = console.info;
  3977. /**
  3978. * 警告日志:一些危险的操作可以在这里打印出来,同时会显示在生产环境(例如警告用户不要在控制台输入不了解的代码以避免账号安全)
  3979. */
  3980. this.warn = console.warn;
  3981. /**
  3982. * 错误日志:发生错误时使用的日志,发生影响到用户的错误时必须使用该日志
  3983. */
  3984. this.error = console.error;
  3985. this.dir = console.dir;
  3986. this.dirxml = console.dirxml;
  3987. this.table = console.table;
  3988. this.trace = console.trace;
  3989. this.group = console.group;
  3990. this.groupCollapsed = console.groupCollapsed;
  3991. this.groupEnd = console.groupEnd;
  3992. this.clear = console.clear;
  3993. this.count = console.count;
  3994. this.assert = console.assert;
  3995. this.profile = console.profile;
  3996. this.profileEnd = console.profileEnd;
  3997. this.time = console.time;
  3998. this.timeEnd = console.timeEnd;
  3999. this.timeStamp = console.timeStamp;
  4000. this.enable = enable;
  4001. this.level = level;
  4002. }
  4003. /**
  4004. * 设置 enable 的 setter 属性,在改变时合并对应的子类对象实现
  4005. * @param enable 是否开启
  4006. */
  4007. set enable(enable) {
  4008. Object.keys(console).forEach(k => Reflect.set(this, k, enable ? console[k] : emptyFunc));
  4009. }
  4010. /**
  4011. * 设置日志的级别
  4012. * @param level
  4013. */
  4014. set level(level) {
  4015. Object.keys(console)
  4016. .filter(k => Reflect.has(enumMap, k))
  4017. .forEach(k => Reflect.set(this, k, Reflect.get(enumMap, k) >= level ? console[k] : emptyFunc));
  4018. }
  4019. }
  4020. Logger.Level = LoggerLevelEnum;
  4021. /**
  4022. * 导出一个全局可用的 Logger 对象
  4023. * 使用 enable 属性控制是否开启日志输出,默认为 true
  4024. */
  4025. const logger = new Logger();
  4026.  
  4027. /**
  4028. * 将 Object 对象 转换为 Map
  4029. * @param obj Object 对象
  4030. * @returns 转换得到的 Map 键值表
  4031. */
  4032. function objectToMap(obj) {
  4033. return Object.keys(obj).reduce((map, k) => map.set(k, Reflect.get(obj, k)), new Map());
  4034. }
  4035.  
  4036. /**
  4037. * 将列表转换为树节点
  4038. * 注: 该函数默认树的根节点只有一个,如果有多个,则返回一个数组
  4039. * @param list 树节点列表
  4040. * @param options 其他选项
  4041. * @returns 树节点,或是树节点列表
  4042. */
  4043. function listToTree(list, { bridge = returnItself, isRoot = node => !node.parentId, } = {}) {
  4044. const arr = [];
  4045. const res = list.reduce((root, _sub) => {
  4046. const sub = bridge(_sub);
  4047. if (isRoot(sub)) {
  4048. root.push(sub);
  4049. return root;
  4050. }
  4051. for (const _parent of list) {
  4052. const parent = bridge(_parent);
  4053. if (sub.parentId === parent.id) {
  4054. parent.child = parent.child || [];
  4055. parent.child.push(sub);
  4056. return root;
  4057. }
  4058. }
  4059. return root;
  4060. }, arr);
  4061. // 根据顶级节点的数量决定如何返回
  4062. const len = res.length;
  4063. if (len === 0)
  4064. return {};
  4065. if (len === 1)
  4066. return res[0];
  4067. return res;
  4068. }
  4069.  
  4070. /**
  4071. * 桥接对象不存在的字段
  4072. * @param map 代理的字段映射 Map
  4073. * @returns 转换一个对象为代理对象
  4074. * @typeparam 类型解释:1. -readonly 是将使用者的 as const 修改为可变的字段,2. [P in keyof M] 从映射对象中取出所有的 key 作为字段,3. T[M[P] extends keyof T ? M[P] : never] 本质上只是 T[M[P]]],只是 ts 不认为 M[P] 是 T 的字段,所以只能绕一下才能使用
  4075. */
  4076. function bridge(map) {
  4077. /**
  4078. * 为对象添加代理的函数
  4079. * @param obj 任何对象
  4080. * @returns 代理后的对象
  4081. */
  4082. return function (obj) {
  4083. return new Proxy(obj, {
  4084. get(_, k) {
  4085. if (Reflect.has(map, k)) {
  4086. return Reflect.get(_, Reflect.get(map, k));
  4087. }
  4088. return Reflect.get(_, k);
  4089. },
  4090. set(_, k, v) {
  4091. if (Reflect.has(map, k)) {
  4092. Reflect.set(_, Reflect.get(map, k), v);
  4093. return true;
  4094. }
  4095. Reflect.set(_, k, v);
  4096. return true;
  4097. },
  4098. });
  4099. };
  4100. }
  4101.  
  4102. /**
  4103. * 遍历并映射一棵树的每个节点
  4104. * @param root 树节点
  4105. * @param options 其他选项
  4106. * @returns 递归遍历后的树节点
  4107. */
  4108. function treeMapping(root, { before = returnItself, after = returnItself, paramFn = (node, ...args) => [], } = {}) {
  4109. /**
  4110. * 遍历一颗完整的树
  4111. * @param node 要遍历的树节点
  4112. * @param args 每次递归遍历时的参数
  4113. */
  4114. function _treeMapping(node, ...args) {
  4115. // 之前的操作
  4116. const _node = before(node, ...args);
  4117. const _child = _node.child;
  4118. if (!arrayValidator.isEmpty(_child)) {
  4119. _node.child = _child.map(v =>
  4120. // 产生一个参数
  4121. _treeMapping(v, ...paramFn(_node, ...args)));
  4122. }
  4123. // 之后的操作
  4124. return after(_node, ...args);
  4125. }
  4126. return _treeMapping(root);
  4127. }
  4128.  
  4129. /**
  4130. * 将树节点转为树节点列表
  4131. * 这里使用了循环进行遍历,而非传统的递归方式
  4132. * @param root 树节点
  4133. * @param options 其他选项
  4134. * @returns 树节点列表
  4135. */
  4136. function treeToList(root, { calcPath = false, bridge = returnItself, } = {}) {
  4137. const res = [];
  4138. const temp = bridge(root);
  4139. if (calcPath) {
  4140. temp.path = temp.id + '';
  4141. }
  4142. // 利用队列缓存所有未处理的节点
  4143. const queue = [temp];
  4144. // 使用 Set 防止可能的重复引用
  4145. const filterSet = new Set();
  4146. // 使用 lastIdMap 避免重复添加
  4147. const lastIdMap = new Map();
  4148. for (let value; queue.length > 0;) {
  4149. const first = queue.shift();
  4150. value = bridge(first);
  4151. // 判断重复
  4152. if (value === undefined || filterSet.has(first)) {
  4153. continue;
  4154. }
  4155. filterSet.add(first);
  4156. res.push(value);
  4157. const child = value.child;
  4158. if (ArrayValidator.isEmpty(child)) {
  4159. continue;
  4160. }
  4161. const childNonIllegal = child.filter(v => !isNullOrUndefined(v) || filterSet.has(v));
  4162. // TODO 这里和上面的代码明显重复,待优化。。。
  4163. queue.push(...(calcPath
  4164. ? childNonIllegal.map(v => {
  4165. const _v = bridge(v);
  4166. // 如果最后一个的 id 等于自身,说明已经被添加过了
  4167. if (lastIdMap.get(_v.id) === _v.id) {
  4168. return _v;
  4169. }
  4170. // 使用父节点绝对路径 + 当前 id
  4171. _v.path = value.path + ',' + _v.id;
  4172. lastIdMap.set(_v.id, _v.id);
  4173. return _v;
  4174. })
  4175. : childNonIllegal));
  4176. }
  4177. return res;
  4178. }
  4179.  
  4180. /**
  4181. * 树节点桥接工具类
  4182. * 主要实现了桥接 {@field bridge} {@field bridgeTree} 和 {@field bridgeList} 三个函数,事实上桥接之后再转换相当于做了两遍,但就目前而言暂且只能如此了
  4183. */
  4184. class NodeBridgeUtil {
  4185. /**
  4186. * 桥接对象为标准的树结构
  4187. * @param nodeBridge 桥接对象
  4188. * @returns 代理函数
  4189. */
  4190. static bridge({ id = 'id', parentId = 'parentId', child = 'child', path = 'path', } = {}) {
  4191. return bridge({
  4192. id,
  4193. parentId,
  4194. child,
  4195. path,
  4196. });
  4197. }
  4198. /**
  4199. * 桥接一棵完整的树
  4200. * @param tree 树节点
  4201. * @param nodeBridge 桥接对象
  4202. * @returns 代理后的树对象
  4203. */
  4204. static bridgeTree(tree, nodeBridge) {
  4205. return treeMapping(tree, {
  4206. before: this.bridge(nodeBridge),
  4207. });
  4208. }
  4209. /**
  4210. * 桥接一个树节点列表
  4211. * @param list 树节点列表
  4212. * @param nodeBridge 桥接对象
  4213. * @returns 代理后的树节点列表
  4214. */
  4215. static bridgeList(list, nodeBridge) {
  4216. return list.map(this.bridge(nodeBridge));
  4217. }
  4218. }
  4219. /**
  4220. * 导出一个 NodeBridgeUtil 的实例
  4221. * @deprecated 已废弃,请直接使用类的静态函数
  4222. */
  4223. const nodeBridgeUtil = NodeBridgeUtil;
  4224.  
  4225. /**
  4226. * 获取对象中所有的属性及对应的值,包括 ES6 新增的 Symbol 类型的属性
  4227. * @param obj 任何对象
  4228. * @returns 属性及其对应值的二维数组
  4229. * @deprecated 该函数将要被废弃,实质上应用场景很窄
  4230. */
  4231. function getObjectEntries(obj) {
  4232. const mFn = k => [
  4233. k,
  4234. Reflect.get(obj, k),
  4235. ];
  4236. return Reflect.ownKeys(obj).map(mFn);
  4237. }
  4238.  
  4239. /**
  4240. * 获取对象中所有的属性,包括 ES6 新增的 Symbol 类型的属性
  4241. * @param obj 任何对象
  4242. * @returns 属性数组
  4243. * @deprecated 已废弃,请使用 ES6 {@see Reflect.ownKeys} 代替
  4244. * 具体参考 {@url(https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Reflect/ownKeys)}
  4245. */
  4246. function getObjectKeys(obj) {
  4247. if (isNullOrUndefined(obj)) {
  4248. return [];
  4249. }
  4250. return Reflect.ownKeys(obj);
  4251. }
  4252.  
  4253. /**
  4254. * 比较两个浮点数是否相等
  4255. * 具体实现采用差值取绝对值并与 {@link Number.EPSILON} 比较的方式,如果小于浮点数最小差值,则认为它们是 [相等] 的
  4256. * @param num1 第一个浮点数
  4257. * @param num2 第二个浮点数
  4258. * @returns 两数是否相等
  4259. */
  4260. function floatEquals(num1, num2) {
  4261. return Math.abs(num1 - num2) < Number.EPSILON;
  4262. }
  4263.  
  4264. //TODO 暂时绕过类型错误,之后有时间再修
  4265. // export function assign<T, A>(target: T, a: A): T & A
  4266. // export function assign<T, A, B>(target: T, a: A, b: B): T & A & B
  4267. // export function assign<T, A, B, C>(target: T, a: A, b: B, c: C): T & A & B & C
  4268. // export function assign<T, A, B, C, D>(
  4269. // target: T,
  4270. // a: A,
  4271. // b: B,
  4272. // c: C,
  4273. // d: D,
  4274. // ): T & A & B & C & D
  4275. /**
  4276. * 合并多个对象的属性
  4277. * 1. 该合并的方式为浅层合并,只会合并一层的对象
  4278. * 2. 默认忽略值为 undefined/null 的属性
  4279. * @param target 覆盖的对象上
  4280. * @param {...Object} sources 任意数量的对象
  4281. * @returns 合并后的对象
  4282. */
  4283. function assign(target, ...sources) {
  4284. return [target, ...sources].reduce((res, source) => {
  4285. if (isNullOrUndefined(source)) {
  4286. return res;
  4287. }
  4288. return Object.keys(source).reduce((res, k) => {
  4289. const v = Reflect.get(source, k);
  4290. if (isNullOrUndefined(v)) {
  4291. return res;
  4292. }
  4293. Reflect.set(res, k, v);
  4294. return res;
  4295. }, res);
  4296. }, {});
  4297. }
  4298.  
  4299. /**
  4300. * 根据不同的源对象获取不同的正则匹配,代表不需要拷贝的属性
  4301. * @param source 源对象
  4302. * @returns 匹配内部属性的正则表达式
  4303. */
  4304. function getInnerFieldRule(source) {
  4305. if (source instanceof Function) {
  4306. return /^(?:constructor|prototype|arguments|caller|name|bind|call|apply|toString|length)$/;
  4307. }
  4308. else {
  4309. return /^(?:toString|length)$/;
  4310. }
  4311. }
  4312. /**
  4313. * 拷贝对象的属性到目标对象上
  4314. * @param target 目标对象
  4315. * @param source 源对象
  4316. * @returns 返回 {@param target} 目标对象
  4317. */
  4318. function _copyProps(target, source) {
  4319. const innerField = getInnerFieldRule(source);
  4320. Reflect.ownKeys(source).forEach(prop => {
  4321. if (typeof prop === 'string' && innerField.test(prop)) {
  4322. return;
  4323. }
  4324. Reflect.set(target, prop, Reflect.get(source, prop));
  4325. });
  4326. return target;
  4327. }
  4328. /**
  4329. * 混合多个类
  4330. * @param {...Class} mixins 需要混合的多个类及其构造函数参数映射函数的 Map 集合
  4331. * @returns 返回一个混合后的类,构造函数将的参数
  4332. */
  4333. function aggregation(mixins) {
  4334. const arr = Array.from(mixins);
  4335. class Aggregate {
  4336. /**
  4337. * @param args 任意数量的参数
  4338. */
  4339. constructor(...args) {
  4340. arr.forEach(([Mixin, fn = returnItself]) => _copyProps(this, new Mixin(...fn(args))));
  4341. }
  4342. }
  4343. arr.forEach(([Mixin]) => {
  4344. _copyProps(Aggregate.prototype, Mixin.prototype);
  4345. _copyProps(Aggregate, Mixin);
  4346. });
  4347. return Aggregate;
  4348. }
  4349.  
  4350. /**
  4351. * 包装一个异步函数为有限制并发功能的函数
  4352. * @param fn 异步函数
  4353. * @param options 可选参数
  4354. * @param options.limit 并发限制数量,默认为 1
  4355. * @returns 返回被包装后的限制并发功能的函数
  4356. */
  4357. function asyncLimiting(fn, { limit = 1 } = {}) {
  4358. // 当前正在执行异步的数量
  4359. let execCount = 0;
  4360. // waitArr 等待的队列
  4361. const takeQueue = [];
  4362. // 是否增加了 execCount 的标记
  4363. let flag = false;
  4364. return new Proxy(fn, {
  4365. apply(_, _this, args) {
  4366. return __awaiter(this, void 0, void 0, function* () {
  4367. const _takeRun = () => __awaiter(this, void 0, void 0, function* () {
  4368. if (!flag) {
  4369. execCount++;
  4370. flag = false;
  4371. }
  4372. const tempArgs = takeQueue.shift();
  4373. try {
  4374. return yield Reflect.apply(_, _this, tempArgs);
  4375. }
  4376. finally {
  4377. execCount--;
  4378. }
  4379. });
  4380. takeQueue.push(args);
  4381. yield wait(() => {
  4382. const result = execCount < limit;
  4383. // 如果等待结束则必须立刻增加 execCount,避免微任务与宏任务调度可能产生错误
  4384. if (result) {
  4385. flag = true;
  4386. execCount++;
  4387. }
  4388. return result;
  4389. });
  4390. return _takeRun();
  4391. });
  4392. },
  4393. });
  4394. }
  4395.  
  4396. /**
  4397. * 默认的超时时间,可以认为是无限
  4398. */
  4399. const TimeoutInfinity = () => false;
  4400. /**
  4401. * 创建一个 Lock 对象,用于锁住当前的当前的异步流程
  4402. */
  4403. class Locker {
  4404. /**
  4405. * @param options 可选项
  4406. * @param options.limit 限制并发数量,默认为 1
  4407. * @param options.timeout 超时时间,默认为无限
  4408. */
  4409. constructor({ limit = 1, timeout } = {}) {
  4410. this.limit = limit;
  4411. this.timeout = timeout || TimeoutInfinity;
  4412. }
  4413. /**
  4414. * 当前是否锁住了
  4415. * @returns 是否锁住了
  4416. */
  4417. isLocked() {
  4418. return this.limit <= 0;
  4419. }
  4420. /**
  4421. * 添加异步锁
  4422. * @param timeout 超时时间,默认为全局 timeout
  4423. * @returns 进行等待
  4424. */
  4425. lock(timeout = this.timeout) {
  4426. return __awaiter(this, void 0, void 0, function* () {
  4427. if (this.isLocked()) {
  4428. /**
  4429. * @type {Number|Function}
  4430. */
  4431. yield Promise.race([
  4432. wait(() => {
  4433. const result = !this.isLocked();
  4434. if (result) {
  4435. this.limit--;
  4436. }
  4437. return result;
  4438. }),
  4439. wait(timeout),
  4440. ]);
  4441. }
  4442. else {
  4443. this.limit--;
  4444. }
  4445. });
  4446. }
  4447. /**
  4448. * 删除异步锁
  4449. */
  4450. unlock() {
  4451. this.limit++;
  4452. }
  4453. }
  4454.  
  4455. /**
  4456. * 包装一个函数为有错误重试功能的函数
  4457. * 注: 如果发生错误,最终将抛出最后一次调用的异常
  4458. * @param fn 需要被包装的函数
  4459. * @param num 调用的次数。默认为 1
  4460. * @param errorCheck 检查返回结果是否需要重试的函数。默认只要 resolve() 就返回 true
  4461. * @returns 包装后的有错误重试功能的函数
  4462. */
  4463. function trySometime(fn, num = 1, errorCheck = res => true) {
  4464. return new Proxy(fn, {
  4465. apply(target, thisArg, args) {
  4466. return __awaiter(this, void 0, void 0, function* () {
  4467. let err;
  4468. for (let i = 0; i < num; i++) {
  4469. try {
  4470. // 等待结果出来
  4471. const res = yield Reflect.apply(target, thisArg, args);
  4472. // 如果没问题就直接返回了
  4473. if (errorCheck(res)) {
  4474. return res;
  4475. }
  4476. // 否则抛出异常以进行下一次重试
  4477. throw res;
  4478. }
  4479. catch (error) {
  4480. err = error;
  4481. }
  4482. }
  4483. throw err;
  4484. });
  4485. },
  4486. });
  4487. }
  4488.  
  4489. /**
  4490. * 包装一个函数为有错误重试功能的函数
  4491. * 注意: 该函数是并行运行,所以一旦调用,就会同时调用 n 次,不管之前有没有失败。。。此函数不适合包装有副作用的操作,例如修改用户信息,请使用 {@link trySometime} 替代
  4492. * @param fn 需要被包装的函数
  4493. * @param num 调用的次数。默认为 1
  4494. * @param errorCheck 检查返回结果是否需要重试的函数。默认只要 resolve() 就返回 true
  4495. * @returns 包装后的有错误重试功能的函数
  4496. */
  4497. function trySometimeParallel(fn, num = 1, errorCheck = res => true) {
  4498. return new Proxy(fn, {
  4499. apply(target, thisArg, args) {
  4500. return __awaiter(this, void 0, void 0, function* () {
  4501. return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
  4502. let err;
  4503. try {
  4504. yield Promise.all(range(0, num).map(() => __awaiter(this, void 0, void 0, function* () {
  4505. try {
  4506. const res = yield Reflect.apply(target, thisArg, args);
  4507. if (errorCheck(res) === true) {
  4508. resolve(res);
  4509. }
  4510. throw res;
  4511. }
  4512. catch (error) {
  4513. err = error;
  4514. }
  4515. })));
  4516. }
  4517. catch (error) {
  4518. console.log(error);
  4519. }
  4520. reject(err);
  4521. }));
  4522. });
  4523. },
  4524. });
  4525. }
  4526.  
  4527. /**
  4528. * 深度比较两个对象是否相等
  4529. * @param x 任何对象
  4530. * @param y 任何对象
  4531. * @returns 是否相等
  4532. */
  4533. function compare(x, y) {
  4534. if ((typeof x === 'number' || x instanceof Number) &&
  4535. (typeof y === 'number' || y instanceof Number)) {
  4536. const _x = +x;
  4537. const _y = +y;
  4538. // 如果都是 NaN 则直接返回 true
  4539. if (isNaN(_x) && isNaN(_y)) {
  4540. return true;
  4541. }
  4542. // 如果是 -0/+0 则返回 false
  4543. if (_x === _y) {
  4544. return 1 / _x === 1 / _y;
  4545. }
  4546. // 如果均为数字且两数之差的绝对值小于浮点数的最小精度(此举主要是为了避免浮点数的精度丢失)
  4547. if (Math.abs(_x - _y) < Number.EPSILON) {
  4548. return true;
  4549. }
  4550. }
  4551. // 如果恒等表达式成立则直接返回 true
  4552. if (x === y) {
  4553. return true;
  4554. }
  4555. // 比较正则和字符串
  4556. if ((x instanceof RegExp && y instanceof RegExp) ||
  4557. ((typeof x === 'string' || x instanceof String) &&
  4558. (typeof y === 'string' || y instanceof String))) {
  4559. return x.toString() === y.toString();
  4560. }
  4561. // 如果都是时间则比较它们的时间戳
  4562. if (x instanceof Date && y instanceof Date) {
  4563. return x.getTime() === y.getTime();
  4564. }
  4565. // 如果两者有一个不是 Object 类型的话则直接返回 false
  4566. // 注: 此处直接返回 false 是因为特殊原生类型的都在上面处理过了
  4567. // 注: Array 可以按照 Object 的逻辑进行处理
  4568. if (!(x instanceof Object && y instanceof Object)) {
  4569. return false;
  4570. }
  4571. // 比较它们的原型
  4572. if (x.prototype !== y.prototype) {
  4573. return false;
  4574. }
  4575. // 比较构造函数
  4576. if (x.constructor !== y.constructor) {
  4577. return false;
  4578. }
  4579. // 比较 y 中的属性是否全部都在 x 中
  4580. for (const p of Object.keys(y)) {
  4581. if (!Reflect.has(x, p)) {
  4582. return false;
  4583. }
  4584. }
  4585. // 比较 x 中的属性是否全部都在 y 中
  4586. for (const p of Object.keys(x)) {
  4587. if (!Reflect.has(y, p)) {
  4588. return false;
  4589. }
  4590. // 比较每个元素的类型,如果不同则直接返回 false
  4591. if (typeof y[p] !== typeof x[p]) {
  4592. return false;
  4593. }
  4594. // 递归比较每个元素
  4595. if (!compare(x[p], y[p])) {
  4596. return false;
  4597. }
  4598. }
  4599. // 全部比较完成仍然没有结果就返回 true
  4600. return true;
  4601. }
  4602.  
  4603. /**
  4604. * 阻塞主线程指定时间
  4605. * 注: 和 {@see wait} 不同,该函数会真的阻塞住主线程,即这段时间内其他的代码都无法执行,例如用户的点击事件!
  4606. * @param time 阻塞毫秒数
  4607. */
  4608. function sleep(time) {
  4609. const end = performance.now() + time;
  4610. while (performance.now() <= end) { }
  4611. }
  4612.  
  4613. /**
  4614. * 包装一个函数为异步函数
  4615. * 如果是一个异步函数,则直接返回,否则返回一部函数
  4616. * @param fn 任意一个函数
  4617. * @returns 返回的异步结果 Promise 对象
  4618. * @typeparam R 原函数函数返回值类型
  4619. */
  4620. function async(fn) {
  4621. return new Proxy(fn, {
  4622. apply(_, _this, args) {
  4623. return __awaiter(this, void 0, void 0, function* () {
  4624. return yield Reflect.apply(_, _this, args);
  4625. });
  4626. },
  4627. });
  4628. }
  4629.  
  4630. /**
  4631. * 将一个异步函数包装为具有时序的异步函数
  4632. * 注: 该函数会按照调用顺序依次返回结果,后面的调用的结果需要等待前面的,所以如果不关心过时的结果,请使用 {@link switchMap} 函数
  4633. * @param fn 一个普通的异步函数
  4634. * @returns 包装后的函数
  4635. */
  4636. function mergeMap(fn) {
  4637. // 当前执行的异步操作 id
  4638. let id = 0;
  4639. // 所执行的异步操作 id 列表
  4640. const ids = new Set();
  4641. return new Proxy(fn, {
  4642. apply(_, _this, args) {
  4643. return __awaiter(this, void 0, void 0, function* () {
  4644. const prom = Reflect.apply(_, _this, args);
  4645. const temp = id;
  4646. ids.add(temp);
  4647. id++;
  4648. yield wait(() => !ids.has(temp - 1));
  4649. ids.delete(temp);
  4650. return yield prom;
  4651. });
  4652. },
  4653. });
  4654. }
  4655.  
  4656. /**
  4657. * 将一个异步函数包装为具有时序的异步函数
  4658. * 注: 该函数会丢弃过期的异步操作结果,这样的话性能会稍稍提高(主要是响应比较快的结果会立刻生效而不必等待前面的响应结果)
  4659. * @param fn 一个普通的异步函数
  4660. * @returns 包装后的函数
  4661. */
  4662. function switchMap(fn) {
  4663. // 当前执行的异步操作 id
  4664. let id = 0;
  4665. // 最后一次异步操作的 id,小于这个的操作结果会被丢弃
  4666. let last = 0;
  4667. // 缓存最后一次异步操作的结果
  4668. let cache;
  4669. return new Proxy(fn, {
  4670. apply(_, _this, args) {
  4671. return __awaiter(this, void 0, void 0, function* () {
  4672. const temp = id;
  4673. id++;
  4674. const res = yield Reflect.apply(_, _this, args);
  4675. if (temp < last) {
  4676. return cache;
  4677. }
  4678. cache = res;
  4679. last = temp;
  4680. return res;
  4681. });
  4682. },
  4683. });
  4684. }
  4685.  
  4686. /**
  4687. * 将指定函数包装为只调用一次,其他的调用返回旧值
  4688. * 主要适用场景是只允许调用一次的地方,例如 Tab 的初始化
  4689. * * 示意图:
  4690. * a => b => c => d => e =>
  4691. * a ==|====|====|====|====>
  4692. * |b |c |d |e (die)
  4693. *
  4694. * @param fn 需要包装的函数
  4695. * @returns 包装后的函数
  4696. */
  4697. function once(fn) {
  4698. let flag = true;
  4699. let cache;
  4700. const res = new Proxy(fn, {
  4701. apply(target, thisArg, args) {
  4702. if (!flag) {
  4703. return cache;
  4704. }
  4705. flag = false;
  4706. // 如果是异步函数则返回异步的结果
  4707. return compatibleAsync(Reflect.apply(target, thisArg, args), res => {
  4708. cache = res;
  4709. return cache;
  4710. });
  4711. },
  4712. });
  4713. return Object.assign(res, {
  4714. origin: fn,
  4715. clear() {
  4716. cache = null;
  4717. },
  4718. });
  4719. }
  4720.  
  4721. /**
  4722. * 将一个异步函数包装为具有时序的异步函数
  4723. * 注: 该函数会按照调用顺序依次返回结果,后面的执行的调用(不是调用结果)需要等待前面的,此函数适用于异步函数的内里执行也必须保证顺序时使用,否则请使用 {@link mergeMap} 函数
  4724. * 注: 该函数其实相当于调用 {@code asyncLimiting(fn, {limit: 1})} 函数
  4725. * 例如即时保存文档到服务器,当然要等待上一次的请求结束才能请求下一次,不然数据库保存的数据就存在谬误了
  4726. * @param fn 一个普通的异步函数
  4727. * @returns 包装后的函数
  4728. */
  4729. function concatMap(fn) {
  4730. // 当前执行的异步操作 id
  4731. let id = 0;
  4732. // 所执行的异步操作 id 列表
  4733. const ids = new Set();
  4734. return new Proxy(fn, {
  4735. apply(_, _this, args) {
  4736. return __awaiter(this, void 0, void 0, function* () {
  4737. const temp = id;
  4738. ids.add(temp);
  4739. id++;
  4740. yield wait(() => !ids.has(temp - 1));
  4741. const res = yield Reflect.apply(_, _this, args);
  4742. ids.delete(temp);
  4743. return res;
  4744. });
  4745. },
  4746. });
  4747. }
  4748.  
  4749. /**
  4750. * 重复执行指定的函数
  4751. * @param num 重复的次数
  4752. * @param fn 执行的函数,如果是异步函数,则返回 Array.<Promise>
  4753. * @param {...Object} args 参数
  4754. * @returns 执行返回结果
  4755. */
  4756. function repeatedCall(num, fn, ...args) {
  4757. return range(0, num).map(() => fn(...args));
  4758. }
  4759.  
  4760. /**
  4761. * 发布订阅模式
  4762. * @typeparam T 订阅主题的类型,虽然可以为 any,但这里是刻意进行限制以避免 “全局” 的发布订阅中心对象
  4763. * @deprecated 已废弃,请使用语义更好、类型安全且 API 更强大的 {@see EventEmitter} 进行事件总线处理
  4764. */
  4765. class PubSubMachine {
  4766. constructor() {
  4767. /**
  4768. * 订阅者集合
  4769. */
  4770. this.subMap = new Map();
  4771. }
  4772. /**
  4773. * 发布一个主题
  4774. * @param topic 发布的主题
  4775. * @param [args] 主题订阅所需要的参数
  4776. */
  4777. pub(topic, ...args) {
  4778. const fns = this.subMap.get(topic);
  4779. if (fns === undefined) {
  4780. return;
  4781. }
  4782. fns.forEach(fn => fn(...args));
  4783. }
  4784. /**
  4785. * 订阅一个主题
  4786. * @param topic 订阅的主题
  4787. * @param fn 回调的函数
  4788. */
  4789. sub(topic, fn) {
  4790. if (!this.subMap.has(topic)) {
  4791. this.subMap.set(topic, []);
  4792. }
  4793. this.subMap.get(topic).push(fn);
  4794. }
  4795. /**
  4796. * 取消订阅
  4797. * @param topic 订阅的主题
  4798. * @param fn 订阅的函数,没有则删除这个主题下所有的函数
  4799. */
  4800. unsub(topic, fn) {
  4801. if (fn === undefined) {
  4802. this.subMap.delete(topic);
  4803. return;
  4804. }
  4805. if (!this.subMap.has(topic)) {
  4806. return;
  4807. }
  4808. const fns = this.subMap.get(topic);
  4809. fns.splice(fns.indexOf(fn), 1);
  4810. }
  4811. }
  4812.  
  4813. /**
  4814. * 提取对象数组为 Map
  4815. * @param arr 对象数组
  4816. * @param fields 提取的字段
  4817. * @returns 提取字段名对应其字段值数组的 Map
  4818. * @typeparam T 数组元素的类型,必须为可提取字段的对象
  4819. */
  4820. function extractFieldMap(arr, fields) {
  4821. return arr.reduce((res, v) => {
  4822. for (const [k, _arr] of res) {
  4823. _arr.push(Reflect.get(v, k));
  4824. }
  4825. return res;
  4826. }, arrayToMap(fields, k => k, () => new Array()));
  4827. }
  4828.  
  4829. /**
  4830. * 数组按照指定长度进行分段为二维数组
  4831. * 注: num 必须要大于 1
  4832. * @param arr 要进行分段的数组
  4833. * @param num 每段的长度
  4834. * @returns 分段后的二维数组
  4835. */
  4836. function segmentation(arr, num) {
  4837. return arr.reduce((res, v, i) => {
  4838. const index = (i + 1) % num;
  4839. if (index === 1) {
  4840. res.push([]);
  4841. }
  4842. res[res.length - 1].push(v);
  4843. return res;
  4844. }, new Array());
  4845. }
  4846.  
  4847. /**
  4848. * 切换 DOM 元素的 class
  4849. * @param {Element} el DOM 元素
  4850. * @param {Object} obj 切换的状态/class 键值对象
  4851. * @return 根据状态切换 class 的函数
  4852. */
  4853. function toggleClass(el, obj) {
  4854. const arr = Array.from(Object.values(obj));
  4855. /**
  4856. * 返回切换 class 的函数
  4857. * @param state 切换的状态
  4858. */
  4859. return function toggle(state) {
  4860. arr.forEach(v => el.classList.remove(v));
  4861. el.classList.add(obj[state]);
  4862. };
  4863. }
  4864.  
  4865. /**
  4866. * 将函数包装为偏函数
  4867. * 注: 该函数模仿了 underscore 的 partial 函数
  4868. * @param fn 需要包装的函数
  4869. * @param {...any} args 应用的部分参数
  4870. * @returns 包装后的函数
  4871. */
  4872. function partial(fn, ...args) {
  4873. const realArgs = args.filter(arg => arg !== partial._);
  4874. // 如果函数参数足够则调用传入的函数
  4875. if (realArgs.length >= fn.length) {
  4876. return fn(...realArgs);
  4877. }
  4878. /**
  4879. * 最终返回的函数
  4880. * @param otherArgs 接受任意参数
  4881. * @returns 返回一个函数,或者函数调用完成返回结果
  4882. */
  4883. function innerFn(...otherArgs) {
  4884. // 记录需要移除补到前面的参数
  4885. const removeIndexSet = new Set();
  4886. let i = 0;
  4887. const newArgs = args.map(arg => {
  4888. if (arg !== partial._ ||
  4889. otherArgs[i] === undefined ||
  4890. otherArgs[i] === partial._) {
  4891. return arg;
  4892. }
  4893. removeIndexSet.add(i);
  4894. // 每次补偿前面的 partial._ 参数计数器 +1
  4895. return otherArgs[i++];
  4896. });
  4897. const newOtherArgs = otherArgs.filter((_v, i) => !removeIndexSet.has(i));
  4898. return partial(fn, ...newArgs, ...newOtherArgs);
  4899. }
  4900. // 定义偏函数的剩余参数长度,便于在其他地方进行部分参数应用
  4901. // 注: 不使用 length 属性的原因是 length 属性
  4902. innerFn._length = fn.length - args.filter(arg => arg !== partial._).length;
  4903. // 自定义 toString 函数便于调试
  4904. innerFn.toString = () => `name: ${fn.name}, args: [${args.map(o => o.toString()).join(', ')}]`;
  4905. innerFn._partial = true;
  4906. return innerFn;
  4907. }
  4908. /**
  4909. * 偏的占位符,需要应用后面的参数时使用
  4910. * 例如 {@link partial(fn)(partial._, 1)} 意味着函数 fn 的第二个参数将被确定为 1
  4911. */
  4912. partial._ = Symbol('_');
  4913.  
  4914. /**
  4915. * 事件工具类
  4916. */
  4917. class EventUtil {
  4918. static add(dom, type, listener, options) {
  4919. if (!EventUtil.listenerMap.has(dom)) {
  4920. EventUtil.listenerMap.set(dom, []);
  4921. }
  4922. EventUtil.listenerMap.get(dom).push({
  4923. type,
  4924. listener,
  4925. options,
  4926. });
  4927. dom.addEventListener(type, listener, options);
  4928. }
  4929. static remove(dom, type, listener, options) {
  4930. dom.removeEventListener(type, listener, options);
  4931. EventUtil.listenerMap.set(dom, (EventUtil.listenerMap.get(dom) || []).filter(cacheListener => cacheListener.type !== type ||
  4932. cacheListener.listener !== listener ||
  4933. cacheListener.options !== options));
  4934. }
  4935. static removeByType(dom, type, options) {
  4936. const listenerList = EventUtil.listenerMap.get(dom);
  4937. if (listenerList === undefined) {
  4938. return [];
  4939. }
  4940. const map = groupBy(listenerList, cacheListener => type === cacheListener.type && options === cacheListener.options);
  4941. const removeCacheListenerList = map.get(true) || [];
  4942. const retainCacheListenerList = map.get(true) || [];
  4943. EventUtil.listenerMap.set(dom, retainCacheListenerList);
  4944. return removeCacheListenerList.map(cacheListener => {
  4945. dom.removeEventListener(cacheListener.type, cacheListener.listener, cacheListener.options);
  4946. return cacheListener;
  4947. });
  4948. }
  4949. }
  4950. /**
  4951. * 缓存的事件监听对象映射表
  4952. */
  4953. EventUtil.listenerMap = new Map();
  4954.  
  4955. /**
  4956. * 加载一个远程样式文件
  4957. * @param href 远程 CSS 样式路径
  4958. * @returns 等待异步加载样式完成
  4959. */
  4960. function loadStyle(href) {
  4961. return new Promise((resolve, reject) => {
  4962. const link = document.createElement('link');
  4963. link.rel = 'stylesheet';
  4964. link.href = href;
  4965. link.addEventListener('load', () => resolve());
  4966. link.addEventListener('error', reject);
  4967. document.body.appendChild(link);
  4968. });
  4969. }
  4970.  
  4971. /**
  4972. * 补 0 函数
  4973. * @param time 为时分秒补首位 0
  4974. * @returns 补零得到的字符串
  4975. */
  4976. function zeroPadding(time) {
  4977. return (time >= 10 ? '' : '0') + time;
  4978. }
  4979. /**
  4980. * 秒表
  4981. * 标准格式 `HH:mm:ss`
  4982. * 主要适用场景是格式化/解析时间差值
  4983. */
  4984. class Stopwatch {
  4985. /**
  4986. * 格式化一个以秒为单位的绝对时间为标准时间格式的字符串
  4987. * @param time 时间/s
  4988. * @returns 格式化后的字符串
  4989. */
  4990. static format(time) {
  4991. const seconds = time % 60;
  4992. const minutes = Math.floor(time / 60) % 60;
  4993. const hours = Math.floor(time / 60 / 60);
  4994. return `${zeroPadding(hours)}:${zeroPadding(minutes)}:${zeroPadding(seconds)}`;
  4995. }
  4996. /**
  4997. * 解析一个标准的时间字符串为秒为单位的绝对时间
  4998. * @param str 时间字符串
  4999. * @returns 解析得到的时间/s
  5000. */
  5001. static parse(str) {
  5002. const [, hours, minutes, seconds] = /(\d+):(\d+):(\d+)/.exec(str);
  5003. return (parseInt(hours) * 60 * 60 + parseInt(minutes) * 60 + parseInt(seconds));
  5004. }
  5005. }
  5006.  
  5007. /**
  5008. * 提醒用户当前页面有正在执行的操作,暂时不能离开
  5009. * 注:用户实际上可以不管该提醒
  5010. * @param fn 谓词,是否要提醒不要关闭
  5011. * @returns 返回删除这个事件监听的函数
  5012. */
  5013. function remindLeavePage(fn = () => false) {
  5014. const listener = (e) => {
  5015. if (fn()) {
  5016. return false;
  5017. }
  5018. const confirmationMessage = '请不要关闭页面';
  5019. e.returnValue = confirmationMessage; //Gecko + IE
  5020. return confirmationMessage; //Webkit, Safari, Chrome etc.
  5021. };
  5022. window.addEventListener('beforeunload', listener);
  5023. return () => window.removeEventListener('beforeunload', listener);
  5024. }
  5025.  
  5026. /**
  5027. * 事件总线
  5028. * 实际上就是发布订阅模式的一种简单实现
  5029. * 类型定义受到 {@link https://github.com/andywer/typed-emitter/blob/master/index.d.ts} 的启发,不过只需要声明参数就好了,而不需要返回值(应该是 {@code void})
  5030. */
  5031. class EventEmitter {
  5032. constructor() {
  5033. this.events = new Map();
  5034. }
  5035. /**
  5036. * 添加一个事件监听程序
  5037. * @param type 监听类型
  5038. * @param callback 处理回调
  5039. * @returns {@code this}
  5040. */
  5041. add(type, callback) {
  5042. const callbacks = this.events.get(type) || [];
  5043. callbacks.push(callback);
  5044. this.events.set(type, callbacks);
  5045. return this;
  5046. }
  5047. /**
  5048. * 移除一个事件监听程序
  5049. * @param type 监听类型
  5050. * @param callback 处理回调
  5051. * @returns {@code this}
  5052. */
  5053. remove(type, callback) {
  5054. const callbacks = this.events.get(type) || [];
  5055. this.events.set(type, callbacks.filter((fn) => fn !== callback));
  5056. return this;
  5057. }
  5058. /**
  5059. * 移除一类事件监听程序
  5060. * @param type 监听类型
  5061. * @returns {@code this}
  5062. */
  5063. removeByType(type) {
  5064. this.events.delete(type);
  5065. return this;
  5066. }
  5067. /**
  5068. * 触发一类事件监听程序
  5069. * @param type 监听类型
  5070. * @param args 处理回调需要的参数
  5071. * @returns {@code this}
  5072. */
  5073. emit(type, ...args) {
  5074. const callbacks = this.events.get(type) || [];
  5075. callbacks.forEach(fn => {
  5076. fn(...args);
  5077. });
  5078. return this;
  5079. }
  5080. /**
  5081. * 获取一类事件监听程序
  5082. * @param type 监听类型
  5083. * @returns 一个只读的数组,如果找不到,则返回空数组 {@code []}
  5084. */
  5085. listeners(type) {
  5086. return Object.freeze(this.events.get(type) || []);
  5087. }
  5088. }
  5089.  
  5090. /**
  5091. * 一个简单的微任务队列辅助类,使用(宏)命令模式实现
  5092. * 注:该 class 是为了解决问题 https://segmentfault.com/q/1010000019115775
  5093. */
  5094. class MicrotaskQueue {
  5095. constructor() {
  5096. // task 列表
  5097. this.tasks = [];
  5098. // 当前是否存在正在执行的 task
  5099. this.lock = false;
  5100. }
  5101. add(func) {
  5102. this.tasks.push(func);
  5103. this.execute();
  5104. return this;
  5105. }
  5106. execute() {
  5107. if (this.lock) {
  5108. return;
  5109. }
  5110. this.lock = true;
  5111. const goNext = () => {
  5112. if (this.tasks.length) {
  5113. const task = this.tasks.shift();
  5114. compatibleAsync(task(), () => goNext());
  5115. }
  5116. else {
  5117. this.lock = false;
  5118. }
  5119. };
  5120. Promise.resolve().then(goNext);
  5121. }
  5122. }
  5123.  
  5124. /**
  5125. * 取值的字符串
  5126. */
  5127. const rangeStr = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  5128. const rangeLen = rangeStr.length;
  5129. /**
  5130. * 生成一个随机字符串
  5131. * @param len
  5132. */
  5133. function randomStr(len) {
  5134. let res = '';
  5135. for (let i = 0; i < len; i++) {
  5136. res += rangeStr.charAt(Math.floor(Math.random() * rangeLen));
  5137. }
  5138. return res;
  5139. }
  5140.  
  5141. /**
  5142. * 解析字段字符串为数组
  5143. * @param str 字段字符串
  5144. * @returns 字符串数组,数组的 `[]` 取法会被解析为数组的一个元素
  5145. */
  5146. function parseFieldStr(str) {
  5147. return str
  5148. .split(/[\.\[]/)
  5149. .map(k => (/\]$/.test(k) ? k.slice(0, k.length - 1) : k));
  5150. }
  5151.  
  5152. /**
  5153. * 安全的深度获取对象的字段
  5154. * TODO 该函数尚处于早期测试阶段
  5155. * 注: 只要获取字段的值为 {@type null|undefined},就会直接返回 {@param defVal}
  5156. * 类似于 ES2019 的可选调用链特性: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/%E5%8F%AF%E9%80%89%E9%93%BE
  5157. * @param obj 获取的对象
  5158. * @param fields 字段字符串或数组
  5159. * @param [defVal] 取不到值时的默认值,默认为 null
  5160. */
  5161. function get(obj, fields, defVal = null) {
  5162. if (TypeValidator.isString(fields)) {
  5163. fields = parseFieldStr(fields);
  5164. }
  5165. let res = obj;
  5166. for (const field of fields) {
  5167. try {
  5168. res = Reflect.get(res, field);
  5169. if (isNullOrUndefined(res)) {
  5170. return defVal;
  5171. }
  5172. }
  5173. catch (e) {
  5174. return defVal;
  5175. }
  5176. }
  5177. return res;
  5178. }
  5179.  
  5180. /**
  5181. * 安全的深度设置对象的字段
  5182. * TODO 该函数尚处于早期测试阶段
  5183. * 注: 只要设置字段的值为 {@type null|undefined},就会直接返回 {@param defVal}
  5184. * 类似于 ES2019 的可选调用链特性: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/%E5%8F%AF%E9%80%89%E9%93%BE
  5185. * @param obj 设置的对象
  5186. * @param fields 字段字符串或数组
  5187. * @param [val] 设置字段的值
  5188. */
  5189. function set(obj, fields, val) {
  5190. if (TypeValidator.isString(fields)) {
  5191. fields = parseFieldStr(fields);
  5192. }
  5193. let res = obj;
  5194. for (let i = 0, len = fields.length; i < len; i++) {
  5195. const field = fields[i];
  5196. if (i === len - 1) {
  5197. res[field] = val;
  5198. return true;
  5199. }
  5200. res = res[field];
  5201. if (!TypeValidator.isObject(res)) {
  5202. return false;
  5203. }
  5204. }
  5205. return false;
  5206. }
  5207.  
  5208. /**
  5209. * 获取当前选中的文本
  5210. * @returns 当前选中的文本
  5211. */
  5212. function getSelectText() {
  5213. return getSelection().toString();
  5214. }
  5215.  
  5216. /**
  5217. * 获取图片的尺寸
  5218. * @param url
  5219. */
  5220. function imageSize(url) {
  5221. return new Promise((resolve, reject) => {
  5222. const image = new Image();
  5223. image.addEventListener('load', function () {
  5224. resolve({
  5225. width: this.width,
  5226. height: this.height,
  5227. });
  5228. });
  5229. image.addEventListener('error', ev => {
  5230. reject(ev.error);
  5231. });
  5232. image.src = url;
  5233. });
  5234. }
  5235.  
  5236. /**
  5237. * 获取鼠标的位置
  5238. * @param e 触发的鼠标事件对象
  5239. * @returns 鼠标的坐标
  5240. */
  5241. function getMousePos(e) {
  5242. const scrollX = document.documentElement.scrollLeft || document.body.scrollLeft;
  5243. const scrollY = document.documentElement.scrollTop || document.body.scrollTop;
  5244. const x = e.pageX || e.clientX + scrollX;
  5245. const y = e.pageY || e.clientY + scrollY;
  5246. return { x, y };
  5247. }
  5248.  
  5249. /**
  5250. * 将多个并发异步调用合并为一次批处理
  5251. * @param handle 批处理的函数
  5252. * @param ms 等待的时长(时间越长则可能合并的调用越多,否则将使用微任务只合并一次同步执行的所有调用)
  5253. */
  5254. function batch(handle, ms = 0) {
  5255. //参数 => 结果 映射
  5256. const resultCache = new Map();
  5257. //参数 => 次数的映射
  5258. const paramCache = new Map();
  5259. //当前是否被锁定
  5260. let lock = false;
  5261. return function (...args) {
  5262. return __awaiter(this, void 0, void 0, function* () {
  5263. const key = JSON.stringify(args);
  5264. paramCache.set(key, (paramCache.get(key) || 0) + 1);
  5265. yield Promise.all([wait(() => resultCache.has(key) || !lock), wait(ms)]);
  5266. if (!resultCache.has(key)) {
  5267. try {
  5268. lock = true;
  5269. Array.from(yield handle(Array.from(paramCache.keys()).map(v => JSON.parse(v)))).forEach(([k, v]) => {
  5270. resultCache.set(JSON.stringify(k), v);
  5271. });
  5272. }
  5273. finally {
  5274. lock = false;
  5275. }
  5276. }
  5277. const value = resultCache.get(key);
  5278. paramCache.set(key, paramCache.get(key) - 1);
  5279. if ((paramCache.get(key) || 0) <= 0) {
  5280. paramCache.delete(key);
  5281. resultCache.delete(key);
  5282. }
  5283. if (value instanceof Error) {
  5284. resultCache.delete(key);
  5285. throw value;
  5286. }
  5287. return value;
  5288. });
  5289. };
  5290. }
  5291.  
  5292. /**
  5293. * 从一个对象中挑选出来几个指定的字段
  5294. * @param obj 指定对象
  5295. * @param fieldList 指定对象字段列表
  5296. * @returns 返回挑选字段组成的新对象
  5297. */
  5298. function pick(obj, ...fieldList) {
  5299. const set = new Set(fieldList);
  5300. return Object.entries(obj).reduce((res, [k, v]) => {
  5301. if (set.has(k)) {
  5302. Reflect.set(res, k, v);
  5303. }
  5304. return res;
  5305. }, {});
  5306. }
  5307.  
  5308. exports.AntiDebug = AntiDebug;
  5309. exports.ArrayValidator = ArrayValidator;
  5310. exports.AsyncArray = AsyncArray;
  5311. exports.CacheUtil = CacheUtil;
  5312. exports.CombinedPredicate = CombinedPredicate;
  5313. exports.DateConstants = DateConstants;
  5314. exports.DateFormatter = DateFormatter;
  5315. exports.EventEmitter = EventEmitter;
  5316. exports.EventUtil = EventUtil;
  5317. exports.FetchLimiting = FetchLimiting;
  5318. exports.LocalStorageCache = LocalStorageCache;
  5319. exports.Locker = Locker;
  5320. exports.Logger = Logger;
  5321. exports.MemoryCacheFactory = MemoryCacheFactory;
  5322. exports.MicrotaskQueue = MicrotaskQueue;
  5323. exports.NodeBridgeUtil = NodeBridgeUtil;
  5324. exports.PathUtil = PathUtil;
  5325. exports.PubSubMachine = PubSubMachine;
  5326. exports.StateMachine = StateMachine;
  5327. exports.Stopwatch = Stopwatch;
  5328. exports.StringStyleUtil = StringStyleUtil;
  5329. exports.StringValidator = StringValidator;
  5330. exports.TypeValidator = TypeValidator;
  5331. exports.aggregation = aggregation;
  5332. exports.and = and;
  5333. exports.antiDebug = antiDebug;
  5334. exports.appends = appends;
  5335. exports.arrayDiffBy = arrayDiffBy;
  5336. exports.arrayToMap = arrayToMap;
  5337. exports.arrayValidator = arrayValidator;
  5338. exports.asIterator = asIterator;
  5339. exports.assign = assign;
  5340. exports.async = async;
  5341. exports.asyncFlatMap = asyncFlatMap;
  5342. exports.asyncLimiting = asyncLimiting;
  5343. exports.autoIncrement = autoIncrement;
  5344. exports.batch = batch;
  5345. exports.blankToNull = blankToNull;
  5346. exports.blankToNullField = blankToNullField;
  5347. exports.bridge = bridge;
  5348. exports.cacheUtil = cacheUtil;
  5349. exports.compare = compare;
  5350. exports.compatibleAsync = compatibleAsync;
  5351. exports.compose = compose;
  5352. exports.concatMap = concatMap;
  5353. exports.copyText = copyText;
  5354. exports.createElByString = createElByString;
  5355. exports.curry = curry;
  5356. exports.dateBetween = dateBetween;
  5357. exports.dateConstants = dateConstants;
  5358. exports.dateEnhance = dateEnhance;
  5359. exports.dateFormat = dateFormat;
  5360. exports.dateParse = dateParse;
  5361. exports.debounce = debounce;
  5362. exports.deepExcludeFields = deepExcludeFields;
  5363. exports.deepFreeze = deepFreeze;
  5364. exports.deepProxy = deepProxy;
  5365. exports.deletes = deletes;
  5366. exports.deny = deny;
  5367. exports.diffBy = diffBy;
  5368. exports.download = download;
  5369. exports.downloadString = downloadString;
  5370. exports.downloadUrl = downloadUrl;
  5371. exports.emptyAllField = emptyAllField;
  5372. exports.emptyFunc = emptyFunc;
  5373. exports.excludeFields = excludeFields;
  5374. exports.excludeFieldsDeep = excludeFieldsDeep;
  5375. exports.extractFieldMap = extractFieldMap;
  5376. exports.fetchTimeout = fetchTimeout;
  5377. exports.fill = fill;
  5378. exports.filterItems = filterItems;
  5379. exports.findIndex = findIndex;
  5380. exports.flatMap = flatMap;
  5381. exports.floatEquals = floatEquals;
  5382. exports.formDataToArray = formDataToArray;
  5383. exports.format = format;
  5384. exports.get = get;
  5385. exports.getCookies = getCookies;
  5386. exports.getCursorPosition = getCursorPosition;
  5387. exports.getCusorPostion = getCusorPostion;
  5388. exports.getKFn = getKFn;
  5389. exports.getMousePos = getMousePos;
  5390. exports.getObjectEntries = getObjectEntries;
  5391. exports.getObjectKeys = getObjectKeys;
  5392. exports.getSelectText = getSelectText;
  5393. exports.getYearWeek = getYearWeek;
  5394. exports.groupBy = groupBy;
  5395. exports.imageSize = imageSize;
  5396. exports.insertText = insertText;
  5397. exports.isBlank = isBlank;
  5398. exports.isEditable = isEditable;
  5399. exports.isEmpty = isEmpty;
  5400. exports.isFloat = isFloat;
  5401. exports.isNullOrUndefined = isNullOrUndefined;
  5402. exports.isNumber = isNumber;
  5403. exports.isRange = isRange;
  5404. exports.lastFocus = lastFocus;
  5405. exports.listToTree = listToTree;
  5406. exports.loadResource = loadResource;
  5407. exports.loadScript = loadScript;
  5408. exports.loadStyle = loadStyle;
  5409. exports.logger = logger;
  5410. exports.mapToObject = mapToObject;
  5411. exports.mergeMap = mergeMap;
  5412. exports.nodeBridgeUtil = nodeBridgeUtil;
  5413. exports.not = not;
  5414. exports.objToFormData = objToFormData;
  5415. exports.objectToMap = objectToMap;
  5416. exports.once = once;
  5417. exports.onceOfSameParam = onceOfSameParam;
  5418. exports.or = or;
  5419. exports.parseUrl = parseUrl;
  5420. exports.partial = partial;
  5421. exports.pathUtil = pathUtil;
  5422. exports.pick = pick;
  5423. exports.randomInt = randomInt;
  5424. exports.randomStr = randomStr;
  5425. exports.range = range;
  5426. exports.readLocal = readLocal;
  5427. exports.remindLeavePage = remindLeavePage;
  5428. exports.removeEl = removeEl;
  5429. exports.removeText = removeText;
  5430. exports.repeatedCall = repeatedCall;
  5431. exports.returnItself = returnItself;
  5432. exports.returnReasonableItself = returnReasonableItself;
  5433. exports.safeExec = safeExec;
  5434. exports.segmentation = segmentation;
  5435. exports.set = set;
  5436. exports.setCursorPosition = setCursorPosition;
  5437. exports.setCusorPostion = setCusorPostion;
  5438. exports.sets = sets;
  5439. exports.singleModel = singleModel;
  5440. exports.sleep = sleep;
  5441. exports.sortBy = sortBy;
  5442. exports.spliceParams = spliceParams;
  5443. exports.strToArrayBuffer = strToArrayBuffer;
  5444. exports.strToDate = strToDate;
  5445. exports.stringValidator = stringValidator;
  5446. exports.switchMap = switchMap;
  5447. exports.throttle = throttle;
  5448. exports.timing = timing;
  5449. exports.toLowerCase = toLowerCase;
  5450. exports.toObject = toObject;
  5451. exports.toString = toString$1;
  5452. exports.toUpperCase = toUpperCase;
  5453. exports.toggleClass = toggleClass;
  5454. exports.treeMapping = treeMapping;
  5455. exports.treeToList = treeToList;
  5456. exports.trySometime = trySometime;
  5457. exports.trySometimeParallel = trySometimeParallel;
  5458. exports.uniqueBy = uniqueBy;
  5459. exports.wait = wait;
  5460. exports.waitResource = waitResource;
  5461. exports.watch = watch;
  5462. exports.watchEventListener = watchEventListener;
  5463. exports.watchObject = watchObject;
  5464.  
  5465. Object.defineProperty(exports, '__esModule', { value: true });
  5466.  
  5467. })));
  5468. //# sourceMappingURL=rx-util.js.map