@ambiere/to-object
v0.3.0
Published
Parse string representations of objects into JavaScript objects.
Downloads
2
Maintainers
Readme
to-object
Lightweight utility package designed to simplify— the process of parsing string representations of objects into JavaScript objects.
As implemented in next-clsx
contents 1. Installattion2. Use case3. 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