Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

dotenv-utils

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dotenv-utils - npm Package Compare versions

Comparing version 0.1.1 to 0.1.2

2

index.js
const boolean = (s) => s
? s.trim() === "true"
? s.toLowerCase().trim() === "true"
: false

@@ -4,0 +4,0 @@

@@ -6,2 +6,3 @@ const test = require('ava')

t.true(boolean("true"))
t.true(boolean("TRUE"))
t.true(boolean(" true "))

@@ -8,0 +9,0 @@ t.false(boolean("false"))

{
"name": "dotenv-utils",
"version": "0.1.1",
"version": "0.1.2",
"description": "Covert env var strings to other types",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -5,14 +5,17 @@ # dotenv-utils

> Covert env var strings to other types
> Covert env var strings to booleans, numbers, arrays, and objects.
Alright, so you're using [`dotenv`](https://www.npmjs.com/package/dotenv). Everything is smooth sailing until you need to convert some of the strings it outputs into a different type.
**Context:** Environment variables are a great way to separate config from code. Tools like [`dotenv`](https://www.npmjs.com/package/dotenv) make this almost perfect but...
It probably starts with something like `process.env.MINIFY === "true"` but as your app grows, other "types" might crop up. Ad-hoc solutions slowly become hard to read and maintain. If you have many apps, you might end up with many — subtly different — ways in which you convert vars into something that can be safely consumed.
**The problem:** Environment variables are always strings. If you set a variable `MINIFY=false` and try to check it using `if (process.env.MINIFY) { ...some logic }`, then "...some logic" will actually be executed as non-empty strings are truthy.
This is where `dotenv-utils` comes into play. :tada:
The quick solution is to write `process.env.MINIFY === "true"` instead, but as you get more vars and more var "types", these ad-hoc solutions become tedious, unclear, and error-prone.
You can use individual conversion functions (`boolean`, `number`, `array`, `object`) to convert vars one at a time, or create a new, squeaky clean config object based on a schema using the `conform` helper.
**The solution**: `dotenv-utils` provides several helpers to make sure you never have to worry about this again.
The output of `dotenv-utils` conversion functions is always of the specified type. That way, you can safely call whichever methods that type has without worrying about getting that `Uncaught Type Error: undefined is not a function` fun.
- [`boolean`](#boolean), [`number`](#number), [`array`](#array), [`object`](#object) convert vars one at a time.
- [`conform`](#conform) creates a new config object with the correct types based on a schema.
Conversion functions always return the right type. That way, you can safely call methods without worrying about getting that `Uncaught Type Error: undefined is not a function` fun.
## Install

@@ -28,2 +31,4 @@

Tested on Node.js v6.9.2, likely runs on earlier versions too.
## API

@@ -33,3 +38,3 @@

Converts a string representation of a boolean to an actual boolean. Note that only `"true"` will evaluate to `true` — everything else is `false`.
Converts a string representation (case-insensitive) of a boolean to an actual boolean.

@@ -40,2 +45,3 @@ ```js

boolean("true") // true
boolean("TRUE") // true
boolean("false") // false

@@ -62,3 +68,3 @@ boolean("foo") // false

Trims the supplied string. If provided a falsy value, returns `""`. This is mainly useful when used in conjunction with the `conform` helper.
Trims the supplied string. If provided a falsy value, returns `""`. This is mainly useful when used in conjunction with the [`conform`](#conform) helper.

@@ -96,3 +102,3 @@ ```js

object("foo: bar, baz: quux") // {foo: "bar", baz: "quux"}
object("foo: bar , baz:quux") // {foo: "bar", baz: "quux"}
object("foo: bar ,baz:quux") // {foo: "bar", baz: "quux"}
object(":,foo:") // {foo: ""}

@@ -106,7 +112,7 @@ object("::,") // {}

Provided a schema, `conform` picks keys from the supplied env object and converts them using the supplied functions.
Provided a schema, `conform` picks keys from an env object and converts them using the supplied functions.
Keys which are present in the `schema`, but not in you `process.env` will be present in the final object, having a value based on calling the conversion function with `undefined`.
Keys which are present in the `schema`, but not in the supplied `env` object *will* be present in the final object, having a value/type based on calling the conversion function with `undefined`.
Given this `.env`:
Given these env vars:
```

@@ -117,6 +123,6 @@ DEFAULT_LOCALE=en-GB

You can do this in your `config.js`:
You can do this:
```js
// Load your env vars
require('dotenv').config();
// Make sure you have loaded the env vars somehow,
// either inline or using `dotenv`...

@@ -123,0 +129,0 @@ const {conform, boolean, array, string} = require("dotenv-utils")

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc