tp-config
TypeScript icon, indicating that this package has built-in type declarations

1.0.5 • Public • Published

tp-config

Build Status Coverage Status npm version monthly downloads code style: prettier semantic-release UNPKG liense

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

NPM Badge

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 is null. Fallback value defaults to null.
  • @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 was null. Fallback value defaults to false.
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 be NaN. Fallback value defaults to NaN.
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 suite
  • npm start: Run npm run build in watch mode
  • npm run test:watch: Run test suite in interactive watch mode
  • npm run test:prod: Run linting and generate coverage
  • npm run build: Generate bundles and typings, create docs
  • npm run lint: Lints code
  • npm run commit: Commit using conventional commit style (husky will tell you to use it if you haven't 😉)

License

MIT

Package Sidebar

Install

npm i tp-config

Weekly Downloads

6

Version

1.0.5

License

MIT

Unpacked Size

1.7 MB

Total Files

61

Last publish

Collaborators

  • hsiang