auto-account-machine/browser-automation-ts/src/tools/card/formatter.ts
2025-11-21 17:59:49 +08:00

49 lines
1003 B
TypeScript

/**
* Formatter - 输出格式化
*/
const { OUTPUT_FORMATS } = require('./config');
class Formatter {
constructor() {
this.formats = OUTPUT_FORMATS;
}
/**
* 格式化单张卡
* @param {Object} card - 卡信息
* @param {string} format - 格式类型
* @returns {string}
*/
format(card, format = 'pipe') {
const formatter = this.formats[format];
if (!formatter) {
throw new Error(`Unknown format: ${format}`);
}
return formatter.formatter(card);
}
/**
* 格式化多张卡
* @param {Array} cards - 卡信息数组
* @param {string} format - 格式类型
* @returns {string}
*/
formatBatch(cards, format = 'pipe') {
return cards.map(card => this.format(card, format)).join('\n');
}
/**
* 获取所有支持的格式
* @returns {Array}
*/
getSupportedFormats() {
return Object.keys(this.formats).map(key => ({
id: key,
name: this.formats[key].name
}));
}
}
module.exports = Formatter;