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

continu

v1.3.2

Published

Zero dependency container/config library.

Downloads

613

Readme

Continu 🐯

npm version main codecov Known Vulnerabilities semantic-release: angular

Continu is a powerful tool for managing key-value pairs in an application. Its simple and lightweight design makes it easy to use. Besides, defining default values it is also possible to execute transformations & validations before setting any value.

Table of Contents

Installation

npm install continu --save

Usage

Basic

On the basic level, this library can be used as container for arbitrary key value pairs.

import { Continu } from 'continu';

type Options = {
    foo: string
}

const continu = new Continu<Options>();

console.log(continu.has('foo'));
// false
console.log(continu.get('foo'));
// undefined

continu.set('foo', 'bar');

console.log(continu.has('foo'));
// true
console.log(continu.get('foo'));
// bar

continu.reset('foo');

console.log(continu.has('foo'));
// false

Defaults

It is also possible to define default values which are returned if a given value is not set.

import { Continu } from 'continu';

type Options = {
    foo: string
}

const continu = new Continu<Options>({
    defaults: {
        foo: 'bar'
    }
});

console.log(continu.has('foo'));
// false
console.log(continu.get('foo'));
// 'bar'

continu.set('foo', 'baz');

console.log(continu.has('foo'));
// true
console.log(continu.get('foo'));
// 'baz'

Transformation

Transformers can be used to accept multiple input formats. The data can than be converted to the appropriate format before setting. Therefore, the setRaw method must be used.

import { Continu } from 'continu';

type Options = {
    foo: string
}

const continu = new Continu<Options>({
    transformers: {
        foo: (value) => {
            if(typeof value === 'number') {
                return `${value}`;
            }

            if(typeof value === 'string') {
                return value;
            }

            throw new Error('Option could not be transformed.')
        }
    }
});

continu.set('foo', '123');

console.log(continu.get('foo'));
// '123'

continu.set('foo', 456);

console.log(continu.get('foo'));
// '456'

continu.set('foo', {bar: 'baz'});
// this statement will throw an error!

Validation

Validators can be useful for defining constraints and prevent values from being set, if they don't match specific criteria.

import { Continu } from 'continu';

type Options = {
    foo: string
}

const continu = new Continu<Options>({
    validators: {
        foo: (value) => typeof value === 'string' && value.length > 3,
    }
});

continu.set('foo', 'bar');

console.log(continu.get('foo'));
// undefined

continu.set('foo', 'bar-baz');

console.log(continu.get('foo'));
// 'bar-baz'

Nesting

When using nested object types, it is also possible to use key paths (separated by .) to access properties in depth.

import { Continu } from 'continu';

type Options = {
    nested: {
        foo: string
    }
}

const continu = new Continu<Options>();

console.log(continu.has('nested.foo'));
// false
console.log(continu.get('nested.foo'));
// undefined

continu.set('nested.foo', 'bar');

console.log(continu.has('nested.foo'));
// true
console.log(continu.get('nested.foo'));
// 'bar'

console.log(continu.has('nested'));
// true;
console.log(continu.get('nested'));
// { foo: 'bar' }

Getters

It is also possible to define dynamic getters for specific key (paths).

import { Continu } from 'continu';

type Options = {
    foo: string,
    nested: {
        baz: string
    }
}

const continu = new Continu<Options>({
    defaults: {
        foo: 'bar'
    },
    getters: {
        nested: (context) => {
            return {
                baz: context.getDefault('foo')
            }
        }
    }
});

// always evaluates to false,
// since a key path may not be accessible until a getter has been evaluated.
console.log(continu.has('nested'));

console.log(continu.get('nested'));
// { foo: 'bar' }

console.log(continu.get('nested.baz'));
// 'bar'

Contributing

Before starting to work on a pull request, it is important to review the guidelines for contributing and the code of conduct. These guidelines will help to ensure that contributions are made effectively and are accepted.

License

Made with 💚

Published under MIT License.