npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dot_configurator

v1.0.8

Published

takes .env files and makes them into different values

Downloads

2

Readme

Dot Configurator

Dot configurator is a lightweight utility package for Node.js that simplifies the process of converting process.env variables into various JavaScript data types. Whether you're dealing with numbers, objects, arrays, or booleans, DotConfigurator can automatically transform your environment variables into the correct data types.

Features

  • 🚀 Effortlessly convert environment variables
  • 📦 Supports numbers, objects, arrays, and booleans
  • 🌈 Intuitive and easy-to-use API
  • ⚙️ Lightweight and dependency-free

Installation

npm install dotenv_configurator

How it works? It's quite simple

Booleans

The DotConfigurator class provides an intuitive mechanism to detect and convert environment variables to their respective JavaScript data types. In the case of boolean values, the class employs a comprehensive approach that covers various representations. Whether an environment variable's value is specified as

  • "true"
  • "false"
  • "on"
  • "off"
  • "yes"
  • "no"

Logic for Detecting Array, Object, and Number

In the DotConfigurator class, the logic is implemented to automatically detect and convert different types of environment variables. Here's how it handles arrays, objects, and numbers:

  • Array Detection: If a variable value is enclosed within square brackets ([...]), the DotConfigurator class attempts to parse it using JSON.parse() to convert it into an array. If the parsing is successful and the result is indeed an array, the variable is stored as an array. If the parsing fails or the result is not an array, the variable is treated as a parsing error and assigned the value 'PARSING_ERROR'.

  • Object Detection: For variables that contain curly braces ({...}), the DotConfigurator class applies JSON.parse() to convert the variable into an object. If the parsing is successful and the result is a valid object, the variable is stored as an object. If the parsing fails, the variable is treated as a parsing error and assigned the value 'PARSING_ERROR'.

  • Number Detection: Numeric variables are detected using the isNaN() function. If the variable can be successfully converted to a number using Number(), it is stored as a number. Otherwise, it's left unchanged.

This logic ensures that environment variables are correctly converted to their intended data types, and any parsing errors are properly handled. The DotConfigurator class simplifies the process of working with various data types in the process.env variables while providing ease of use and reliability.

the DotConfigurator class intelligently detects these variations and accurately transforms them into the corresponding boolean value. This smart handling ensures that your boolean variables are consistently interpreted, regardless of the specific string representation used in your environment. This capability simplifies the process of managing and utilizing boolean configuration settings within your application.

Usage

  SERVER_PORT: '8080',
  API_URL: 'https://api.example.com',
  ENABLE_FEATURE: 'true',
  DATABASE_CONFIG: '{"host":"localhost","port":3306}'
const DotConfigurator = require('dotenv_configurator');

const dot = new DotConfigurator(process.env);

const serverPort = dot.GET('SERVER_PORT'); // Returns a number: 8080

const apiUrl = dot.GET('API_URL'); // Returns a string: "https://api.example.com"

const enableFeature = dot.GET('ENABLE_FEATURE'); // Returns a boolean: true

const dbConfig = dot.GET('DATABASE_CONFIG'); // Returns an object: { host: localhost, port:3306 }

Error Handling

If there's an error during conversion, the variable will not be cached, and "PARSING_ERROR" will be assigned instead. Be sure to check for this value when retrieving a converted variable.

In case of error's the dotConfigurator will only alert but wont break the app in case you want to still use it you can always access it with just process.env...

License

This project is licensed under the MIT License.

Happy coding! 👨‍💻👩‍💻