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

Add .asStringRecord #174

Open
hongkongkiwi opened this issue Nov 1, 2024 · 0 comments
Open

Add .asStringRecord #174

hongkongkiwi opened this issue Nov 1, 2024 · 0 comments

Comments

@hongkongkiwi
Copy link

hongkongkiwi commented Nov 1, 2024

It would be great to have a feature which produces a Record<string, string> from a set of strings.

For example if I have an environment variable like this:
"a=1,b=2,c=3" or string quoted ""a"="1","b"="2""

Right now, I can do it with this code:

  const envMetadata = Array.from(env.get("METADATA").default([]).asSet()).reduce((acc: Record<string, string>, metadata: string) => {
    const [key, value = ""] = metadata.split("=");
    acc[key] = value;
    return acc;
  }, {});

Or to handle the string quoted case:

  const envMetadata = Array.from(env.get("METADATA").default([]).asSet()).reduce((acc: Record<string, string>, metadata: string) => {
    const [key, value = ""] = metadata.split("=");
    acc[key.replace(/^["']|["']$/g, "")] = value.replace(/^["']|["']$/g, "");
    return acc;
  }, {});

It would be super awesome if I could just have a asStringRecord(",", "="), then the code would be much cleaner.

So the proposal is to add:

asStringRecord(recordDelimiter: string = ",", itemSeperator: string = "=")

This is quite neat as you could also do something like asIntRecord etc. And it's quite flexible as you can set the delimiter to say newline for records.

@hongkongkiwi hongkongkiwi changed the title asStringRecord(recordDelimiter: string = ",", itemSeperator: string = "=") Add .asStringRecord Nov 1, 2024
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

No branches or pull requests

1 participant