1.0.17 • Published 6 years ago

perfectform v1.0.17

Weekly downloads
18
License
MIT
Repository
github
Last release
6 years ago

Perfectform

Parse and format forms perfectly ✨

npm

Install

npm install perfectform --save

How to use

const perfectform = require('perfectform');

const input = {
  fullname: 'Jordan Belfort',
  username: 'jordan22',
  favoriteNumber: 8,
  hasFerrari: false
}

perfectform(input, {
  name: {
    type: 'string',
    from: 'fullname'
  },
  username: 'string', // You can just check the type
  favoriteNumber: {
    type: 'integer',
    gte: 0,
    lte: 9
  },
  hasFerrari: 'bool'
}, (err, form) => {
  if(err) return console.log(err);
  
  console.log(form);
  // Form is now 
  // {
  //   name: 'Jordan Belfort',
  //   username: 'jordan22',
  //   favoriteNumber: 8
  //   hasFerrari: false
  // }
})

Checkout examples folder for more.

Documentation

Supported types

string, integer, float, boolean, object, array

Default options

KeyValueDefault
typeType of value.Type of value
fromWhere to get key from form.Same key from form
requiredFires error if one of required keys missing.true
formatFormat value with custom function. Skips all other tests.-

Type specific options

String

Format

KeyTypeOption
trimbooleanTrim string.
toLowerCasebooleanConvert to lower case.
toUpperCasebooleanConvert to upper case.
toLocaleLowerCaselocale array/stringConvert to lower case with locale. More info
toLocaleUpperCaselocale array/stringConvert to upper case with locale. More info

Test

KeyTypeOption
minLengthnumberMinimum length of string. (<)
maxLengthnumberMaximum length of string. (>)
regexpRegExpTest input with regexp.
startsWithstringTest input starts with specified string.
endsWithstringTest input ends with specified string.
includesstringTest input includes specified string.
notIncludesstringTest input not includes specified string.
arrayIncludesarrayTest input includes any string from specified array.
arrayNotIncludesarrayTest input not includes any string from specified array.
eqstringEquals. (value ==)
noteqstringNot equals. (value !==)
testfunctionTest input with custom function. Return null for successful tests.

Number

Test

KeyTypeOption
eqnumberEquals. (value ==)
noteqnumberNot equals. (value !==)
ltnumberLess than. (value <)
ltenumberLess than equals. (value <=)
gtnumberGreater than. (value >)
gtenumberGreater than equals. (value >=)
  • Uses parseInt/parseFloat.

License

MIT

✨ – Try pollme!

1.0.17

6 years ago

1.0.16

6 years ago

1.0.15

6 years ago

1.0.14

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago