parsa
parsa
is an all purpose module which can parse, validate, extract and more!
- Parse a date string (with known format) into a Javascript Date Object
- Validate an Object against a schema, including: required, min/max length, numerical, string, array and many more
- Validates IPv4 and IPv6 (true/false)
- Parses a URL query string into an Object
- Parses a URL into an Object with Host, Path, Hash, Protocol etc
- Validates whether a string URL is valid
- Validates Email address (true/false)
- Extracts numbers and decimals from string
- Extracts words from string
- Extracts phone numbers from string
- Removes Alpha characters from string
- Removes numeric characters from string
- Changes first character of each word to uppercase
- Checks for a secure password (8 Characters, uppercase, lowercase, number & special characters)
- Many many more...
Jump to section
- parseDate
- validateIp
- validateIpv6
- parseQuery
- parseUrl
- validateUrl
- validateEmail
- extractNum
- extractWords
- extractPhone
- securePassword
- removeAlpha
- removeNumeric
- firstUppercase
- validateObject
- isAlpha
- isNumeric
- isObject
- isArray
- isString
- isFunction
- minLength
- maxLength
- isBetweenLength
parsa
is only 8KB compare to Moment.js
which is ~51KB. This is handy if using in the browser.
Installation
Browser
CDN
Node
const parsa = ;parsa;
Tests
npm test
Build
gulp deploy
parseDate
The parseDate
function takes a date string and format string parameters and returns a Javascript Date()
Object.
Usage
parsa
Returns:
Sun Nov 25 2012 01:00:00 GMT+0100 (CET)
Supported formats
YYYYMMDD
YYYYDDMM
DDMMYYYY
MMDDYYYY
MMDDYY
DDMMYY
MM/DD/YYYY
DD/MM/YYYY
YYYY/DD/MM
DD-MM-YYYY
MM-DD-YYYY
YYYY-DD-MM
YYYY-MM-DD
DD MM YYYY
MM DD YYYY
YYYY MM DD
YYYYMMDD HH:MM
YYYYDDMM HH:MM
YYYYMMDD HH:MM:SS
YYYYDDMM HH:MM:SS
YYYY-DD-MM HH:MM
YYYY-MM-DD HH:MM
YYYY/MM/DD HH:MM
YYYY/DD/MM HH:MM
Do MMMM YYYY
Do, MMMM, YYYY
MM MMMM YYYY
validateIp
The validateIp
function takes an IP address string and returns a boolean
value whether it is valid or invalid.
Usage
parsa
Returns:
true
validateIpv6
The validateIpv6
function takes an IP address string and returns a boolean
value whether it is valid or invalid.
Usage
parsa
Returns:
true
parseQuery
The parseQuery
function takes a URL and returns an Object
of the Query string parameters.
Usage
parsa
Returns:
parseUrl
The parseUrl
function takes a URL and returns an Object
of the URL section.
Usage
parsa
Returns:
validateUrl
The validateUrl
function takes a URL and returns a boolean
result.
Usage
parsa
Returns:
true
validateEmail
The validateEmail
function takes a email address string and returns a boolean
value whether it is valid or invalid.
Usage
parsa
Returns:
true
extractNum
The extractNum
function takes a string and returns an array
of numbers/decimals found in that string.
Usage
parsa
Returns:
'10''3.14''6'
extractWords
The extractWords
function takes a string and an array
of words and returns an array
of matched words in the string.
Usage
var words = 'this' 'some' 'words';parsa
Returns:
'this''some''words'
extractPhone
The extractPhone
function takes a string and returns an array
of matched phone numbers.
Usage
parsa
Returns:
'this''some''words'
securePassword
The securePassword
function takes a password string returns a boolean
whether it's a secure password.
Usage
parsa
Password requirements are set to standard defaults:
- at least 8 characters
- must contain at least 1 uppercase letter, 1 lowercase letter, and 1 number
- Can contain special characters
Returns:
true
removeAlpha
The removeAlpha
function takes a string and removes all non number characters.
Usage
parsa
Returns:
1
removeNumeric
The removeNumeric
function takes a string and removes all numbers.
Usage
parsa
Returns:
somenumber
firstUppercase
The firstUppercase
function takes a string and makes the first character of each word uppercase.
Usage
parsa
Returns:
This Is A Test String
validateObject
The validateObject
function takes an Object and a Schema and returns a validation result with any errors.
Each schema validation requires a name
and a rules
array. The name
property refers to the key in the Object
being supplied.
Supported schema rules
isAlpha
isNumeric
isString
minLength
maxLength
isBetweenLength
isObject
isArray
isRequired
securePassword
Usage
let object ="test_number": 1234"test_string": 'abcdefg'"test_array": 1 2 3"test_required": ''"test_length": 'I am a long string';let schema ="name": "test_number""rules":'isNumeric'"name": "test_string""rules":'isString'"name": "test_array""rules":'isArray'"name": "test_required""rules":'isRequired'"name": "test_length""rules":'minLength|5''maxLength|25';
Note: when using a schema validation which requires multiple arguments other than the value (Eg:
minLength
,isBetweenLength
etc) you pass arguments using the|
character as a separator. For example:isBetweenLength
would look like:isBetweenLength|0|16
which would validate values between 0 and 16 characters in length.
parsa
Returns:
With errors
errors:property: 'test_string'message: 'Value is greater than the maximum length'property: 'test_required'message: 'Value is required'result: false
Without errors
errors:result: true
isAlpha
The isAlpha
function takes a value and returns a boolean
whether it contains only alpha characters.
Usage
parsa
Returns:
true
isNumeric
The isNumeric
function takes value and returns a boolean
whether it contains only alpha numbers.
Usage
parsa
Returns:
true
isObject
The isObject
function takes value and returns a boolean
whether it is a Object
.
Usage
parsa
Returns:
true
isArray
The isArray
function takes value and returns a boolean
whether it is a Array
.
Usage
parsa
Returns:
true
isString
The isString
function takes value and returns a boolean
whether it is a String
.
Usage
parsa
Returns:
true
isDefined
The isDefined
function takes value and returns a boolean
whether the value is null or undefined.
Usage
parsa
Returns:
false
isFunction
The isFunction
function takes value and returns a boolean
whether it is a Function
.
Usage
parsa
Returns:
true
minLength
The minLength
function takes value and a desired length and returns a boolean
whether it's is greater than supplied value.
Usage
parsa
Returns:
true
maxLength
The maxLength
function takes value and a desired length and returns a boolean
whether it's is less than supplied value.
Usage
parsamaxLength'23434fdfdfd' 5
Returns:
false
isBetweenLength
The isBetweenLength
function takes value, a min length and a max length and returns a boolean
whether the value is between the range.
Usage
parsamaxLength'23434fdf' 5 10
Returns:
true