1.1.0 • Published 5 months ago

dycraft v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

dycraft 🌈

npm version npm version Master Workflow Known Vulnerabilities Conventional Commits

This is a versatile package for dynamically shaping objects. It seamlessly integrates default values and getters, providing a flexible way to tailor objects based on specific requirements.

Table of Contents

Installation

npm install dycraft --save

Usage

Defaults

import { dycraft } from 'dycraft';

type Options = {
    foo: string,
    bar: string
};

type OptionsInput = Partial<Options>;

const data : OptionsInput = {
    foo: 'bar'
};

const record = dycraft({
    data,
    defaults: {
        foo: 'baz',
        bar: 'boz'
    }
});

console.log(record.foo);
// bar

console.log(record.bar);
// boz

delete record.foo;

console.log(record.foo);
//baz

Getters

import { dycraft } from 'dycraft';

type Options = {
    foo: string,
    bar: string
};

type OptionsInput = Partial<Options>;

const record = dycraft({
    data: {
        foo: 'bar',
    } as OptionsInput,
    getters: {
        bar: defineGetter((context) : string => {
            if (context.has('foo')) {
                return context.get('foo');
            }

            return 'baz';
        }),
    },
});

console.log(record.foo);
// bar
console.log(record.bar);
// bar

delete record.foo;

console.log(record.foo);
// undefined
console.log(record.bar);
// baz

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.