-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathformat.ts
More file actions
38 lines (30 loc) · 1.22 KB
/
format.ts
File metadata and controls
38 lines (30 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import data from "./metadata.json";
import { createObjectCsvWriter } from "csv-writer";
const attributes = data[0].attributes.map((attribute: { trait_type: string, value: string; }) => attribute.trait_type);
const formatJson = (json: any) => {
return json.map((nft: any) => {
const formattedNft = {
name: nft.name,
description: nft.description,
dna: nft.dna,
edition: nft.edition,
date: nft.date,
};
attributes.map((attribute: any) => {
const findAttribute = nft.attributes.find(({ trait_type }: { trait_type: string; }) => trait_type === attribute);
const value = findAttribute?.value || "";
formattedNft[attribute] = value.trim() === "None" ? "" : value.trim();
});
return formattedNft;
});
};
const metadata = formatJson(data);
const header = [
{ id: 'name', title: 'name' },
{ id: 'description', title: 'description' },
{ id: 'dna', title: 'dna' },
{ id: 'edition', title: 'edition' },
{ id: 'date', title: 'date' },
];
attributes.map((attribute: string) => header.push({ id: attribute, title: attribute }));
createObjectCsvWriter({ path: "./metadata.csv", header }).writeRecords(metadata).then(() => console.log('The CSV file was written successfully'));