Package Exports
- @miaooo/pika
- @miaooo/pika/es/index.js
- @miaooo/pika/lib/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@miaooo/pika) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@miaooo/pika
Easy-to-use config hierarchical configuration.
Usage
import Pika from '@miaooo/pika'
const env = new Pika(process.env.NODE_ENV, {
prod: value => value === 'prod' || value === 'production',
test: value => value === 'test',
dev: value => value === 'dev' || value === 'development',
local: value => value === 'local',
})
const databaseHost = env.switch({
priority: process.env.DB_HOST,
prod: 'prod.mysql.com',
test: 'test.mysql.com',
dev: 'dev.mysql.com',
local: 'localhost.com',
default: 'default.mysql.com',
})
The meaning of the above code is:
- When
process.env.DB_HOST
is not ʻundefined, regardless of the value of
process.env.NODE_ENV,
databaseHostwill be set to
process.env.DB_HOST`. - When
process.env.NODE_ENV ==='prod' || process.env.NODE_ENV ==='production
,databaseHost
will be set to'prod.mysql.com'
. In the same way, when the conditions such astest
,dev
, andlocal
match, thedatabaseHost
will be set to the corresponding data. - When
process.env.NODE_ENV
does not match any of the conditions ofprod
,test
,dev
, andlocal
, thedatabaseHost
will be set to'default.mysql.com'
'.
Through the above code interpretation, it should be easy for everyone to understand the purpose of the Pika
library: reduce repetitive writing of ʻif` and improve the readability of configuration files.
In addition, Pika also provides some variables for use in the development:
if (env.is.prod) console.log('In production environment')
if (env.is.test) console.log('In test environment')
if (env.is.dev) console.log('In development environment')
if (env.is.local) console.log('In local environment')
if (env.not.prod) console.log('Not in production environment')
if (env.not.test) console.log('Not in test environment')
if (env.not.dev) console.log('Not in develoption environment')
if (env.not.local) console.log('Not in local environment')
Pika
can encapsulate the decision logic of process.env.NODE_ENV
, provide a unified API interface for calls, make the code easy to read and avoid writing unsound ʻif` in multi-person development.
In addition, the names of prod
, test
, dev
, and local
can be changed arbitrarily, and any number of key
can also be provided. As long as the judgment conditions declared in new Pika
can be used, there are no restrictions on Pika
:
const env = new Pika(process.env.NODE_ENV, {
customA: value => value === 'A',
customB: value => value === 'B',
})
const x = env.switch({
customA: 'abc',
default: 'def',
})
if (env.is.customA) console.log('abc')
if (env.is.not.customB) console.log('not def')
The first parameter of env.switch
is an enumeration object, priority
is a built-in key
, with the highest priority, as long as the data set by priority
is not ʻundefined, then
prioritytakes precedence . Otherwise, various conditions will be judged and the final decision will be made on which value to use. If none of the conditions are matched, the word
default`
Precautions
Pika
is completely developed byTypescript
, with completeness and code hints, and the types of all enumeration values of.switch
must be consistent.- The enumeration
key
of.switch
can be filled in as many as you want, except for the field ofdefault
, which must be filled in. This is to prevent the conditions set during the initialization ofnew Pika
from being unable to cover all conditions, resulting in code An accident occurred while running. - When
new Pick
is initialized, all the set judgment rules must be mutually exclusive, otherwise.swtich
cannot guarantee which value will be returned when multiple conditions are matched at the same time.
Contributing & Development
If there is any doubt, it is very welcome to discuss the issue together. Please read Contributor Covenant Code of Conduct and CONTRIBUTING.