
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
logical-config
Advanced tools
Allows you, through a short-hand notation, to invoke functions directly from your configuration files at load-time and map configuration entries to variables, classes, or functions.
The logical-config JavaScript package allows you, through short-hand notation, to invoke functions directly from your configuration files at load-time and map configuration entries to variables, classes, or functions.
$ yarn add logical-config
See Winston Logical Config for a practical example of using a Logical Config file. Winston Logical Config allows you to configure a Winston logger directly from a config file.
config.yaml
Data:
Connection: "{database.getConnection}"
Code
const LogicalConfig = require('logical-config');
const yaml = require('js-yaml');
const fs = require('fs');
const config = await LogicalConfig.fill({
input: yaml.load(fs.readFileSync('./config.yaml', 'utf8')),
data: {
database: {
getConnection: async () => Promise.resolve({ connected: true })
}
}
});
console.log(config);
Output
{ Data: { Connection: { connected: true } } }
Path objects tell the LogicalConfig how to look up the desired value.
Each path object consists of three properties.
| Property | Required | Default | Description |
|---|---|---|---|
$fill | Yes | undefined | The dot path at which the desired value can be found in the map. |
parameters | No | [] | When the value found at the specified path is callable, and the call property is enabled, this is a list of parameters that will be passed to it. |
call | No | true | If the value found at the specified path is callable, this boolean indicates if the response of calling that value should be used, or the value itself. |
Example Path Object
{
'$fill': 'user.isOlderThan',
parameters: [ 18 ],
call: true
}
{}.;.$fill, parameters, call).parameters property should be a JSON encoded array.$fill property must be specified.The above example path object can be written in short-hand like this:
"{user.isOlderThan;[18]}"
You can attempt to parse a short-hand path object yourself using the .parsePathObject() function.
const parsed = LogicalConfig.parsePathObject(`{user.setName;["Nathan"];true}`);
console.log(parsed);
{ '$fill': 'user.setName', parameters: [ 'Nathan' ], call: true }
You can use Path Objects anywhere within the parameters array property of another Path Object. Please note however that nested Path Objects are not supported in short-hand path objects.
In this example we:
user.getAgeAsStr. The return value of this function is a string.funcs.toInt Number function.user.isOlderThan function and returning the response.Code
const canBuyAlcohol = await LogicalConfig.fill({
input: {
'$fill': 'item.canBuy',
parameters: [{
'$fill': 'Number',
parameters: ["{user.getAgeAsStr}"]
}]
},
data: {
Number,
item: {
name: 'alcohol',
canBuy: age => age > 21,
},
user: {
getAgeAsStr: () => "27",
}
}
});
.fill() functionconst config = await LogicalConfig.fill(...
The .fill() function takes an input object and data object containing data that Path Objects can access. Itt will replace each instance of a Path Object with the value it describes from the datab object. This will be performed on the input object recursively until all path objects have been resolved, at which point the finalized object will be returned.
Parameters
| Parameter | Required | Description |
|---|---|---|
input | Yes | The input object that will be parsed. Can be an array of Path Objects, a single Path Object, or an object in which any value (at any depth) is either an array of Path Objects or a Path Object. |
data | Yes | An object containing data to which path objects can correspond |
ignoredPaths | No | An array containing dot paths to keys in the input property that can be ignored when searching for Path Objects. |
Return
The new object.
Retrieve a property from the map using short-hand
const res = await LogicalConfig.fill({
input: '{user.age}',
data: {
user: {
age: 27
}
}
});
console.log(res); // Outputs: 27
Call a function from the map using short-hand
const res = await LogicalConfig.fill({
input: '{user.getName}',
data: {
user: {
getName: () => "Nathan"
}
}
});
console.log(res); // Outputs: "Nathan"
Call a function with parameters from the map using short-hand
const res = await LogicalConfig.fill({
input: `{user.info;[{"name":"Nathan"}, 27]}`,
data: {
user: {
info: ({name}, age) => ({name, age})
}
}
});
console.log(res); // Outputs: { name: 'Nathan', age: 27 }
Retrieve a function as a value from the map using short-hand
By default, if a property is callable (is a class or a function), it will be invoked and it's return value will be used. You can override this by setting the call property of the Path Object to false.
const res = await LogicalConfig.fill({
input: `{user.info;;false}`,
data: {
user: {
info: () => {}
}
}
});
console.log(res); // Outputs: [Function: info]
Retrieve a new instance of a class from the map using short-hand
const res = await LogicalConfig.fill({
input: '{person.c}',
data: {
person: {
c: class {
constructor() {
this.name = "Nathan";
}
}
}
}
});
console.log(res); // Outputs: c { name: 'Nathan' }
Retrieve a new instance of a class with parameters from the map using short-hand
const res = await LogicalConfig.fill({
input: '{person.c;["Nathan"]}',
data: {
person: {
c: class {
constructor(name) {
this.name = name;
}
}
}
}
});
console.log(res); // Outputs: c { name: 'Nathan' }
Retrieve a class as a value from the map using short-hand.
By default, if a property is callable (is a class or a function), it will be invoked and it's return value will be used. You can override this by setting the call property of the Path Object to false.
const res = await LogicalConfig.fill({
input:'{person.c;;false}',
data: {
person: {
c: class {}
}
}
});
console.log(res); // Outputs: [class c]
FAQs
Allows you, through a short-hand notation, to invoke functions directly from your configuration files at load-time and map configuration entries to variables, classes, or functions.
We found that logical-config demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.