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

form-buffer

v1.0.3

Published

Help class for working with forms in web

Downloads

2

Readme

FormBuffer

Description

FormBuffer is a class for working with form values, you can format values on entry in buffer (inFormaters), on output(outFormaters), and on clear(defaultFormaters).

If you are tired of it, or if this is not enough for you

const data = (() => {
    name: '',
    phone: '',
    ...
})

Typescript

It fully compatible with typescript, for using you only need call FormBuffer.extend(...), not new FormBuffer(...)

Installation

Using npm:

npm install --save form-buffer
import FormBuffer from 'form-buffer';

Using cdn:

<script src="https://unpkg.com/form-buffer"></script>
<script>
    const FormBufferClass = FormBuffer.default;
    console.log(FormBuffer);
    console.log(FormBufferClass);
</script>

Example

const options = {
    preset: {
        test: {
            type: String,
        },
        test2: {
            default: 123,
        },
        test3: {
            default: () => 123,
        },
    },
};

const buffer = new FormBuffer(options);

Options

preset

Schema

{
    propName: {
        modif1: modif1Options,
        modif2: modif2Options,
    }
}

Description

Object which contain keys future props and used modificators

Required true

global

Schema

{
    modif1: modif1Options,
    modif2: modif2Options,
}

Description

Contain global modificators which spreading on all props(local modifs have more priority)

Required false

inFormaters

Schema

{
    inFormaters: {
        propName(oldValue, newValue, formBuffer, symbols) {

        }
    }
}

Description

Contain inFormaters for props

Arguments

oldValue - previously value

newValue - prop value which transferred in setValues method

formBuffer - object of this FormBuffer

symbols - object with delSymbol and skipSymbol

Required false

outFormaters

Schema

{
    outFormaters: {
        propName(value, formBuffer, symbols) {

        }
    }
}

Description

Contain outFormaters(called when call getFormData) for props

Arguments

value - last value of prop

formBuffer - object of this FormBuffer

symbols - object with delSymbol and skipSymbol

Required false

defaultFormaters

Schema

{
    outFormaters: {
        propName(oldValue, formBuffer, symbols) {

        }
    }
}

Description

Contain defaultFormaters(called when call clear) for props

Arguments

oldValue - last value of prop

formBuffer - object of this FormBuffer

symbols - object with delSymbol and skipSymbol

Required false

Modificators

Description

Modificators - are objects which group formaters, modificator can have 1 or all 3 formaters, and modificators can pass parameters

Default modificators

Autoclean modificator

Lightweight modificator which do only one thing, if value from formData not equal true(not strict euqal) delete prop with this value from result object

Example
const config = {
    preset: {
        test1: {
            type: String,
            autoclean: true,
        },
        test2: {
            type: String,
        },
    },
};

const buffer = new FormBuffer(config);
buffer.test2 = 'foo';

const result = buffer.getFormData();
// result = {
//    test2: 'foo',
//}

Deep modificator

Modificator for copying object/array structures on in and out, now don`t support symbols in props, only JSON stringify objects

Example
const config = {
    preset: {
        test1: {
            type: Object,
            deep: true,
        },
        test2: {
            type: Object,
        },
    },
};

const buffer = new FormBuffer(config);
const testObj = {
    test1: {},
    test2: {},
};

buffer.setValues(testObj);

// buffer.test2 === testObj.test2
// buffer.test1 !== testObj.test1

const result = buffer.getFormData();

// result.test2 === testObj.test2
// result.test1 !== testObj.test1

Default modificator

Modificator for default value(when called clear), return everything, that pass in parameter, but if parameter function, call it without some arguments

Example
const config = {
    preset: {
        test1: {
            default: () => 'foo',
        },
        test2: {
            default: 'bar',
        },
    },
};

const buffer = new FormBuffer(config);
// buffer = {
//    test1: 'foo',
//    test2: 'bar',
//    ...
//}

const result = buffer.getFormData();
// result = {
//    test1: 'foo',
//    test2: 'bar'
//}

Type modificator

Modificator for fast working with defaultModificator and using nested FormBuffer

You can custom this modificator by methods {add/has/remove/get}Type

Example
const configNested = {
    preset: {
        foo: {
            type: String,
        },
    },
};

const config = {
    preset: {
        test1: {
            type: String,
        },
        test2: {
            type: {
                type: FormBuffer,
                options: configNested,
            },
        },
    },
};

const buffer = new FormBuffer(config);

// buffer.test1 === ''
// buffer.test2.foo === ''