diff --git a/contributors/TEMPLATING.md b/contributors/TEMPLATING.md index 33a3d49db..6632eefa4 100644 --- a/contributors/TEMPLATING.md +++ b/contributors/TEMPLATING.md @@ -180,19 +180,15 @@ The special files and folders are: Most of the time you will use string arguments for templating, but sometimes you will need to add arrays, objects, bigints, etc. You can handle them however you want, but we're recommending to use the table below as a helper. -| Pattern | Template | Args | Result | -| --------------------------- | ---------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------- | -| Replace an object | `const replacedObj = ${JSON.stringify(replacedObj[0])}` | `const replacedObj = { key1: "Replaced", key2: "Object" }` | `const replacedObj = { key1: "Replaced", key2: "Object" }` | -| Replace an array | `const replacedArr = ${JSON.stringify(replacedArr[0])}` | `const replacedArr = ["Replaced", "Array"]` | `const replacedArr = ["Replaced", "Array"]` | -| Object, add new entries | `const mergedObj = {key1: 'value1', key2: 'value2', ${getStringifiedObjectContent(objToMerge[0])}}` | `const objToMerge = { key3: "Merged", key4: "Object" }` | `const mergedObj = { key1: "value1", key2: "value2", key3: "Merged", key4: "Object" };` | -| Array, add new items | `const arrWithAdditionalItems = ['a', 'b', ${getStringifiedArrayContent(arrayToSpread[0])}]` | `const arrayToSpread = ["Spread", "This"]` | `const arrWithAdditionalItems = ["a", "b", "Spread", "This"];` | -| Object, add new entries, v2 | `const mergedObj = ${JSON.stringify(structuredClone({key1: 'value1', key2: 'value2', ...objToMerge[0]}))}` | `const objToMerge = { key3: "Merged", key4: "Object" }` | `const mergedObj = { key1: "value1", key2: "value2", key3: "Merged", key4: "Object" };` | -| Array, add new items, v2 | `const arrWithAdditionalItems = ${JSON.stringify(structuredClone(['a', 'b', ...arrayToSpread[0]]))};` | `const arrayToSpread = ["Spread", "This"]` | `const arrWithAdditionalItems = ["a", "b", "Spread", "This"];` | -| BigInt, simple var | `const bigInt = BigInt("${someBigInt[0]}");` | `const someBigInt = 123n` | `const bigInt = BigInt("123");` | -| Simple object with bigints | `const simpleObjectWithBigInt = { key1: BigInt("${simpleObjectWithBigInt[0].key1}") };` | `const simpleObjectWithBigInt = { key1: 123n }` | `const simpleObjectWithBigInt = { key1: BigInt("123") };` | -| Complex object with bigints | `const objWithBigInt = JSON.parse('${JSON.stringify(objWithBigInt[0])}', bigintReviver)` | `const objWithBigInt = { key1: 123n }` | `const objWithBigInt = JSON.parse('{"key1":{"$bigint":"123"}}', bigintReviver);`, which is equal to `{ key1: 123n } ` | - -Don't forget to import `bigintReviver` from `~~/utils/scaffold-eth/bigint-reviver` in the template file for the last pattern. +| Pattern | Template | Args | Result | +| ----------------------- | ------------------------------------------------------------------------------------------- | ---------------------------------------------------------- | --------------------------------------------------------------------------------------- | +| Replace an object | `const replacedObj = ${deepStringify(replacedObj[0])}` | `const replacedObj = { key1: "Replaced", key2: "Object" }` | `const replacedObj = { key1: "Replaced", key2: "Object" }` | +| Replace an array | `const replacedArr = ${deepStringify(replacedArr[0])}` | `const replacedArr = ["Replaced", "Array"]` | `const replacedArr = ["Replaced", "Array"]` | +| Object, add new entries | `const mergedObj = ${deepStringify({ key1: "value1", key2: "value2", ...objToMerge[0] })};` | `const objToMerge = { key3: "Merged", key4: "Object" }` | `const mergedObj = { key1: "value1", key2: "value2", key3: "Merged", key4: "Object" };` | +| Array, add new items | `const arrWithAdditionalItems = ${deepStringify(['a', 'b', ...arrayToSpread[0]])}` | `const arrayToSpread = ["Spread", "This"]` | `const arrWithAdditionalItems = ["a", "b", "Spread", "This"]` | +| BigInt, simple var | `const bigInt = ${deepStringify(someBigInt[0])};` | `const someBigInt = 123n` | `const bigInt = 123n;` | +| Object with bigints | `const objectWithBigInt = ${deepStringify(objectWithBigInt[0])}` | `const objectWithBigInt = { key1: 123n }` | `const objectWithBigInt = { key1: 123n };` | +| | ## Merging package.json files diff --git a/src/tasks/copy-template-files.ts b/src/tasks/copy-template-files.ts index ded18e534..c00f43bd1 100644 --- a/src/tasks/copy-template-files.ts +++ b/src/tasks/copy-template-files.ts @@ -10,7 +10,6 @@ import { promisify } from "util"; import link from "../utils/link"; import { getArgumentFromExternalExtensionOption } from "../utils/external-extensions"; import { BASE_DIR, SOLIDITY_FRAMEWORKS, SOLIDITY_FRAMEWORKS_DIR } from "../utils/consts"; -import "../utils/bigint-to-json"; const EXTERNAL_EXTENSION_TMP_DIR = "tmp-external-extension"; diff --git a/src/utils/bigint-to-json.ts b/src/utils/bigint-to-json.ts deleted file mode 100644 index e4850f86f..000000000 --- a/src/utils/bigint-to-json.ts +++ /dev/null @@ -1,5 +0,0 @@ -// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#use_within_json -(BigInt.prototype as any).toJSON = function () { - // eslint-disable-next-line @typescript-eslint/no-unsafe-call - return { $bigint: this.toString() }; -}; diff --git a/templates/base/packages/nextjs/utils/scaffold-eth/bigint-reviver.ts b/templates/base/packages/nextjs/utils/scaffold-eth/bigint-reviver.ts deleted file mode 100644 index 8e004f331..000000000 --- a/templates/base/packages/nextjs/utils/scaffold-eth/bigint-reviver.ts +++ /dev/null @@ -1,5 +0,0 @@ -// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#use_within_json -export const bigintReviver = (key: string, value: unknown) => - value !== null && typeof value === "object" && "$bigint" in value && typeof value.$bigint === "string" - ? BigInt(value.$bigint) - : value; diff --git a/templates/utils.js b/templates/utils.js index 2ad51468d..2189577e8 100644 --- a/templates/utils.js +++ b/templates/utils.js @@ -1,3 +1,5 @@ +import { inspect } from "util"; + export const withDefaults = (template, expectedArgsDefaults, debug = false) => (receivedArgs) => { @@ -24,9 +26,4 @@ export const withDefaults = return template(argsWithDefault); }; -export const getStringifiedObjectContent = (obj) => - Object.entries(obj) - .map(([key, value]) => `${key}: ${JSON.stringify(value)},`) - .join("\n"); - -export const getStringifiedArrayContent = (arr) => arr.map(item => `${JSON.stringify(item)},`).join("\n"); +export const deepStringify = val => inspect(val, { depth: null, compact: true, maxArrayLength: null, maxStringLength: null }) \ No newline at end of file