https://github.com/tada5hi/continu
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.
https://github.com/tada5hi/continu
container key-path key-value nested-objects object option transformation transformer validation validator
Last synced: 3 months ago
JSON representation
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.
- Host: GitHub
- URL: https://github.com/tada5hi/continu
- Owner: tada5hi
- License: mit
- Created: 2022-12-16T17:38:36.000Z (almost 3 years ago)
- Default Branch: develop
- Last Pushed: 2024-03-28T17:16:17.000Z (over 1 year ago)
- Last Synced: 2025-06-14T05:17:57.947Z (4 months ago)
- Topics: container, key-path, key-value, nested-objects, object, option, transformation, transformer, validation, validator
- Language: TypeScript
- Homepage:
- Size: 1.21 MB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 8
-
Metadata Files:
- Readme: README.MD
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Continu 🐯
[](https://badge.fury.io/js/continu)
[](https://github.com/tada5hi/continu/actions/workflows/main.yml)
[](https://codecov.io/gh/tada5hi/continu)
[](https://snyk.io/test/github/Tada5hi/continu?targetFile=package.json)
[](https://github.com/semantic-release/semantic-release)**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](#installation)
- [Usage](#usage)
- [Basic](#basic)
- [Defaults](#defaults)
- [Transformation](#transformation)
- [Validation](#validation)
- [Nesting](#nesting)
- [Getters](#getters)
- [Contributing](#contributing)
- [License](#license)## Installation
```bash
npm install continu --save
```## Usage
### Basic
On the basic level, this library can be used as container for arbitrary key value pairs.
```typescript
import { Continu } from 'continu';type Options = {
foo: string
}const continu = new Continu();
console.log(continu.has('foo'));
// false
console.log(continu.get('foo'));
// undefinedcontinu.set('foo', 'bar');
console.log(continu.has('foo'));
// true
console.log(continu.get('foo'));
// barcontinu.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.```typescript
import { Continu } from 'continu';type Options = {
foo: string
}const continu = new Continu({
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.```typescript
import { Continu } from 'continu';type Options = {
foo: string
}const continu = new Continu({
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.```typescript
import { Continu } from 'continu';type Options = {
foo: string
}const continu = new Continu({
validators: {
foo: (value) => typeof value === 'string' && value.length > 3,
}
});continu.set('foo', 'bar');
console.log(continu.get('foo'));
// undefinedcontinu.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.```typescript
import { Continu } from 'continu';type Options = {
nested: {
foo: string
}
}const continu = new Continu();
console.log(continu.has('nested.foo'));
// false
console.log(continu.get('nested.foo'));
// undefinedcontinu.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).
```typescript
import { Continu } from 'continu';type Options = {
foo: string,
nested: {
baz: string
}
}const continu = new Continu({
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](./CONTRIBUTING.md) and the [code of conduct](./CODE_OF_CONDUCT.md).
These guidelines will help to ensure that contributions are made effectively and are accepted.## License
Made with 💚
Published under [MIT License](./LICENSE).