Comments (4)
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.
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.
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…
…or by transforming the input instead:
const dotEnvString = dotenv.stringify(
Object.fromEntries(
Object.entries(obj).map(([k, v]) => [`export ${k}`, v])
),
);
The previous approach, but using std/collections/map_keys.ts
:
const dotEnvString = dotenv.stringify(
mapKeys(obj, (k) => `export ${k}`),
);
from deno_std.
@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)
- todo: add missing tests and documentation to `std/semver`
- todo(semver): add `range_max_test.ts` file HOT 2
- todo(semver): add `parse_range_test.ts` file HOT 2
- todo(semver): add `_shared_test.ts` file HOT 4
- todo(semver): add `_comparator_*_test.ts` files HOT 1
- todo(semver): add `test_range_test.ts` file HOT 2
- todo(fs): make `std/fs` Node-compatible HOT 2
- suggestion: enable `camelcase` linting rule
- suggestion: restructure `std/console`, `std/cli`, `std/fmt` and `std/text` HOT 22
- JSR package for depending on all of std? HOT 8
- Needs a way to distinguish breaking changes of stable modules and unstable modules in the release note HOT 7
- Deprecation of readerFromStreamReader seems to have no direct replacement from Readable to Reader HOT 7
- jsr Support for std/http
- bug: `parseRange()` returns unexpected result HOT 1
- todo: fix tests after workspaces migration script is run HOT 2
- snapshot testing fails with Deno v1.40.5 when stdout is terminal HOT 2
- Fix test failures after running `_tools/convert_to_workspace.ts` HOT 2
- [Streams] Add CSV separator setting on CsvParseStreamOptions documentation HOT 5
- bug(log): `RotatingFileHandler.log()` should handle message longer than 4096 HOT 1
- link replacement of fs.ensureSymlink() HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from deno_std.