New:Microsoft Teams Notifications Are Now Available in Socket.Learn more →
Get Started

continu

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

continu

Zero dependency container library.

Source
npmnpm
Version
1.0.2
Version published
Weekly downloads
17
21.43%
Maintainers
1
Weekly downloads
 
Created
Source

Continu 🌱

npm version main codecov Known Vulnerabilities semantic-release: angular

Continu is a zero dependency container library. Besides, defining default values it is also possible to define 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 data.

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' }

License

Made with 💚

Published under MIT License.

Keywords

container

FAQs

Package last updated on 17 Dec 2022

Related posts