UNPKG

z-utils-ts

Version:

使用TypeScript编写的工具函数库

96 lines (85 loc) 3.51 kB
'use strict'; function parseUrl(url) { const urlObj = new URL(url); return { protocol: urlObj.protocol, host: urlObj.host, pathname: urlObj.pathname, search: urlObj.search, hash: urlObj.hash, }; } /** * 分组函数 * @param {Array} arr * @param {string|Function} generatekey * @returns {object} */ function group(arr, generatekey) { if (typeof generatekey === 'string') { const proName = generatekey; generatekey = (item) => item[proName]; } return arr.reduce((result, currentValue) => { const key = generatekey(currentValue); (result[key] = result[key] || []).push(currentValue); return result; }, {}); } /****************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */ function __awaiter(thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); } typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { var e = new Error(message); return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; }; function computeSHA256Hash(data) { return __awaiter(this, void 0, void 0, function* () { const encoder = new TextEncoder(); const dataBuffer = encoder.encode(data); const hashBuffer = yield crypto.subtle.digest("SHA-256", dataBuffer); const hashArray = Array.from(new Uint8Array(hashBuffer)); let len = hashArray.length; let result = ""; for (let i = 0; i < len; i++) { hashArray[i] = hashArray[i] & 0xFF; let byte = hashArray[i]; result += padStart(byte.toString(16), 2); } return result; }); } function padStart(num, length, symbol = '0') { while (num.length < length) { num = symbol + num; } return num; } // function padEnd(num: string, length: number, symbol: string = '0'): string { // while (num.length < length) { // num = num + symbol; // } // return num; // } exports.computeSHA256Hash = computeSHA256Hash; exports.group = group; exports.parseUrl = parseUrl;