120 lines
3.2 KiB
TypeScript
120 lines
3.2 KiB
TypeScript
import { readFileSync, writeFileSync } from "fs";
|
||
import { dirname, join } from "path";
|
||
import { fileURLToPath } from "url";
|
||
|
||
interface ColorItem {
|
||
"color-name": string;
|
||
color: string;
|
||
}
|
||
|
||
interface ColorGroupItem {
|
||
"color-name": string;
|
||
"color-title": string;
|
||
color: string;
|
||
}
|
||
|
||
interface ColorMap {
|
||
[color: string]: ColorGroupItem[];
|
||
}
|
||
|
||
const __filename = fileURLToPath(import.meta.url);
|
||
const __dirname = dirname(__filename);
|
||
|
||
function parseColorsToMap(inputFile: string): ColorMap {
|
||
console.log(`读取文件: ${inputFile}`);
|
||
|
||
const jsonData = readFileSync(inputFile, "utf-8");
|
||
const inputData: { [key: string]: ColorItem[] } = JSON.parse(jsonData);
|
||
|
||
const colorMap: ColorMap = {};
|
||
|
||
let totalColors = 0;
|
||
let duplicateColors = 0;
|
||
|
||
for (const [colorTitle, colorList] of Object.entries(inputData)) {
|
||
console.log(`处理品牌: ${colorTitle} (${colorList.length} 个颜色)`);
|
||
|
||
for (const colorItem of colorList) {
|
||
const color = colorItem.color;
|
||
const colorName = colorItem["color-name"];
|
||
|
||
totalColors++;
|
||
|
||
if (!colorMap[color]) {
|
||
colorMap[color] = [];
|
||
} else {
|
||
duplicateColors++;
|
||
}
|
||
|
||
colorMap[color].push({
|
||
"color-name": colorName,
|
||
"color-title": colorTitle,
|
||
color: color
|
||
});
|
||
}
|
||
}
|
||
|
||
console.log(`\n统计信息:`);
|
||
console.log(` 处理的品牌数: ${Object.keys(inputData).length}`);
|
||
console.log(` 总颜色条目数: ${totalColors}`);
|
||
console.log(` 重复颜色数: ${duplicateColors}`);
|
||
console.log(` 唯一颜色数: ${Object.keys(colorMap).length}`);
|
||
|
||
return colorMap;
|
||
}
|
||
|
||
function writeColorMap(outputFile: string, colorMap: ColorMap): void {
|
||
console.log(`\n写入文件: ${outputFile}`);
|
||
writeFileSync(outputFile, JSON.stringify(colorMap, null, 2), "utf-8");
|
||
console.log(`✓ 文件已保存`);
|
||
}
|
||
|
||
function displaySampleData(colorMap: ColorMap): void {
|
||
console.log(`\n=== 示例数据 ===\n`);
|
||
|
||
const colorKeys = Object.keys(colorMap);
|
||
const sampleColor = colorKeys[0];
|
||
|
||
console.log(`颜色 ${sampleColor} 的引用数: ${colorMap[sampleColor].length}`);
|
||
console.log(`详细信息:`);
|
||
colorMap[sampleColor].forEach((item, index) => {
|
||
console.log(` ${index + 1}. 品牌: ${item["color-title"]}, 色号: ${item["color-name"]}`);
|
||
});
|
||
|
||
console.log(`\n=== 按引用数排序的颜色(前10个)===\n`);
|
||
|
||
const sortedColors = colorKeys
|
||
.map(color => ({
|
||
color,
|
||
count: colorMap[color].length
|
||
}))
|
||
.sort((a, b) => b.count - a.count)
|
||
.slice(0, 10);
|
||
|
||
sortedColors.forEach((item, index) => {
|
||
console.log(`${index + 1}. ${item.color}: ${item.count} 次引用`);
|
||
const brands = colorMap[item.color].map(c => c["color-title"]);
|
||
console.log(` 品牌: ${[...new Set(brands)].join(", ")}`);
|
||
});
|
||
}
|
||
|
||
function main() {
|
||
const inputFile = join(__dirname, "get-colors.json");
|
||
const outputFile = join(__dirname, "colorMap.json");
|
||
|
||
console.log("=== 颜色数据解析工具 ===\n");
|
||
|
||
try {
|
||
const colorMap = parseColorsToMap(inputFile);
|
||
writeColorMap(outputFile, colorMap);
|
||
displaySampleData(colorMap);
|
||
|
||
console.log(`\n=== 处理完成 ===`);
|
||
} catch (error) {
|
||
console.error(`\n✗ 错误:`, error);
|
||
process.exit(1);
|
||
}
|
||
}
|
||
|
||
main();
|