archieve-projects/微信机器人/node_modules/cmd-ts/dist/cjs/utils.js

86 lines
1.9 KiB
JavaScript
Executable File

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.flatten = exports.flatMap = exports.enumerate = exports.entries = exports.groupBy = exports.padNoAnsi = void 0;
const strip_ansi_1 = __importDefault(require("strip-ansi"));
/**
* @ignore
*/
function padNoAnsi(str, length, place) {
const noAnsiStr = strip_ansi_1.default(str);
if (length < noAnsiStr.length)
return str;
const pad = Array(length - noAnsiStr.length + 1).join(' ');
if (place === 'end') {
return str + pad;
}
else {
return pad + str;
}
}
exports.padNoAnsi = padNoAnsi;
/**
* Group an array by a function that returns the key
*
* @ignore
*/
function groupBy(objs, f) {
var _a;
const result = {};
for (const obj of objs) {
const key = f(obj);
result[key] = (_a = result[key]) !== null && _a !== void 0 ? _a : [];
result[key].push(obj);
}
return result;
}
exports.groupBy = groupBy;
/**
* A better typed version of `Object.entries`
*
* @ignore
*/
function entries(obj) {
return Object.entries(obj);
}
exports.entries = entries;
/**
* Enumerate over a list, to get a pair of [index, value]
*
* @ignore
*/
function* enumerate(arr) {
for (let i = 0; i < arr.length; i++) {
yield [i, arr[i]];
}
}
exports.enumerate = enumerate;
/**
* Array#flatMap polyfill
*
* @ignore
*/
function flatMap(xs, fn) {
const results = [];
for (const x of xs) {
results.push(...fn(x));
}
return results;
}
exports.flatMap = flatMap;
/**
* Flatten an array
*
* @ignore
*/
function flatten(xs) {
const results = [];
for (const x of xs) {
results.push(...x);
}
return results;
}
exports.flatten = flatten;
//# sourceMappingURL=utils.js.map