cis-api-tool
Version:
根据 swagger/yapi/apifox 的接口定义生成 TypeScript/JavaScript 的接口类型及其请求函数代码。
162 lines (110 loc) • 3.29 kB
JavaScript
import { forOwn, has, isPlainObject } from "lodash";
//#region src/vutils/function.ts
/**
* 首先,每一行紧跟前导空白的插入值为多行时,保持缩进。
* 然后,移除每一行的公共前导空白。
*
* @public
* @param literals 字面值
* @param interpolations 插入值
* @returns 返回处理后的结果
* @example
* ```typescript
* dedent` a\n b` // => 'a\nb'
* ```
*/
function dedent(literals, ...interpolations) {
const text = Array.isArray(literals) ? (() => {
let result = "";
for (let i = 0; i < interpolations.length; i++) {
const literal = literals[i];
let interpolation = interpolations[i];
const match = literal.match(/(?:^|[\r\n]+)([^\S\r\n]*)$/);
if (match && match[1]) interpolation = String(interpolation).replace(/([\r\n]+)(?=[^\r\n])/g, `$1${match[1]}`);
result += literal;
result += interpolation;
}
result += literals[literals.length - 1];
return result;
})() : literals;
let commonLeadingWhitespace;
let firstLineIndex;
let lastLineIndex;
const lines = text.split(/[\r\n]/g);
for (let index = 0; index < lines.length; index++) {
const leadingWhitespace = lines[index].match(/^\s*/)[0];
if (leadingWhitespace.length !== lines[index].length) {
lastLineIndex = index;
if (firstLineIndex == null) firstLineIndex = index;
if (commonLeadingWhitespace == null || leadingWhitespace.length < commonLeadingWhitespace.length) commonLeadingWhitespace = leadingWhitespace;
}
}
return commonLeadingWhitespace == null ? text : lines.slice(firstLineIndex, lastLineIndex + 1).map((line) => line.substr(commonLeadingWhitespace.length)).join("\n");
}
/**
* @description 等待一段时间 resolve。
* @public
* @param milliseconds 等待时间(毫秒)
* @param value resolve 值
* @example
* ```typescript
* wait(1000).then(() => {
* console.log('ok')
* }) // => 1秒后在控制台打印字符串: ok
* ```
*/
function wait(milliseconds, value) {
let timer;
const result = new Promise((resolve) => {
timer = setTimeout(() => resolve(value), milliseconds);
});
result.cancel = () => clearTimeout(timer);
return result;
}
/**
* @description 等待一段时间后 reject。
* @public
* @param milliseconds 等待时间(毫秒)
* @param value reject 值
* @example
* ```typescript
* wait.reject(1000).catch(() => {
* console.log('ok')
* }) // => 1秒后在控制台打印字符串: ok
* ```
*/
wait.reject = function reject(milliseconds, value) {
const waitRes = wait(milliseconds);
const res = waitRes.then(() => Promise.reject(value));
res.cancel = waitRes.cancel;
return res;
};
/**
* 遍历对象和数组。
*
* @param value 要遍历的值
* @param callback 遍历回调
* @returns 返回结果
* @example
* ```typescript
* traverse([1, 2, {3: 4}], value => {
* console.log(value)
* // => 1
* // => 2
* // => {3: 4}
* // => 4
* })
* ```
*/
function traverse(value, callback) {
if (Array.isArray(value)) value.forEach((item, index) => {
callback(item, index, value);
if (value[index] !== void 0) traverse(item, callback);
});
else if (isPlainObject(value)) forOwn(value, (item, key) => {
callback(item, key, value);
if (has(value, key)) traverse(item, callback);
});
}
//#endregion
export { dedent, traverse, wait };