Skip to content

Commit

Permalink
feat: getStringifiedContent utils + updated rules
Browse files Browse the repository at this point in the history
  • Loading branch information
rin-st committed Oct 11, 2024
1 parent c0655f3 commit 71989f7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
6 changes: 4 additions & 2 deletions contributors/TEMPLATING.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,10 @@ Most of the time you will use string arguments for templating, but sometimes you
| --------------------------- | ---------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------- |
| 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 = ${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 | `const arrWithAdditionalItems = ${JSON.stringify(structuredClone(['a', 'b', ...arrayToSpread[0]]))};` | `const arrayToSpread = ["Spread", "This"]` | `const arrWithAdditionalItems = ["a", "b", "Spread", "This"];` |
| 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 } ` |
Expand Down
7 changes: 7 additions & 0 deletions templates/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,10 @@ 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");

0 comments on commit 71989f7

Please sign in to comment.