ambiere / to-object

Parse string representations of objects into JavaScript objects.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

to-object ci

Lightweight utility package designed to simplify—
the process of parsing string representations of objects into JavaScript objects.

As implemented in next-clsx

contents
1. Installattion
2. Use case
3. License

Installattion

npm install @ambiere/to-object

Use case

In configuration files when import specifier string/path to the configuration module can only be constructed dynamically, hence static imports become impractical, require not supported in esmodule environment and dynamic imports, import() are not feasible due to their asynchronous nature.

const configs = {
  config1: "first",
  config2: "second",
  //other configs
}
import toObject from "@ambiere/to-object"

function configure() {
  // ...

  /*
  * In your code where
  * you need to import the configs
  * */

  const configs = fs.readFileSync("/dynamically/constructed/import/specifier", "utf8")
  const parsedConfigs = toObject(configs)

  console.log(parsedConfigs.config1) // "first"

  // ...
}

Note: The content of the configuration module/file to be read, should be a valid JavaScript object

License

MIT license

About

Parse string representations of objects into JavaScript objects.

License:MIT License


Languages

Language:JavaScript 100.0%