Files
js/utils/parseAttributes.ts
T
Nacho IacovinoandJonas Daniels 0da7132e36 Make CSV use OS standard (#293)
* Make csv use OS standard

* Update utils/batch.ts

Co-authored-by: Jonas Daniels <[email protected]>

Co-authored-by: Jonas Daniels <[email protected]>
2022-07-26 00:35:33 +02:00

34 lines
924 B
TypeScript

import { EditionMutationInput } from "@3rdweb-sdk/react";
import type { Json, NFTMetadataInput } from "@thirdweb-dev/sdk";
export function parseAttributes<
T extends NFTMetadataInput | EditionMutationInput,
>(input: T): T {
return {
...input,
attributes: removeEmptyValues(input.attributes),
properties: removeEmptyValues(input.properties),
};
}
export function removeEmptyValues(data: NFTMetadataInput["attributes"]) {
if (!data) {
return data;
}
if (Array.isArray(data)) {
const parsedArray = data
.filter(({ value }) => value !== "")
.map(({ value, trait_type }) => ({
...(!!trait_type && { trait_type }),
value,
}));
return parsedArray.length === 0 ? undefined : parsedArray;
}
return Object.entries(data).reduce((acc, [key, value]) => {
if (value !== "") {
acc[key] = value;
}
return acc;
}, {} as Record<string, Json>);
}