Coder Social home page Coder Social logo

Comments (4)

timreichen avatar timreichen commented on May 23, 2024 5

I think this would be a good addition.
But there are multiple ways to go about this and also some problems when arguing that parse() and stringify() should be exact opposite functions that reverse each step exactly.

Adding an option which takes a list of exported keys:

stringify({ FOO: "bar" }, { exports["FOO"] })

or adding an export property:

stringify({ FOO: { value: "bar", export: true } })

For an exact reversal function, the current parse() implementation is inadequately designed because it is lossy. It doesn't distinguish between exported and non-exported keys.

So the reversal would mean either

const { data, exports } = parse("export FOO=bar")
// data: { FOO: "bar" }
// exports: ["FOO"]

or

const data = parse("export FOO=bar")
// data: { FOO: { value: "bar", export: true }

Imo the second approach is better. That would also mean a breaking change in the dotenv API.

from deno_std.

bombillazo avatar bombillazo commented on May 23, 2024 4

Those are nice considerations. Perhaps another separate method can be added for this advanced parsing/stringifying?

I think the current API is very convenient, simple, and sweet for most cases. I'm not sure about the Deno convention for these types of enhanced methods, but forcing the more complex response as the default may make the API usage more verbose and complicated.

from deno_std.

jsejcksn avatar jsejcksn commented on May 23, 2024 1

Describe alternatives you've considered

Manually editing the resulting file and appending the string... :/

@bombillazo You don't have to manually edit the file. Unless a breaking change in the implementation happens, you can automate this by transforming the result before writing the file:

const obj = { FOO: "hello", BAR: "world" };

const dotEnvString = dotenv
  .stringify(obj)
  // Prefix each line with "export "
  .split("\n")
  .map((s) => `export ${s}`)
  .join("\n");

console.log(dotEnvString === "export FOO=hello\nexport BAR=world"); // true

// Write to file…

TS Playground

…or by transforming the input instead:

const dotEnvString = dotenv.stringify(
  Object.fromEntries(
    Object.entries(obj).map(([k, v]) => [`export ${k}`, v])
  ),
);

TS Playground

The previous approach, but using std/collections/map_keys.ts:

const dotEnvString = dotenv.stringify(
  mapKeys(obj, (k) => `export ${k}`),
);

TS Playground

from deno_std.

bombillazo avatar bombillazo commented on May 23, 2024

@jsejcksn Hey! Thats exactly how we are currently handling this. It works and one has complete control on which functions to export or not in the map function. :)

from deno_std.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.