Skip to content

Commit

Permalink
feat: update with deepStringify
Browse files Browse the repository at this point in the history
  • Loading branch information
rin-st committed Oct 14, 2024
1 parent 69dc555 commit eeea6df
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 30 deletions.
22 changes: 9 additions & 13 deletions contributors/TEMPLATING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion src/tasks/copy-template-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
5 changes: 0 additions & 5 deletions src/utils/bigint-to-json.ts

This file was deleted.

This file was deleted.

9 changes: 3 additions & 6 deletions templates/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { inspect } from "util";

export const withDefaults =
(template, expectedArgsDefaults, debug = false) =>
(receivedArgs) => {
Expand All @@ -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 })

0 comments on commit eeea6df

Please sign in to comment.