tp-config
v1.0.5
Published
Used to get the application configuration
Downloads
24
Maintainers
Readme
tp-config
Intro
The Config lets you configure your entire app.
Example
import Config from 'tp-config';
const config = new Config({
testKey: testValue,
});
config.get('testKey') === 'testValue'; // true
Install
API
config.get(key, fallbackValue)
Returns a single config value, given a key.
@param {string} [key]
- the key for the config value@param {any} [fallbackValue]
- a fallback value to use when the config value was not found, or is config value isnull
. Fallback value defaults tonull
.@return {any}
let config = new Config({
testNum: 2,
testFn: function (this: Config) {
return `testFnReturnValue+${this.get("testNum")}`;
}
});
expect(config.get("testNum")).toEqual(2);
expect(config.get("testFn")).toEqual("testFnReturnValue+2");
config.getBoolean(key, fallbackValue)
Same as get()
, however always returns a boolean value. If the value from get()
is null
, then it'll return the fallbackValue
which defaults to false
. Otherwise, getBoolean()
will return if the config value is truthy or not. It also returns true
if the config value was the string value "true"
.
@param {string} [key]
- the key for the config value@param {boolean} [fallbackValue]
- a fallback value to use when the config value wasnull
. Fallback value defaults tofalse
.
let config = new Config({
key1: true,
key2: false
});
expect(config.getBoolean("key1")).toEqual(true);
expect(config.getBoolean("key2")).toEqual(false);
config.getNumber(key, fallbackValue)
Same as get()
, however always returns a number value. Uses parseFloat()
on the value received from get()
. If the result from the parse is NaN
, then it will return the value passed to fallbackValue
. If no fallback value was provided then it'll default to returning NaN
when the result is not a valid number.
@param {string} [key]
- the key for the config value@param {number} [fallbackValue]
- a fallback value to use when the config value turned out to beNaN
. Fallback value defaults toNaN
.
let config = new Config({
key: 6
});
expect(config.getNumber("key")).toEqual(6);
config.set(key, value)
Sets a single config value.
@param {string} [key]
- The key used to look up the value at a later point in time.@param {string} [value]
- The config value being stored.
let config = new Config(null);
config.set("name", "Doc Brown");
config.set("occupation", "Weather Man");
expect(config.get("name")).toEqual("Doc Brown");
expect(config.get("occupation")).toEqual("Weather Man");
config.settings()
Get all configs.
@return {object}
- Return all configs.
let config = new Config({
name: "Doc Brown",
occupation: "Weather Man"
});
expect(config.settings()).toEqual({
name: "Doc Brown",
occupation: "Weather Man"
});
config.settings(configs)
Set(reset) all configs.
@param {object} [configs]
- The configs object will be reset@return {object} this
- Return this value
let config = new Config();
config.settings({
name: "Doc Brown",
occupation: "Weather Man"
});
expect(config.get("name")).toEqual("Doc Brown");
expect(config.get("occupation")).toEqual("Weather Man");
Development
npm t
: Run test suitenpm start
: Runnpm run build
in watch modenpm run test:watch
: Run test suite in interactive watch modenpm run test:prod
: Run linting and generate coveragenpm run build
: Generate bundles and typings, create docsnpm run lint
: Lints codenpm run commit
: Commit using conventional commit style (husky will tell you to use it if you haven't :wink:)
License
MIT