Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object merge with special characters #175

Open
wants to merge 1 commit into
base: tinker-object-merge-template
Choose a base branch
from

Conversation

rin-st
Copy link
Member

@rin-st rin-st commented Dec 19, 2024

No description provided.

const str = inspect(val, { depth: null, compact: true, maxArrayLength: null, maxStringLength: null });
return str
.replace(/"\$\$\$([^"]+)"/g, '$1')
.replace(/'\$\$\$([^']+)'/g, '$1')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these two lines replaces $$$var with var, so if we meet {a: "$$$var"} or {a: '$$$var'} in code it results {a: var}. Also works for {a: '$$$process.env.B = "true"'} => {a: process.env.B = "true"}

return str
.replace(/"\$\$\$([^"]+)"/g, '$1')
.replace(/'\$\$\$([^']+)'/g, '$1')
.replace(/(['"])(.*?\$\{.*?\}.*?)\1/g, '`$2`');
Copy link
Member Author

@rin-st rin-st Dec 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we meet in resulting code "something ${a}" it changes " to backtick (same for '), so it will be

`something ${a}`

Copy link
Collaborator

@technophile-04 technophile-04 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this! and +1 for this solution 🙌 Thanks @rin-st!

Very small down side of this is extension developers creating extension needs to follow our custom instruction for using variables:

  1. While accessing vars from code snipped they need to use $$$
  2. If they want to use string interpolation then they need to use escape that var in string \$

I think it's probably fine because I feel complex templating engine have it too like handlebars have {{var}} kind of thing? Also I think most of the file won't require accessing variables from code snippet too, and if some requires then they can use this format. We can document this nicely 🙌. Also with object-merge util their wouldn't be lot of key values where they need to use this format.

Let's see what others have to say

To test:

  1. switch to this branch object-merge-special-characters

  2. run this:

yarn build && yarn cli ../test-blah -s hardhat -e technophile-04/hardhat-config:v2 --skip

@damianmarti
Copy link
Member

@rin-st this is great and working great too!! This is a simple solution!

Maybe instead of using $$$var for the variables, it is better to use something like $$var$$

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants