rx-util

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

当前为 2020-04-14 提交的版本,查看 最新版本

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