onebots
Version:
基于icqq的多例oneBot实现
53 lines (52 loc) • 1.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.remove = remove;
exports.deepClone = deepClone;
exports.findLastIndex = findLastIndex;
exports.trimQuote = trimQuote;
function remove(list, item) {
const index = list.indexOf(item);
if (index !== -1)
list.splice(index, 1);
}
function deepClone(obj) {
if (typeof obj !== "object")
return obj;
if (Array.isArray(obj))
return obj.map(deepClone);
const Constructor = obj.constructor;
let newObj = Constructor();
for (let key in obj) {
newObj[key] = deepClone(obj[key]);
}
return newObj;
}
/**
* 寻找数组中最后一个符合条件的元素下标
* @param list 数组
* @param predicate 条件
* @returns {number} 元素下标,未找到返回-1
*/
function findLastIndex(list, predicate) {
for (let i = list.length - 1; i >= 0; i--) {
if (predicate(list[i], i))
return i;
}
return -1;
}
function trimQuote(str) {
const quotes = [
['"', '"'],
["'", "'"],
["`", "`"],
["“", "”"],
["‘", "’"],
];
for (let i = 0; i < quotes.length; i++) {
const [start, end] = quotes[i];
if (str.startsWith(start) && str.endsWith(end)) {
return str.slice(1, -1);
}
}
return str;
}