npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@miaooo/pika

v0.0.6

Published

Easy-to-use config hierarchical configuration.

Downloads

12

Readme

@miaooo/pika

version downloads license dependencies coveralls

简体中文

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 toprocess.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 as test, dev, and local match, the databaseHost will be set to the corresponding data.
  • When process.env.NODE_ENV does not match any of the conditions of prod, test, dev, and local, the databaseHost 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 worddefault`

Precautions

  1. Pika is completely developed by Typescript, with completeness and code hints, and the types of all enumeration values of .switch must be consistent.
  2. The enumeration key of .switch can be filled in as many as you want, except for the field of default, which must be filled in. This is to prevent the conditions set during the initialization of new Pika from being unable to cover all conditions, resulting in code An accident occurred while running.
  3. 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.