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...
- 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.
<script type="text/javascript" src="dist/parsa.min.js" charset="utf-8"></script>
<script>
console.log('parseDate: 20121125 = ', parsa.parseDate('20121125', 'YYYYMMDD'));
</script>
<script type="text/javascript" src="https://cdn.rawgit.com/mrvautin/parsa/dist/parsa.min.js" charset="utf-8"></script>
const parsa = require('parsa');
parsa.parseDate('20121125', 'YYYYMMDD');
npm test
gulp deploy
The parseDate
function takes a date string and format string parameters and returns a Javascript Date()
Object.
parsa.parseDate('20121125', 'YYYYMMDD')
Returns:
Sun Nov 25 2012 01:00:00 GMT+0100 (CET)
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
The validateIp
function takes an IP address string and returns a boolean
value whether it is valid or invalid.
parsa.validateIp('115.42.150.37')
Returns:
true
The validateIpv6
function takes an IP address string and returns a boolean
value whether it is valid or invalid.
parsa.validateIpv6('2001:db8:3:4::')
Returns:
true
The parseQuery
function takes a URL and returns an Object
of the Query string parameters.
parsa.parseQuery('http://example.com/product.php?category=4&product_id=2140&query=lcd+tv')
Returns:
{
"category": "4",
"product_id": "2140",
"query": "lcd+tv"
}
The parseUrl
function takes a URL and returns an Object
of the URL section.
parsa.parseQuery('https://www.google.com:80/dir/1/2/search.html?arg=0-a&arg1=1-b&arg3-c#hash')
Returns:
{
"url": "https://www.google.com:80/dir/1/2/search.html?arg=0-a&arg1=1-b&arg3-c#hash",
"protocol": "https",
"host": "www.google.com",
"port": ":80",
"path": "/dir/1/2/",
"file": "search.html",
"query": "?arg=0-a&arg1=1-b&arg3-c",
"hash": "#hash"
}
The validateUrl
function takes a URL and returns a boolean
result.
parsa.validateUrl('https://www.google.com')
Returns:
true
The validateEmail
function takes a email address string and returns a boolean
value whether it is valid or invalid.
parsa.validateEmail('hi@gmail.com')
Returns:
true
The extractNum
function takes a string and returns an array
of numbers/decimals found in that string.
parsa.extractNum('This is a10 string with3.14decimals6 and numbers.')
Returns:
[
'10',
'3.14',
'6'
]
The extractWords
function takes a string and an array
of words and returns an array
of matched words in the string.
var words = ['this', 'some', 'words'];
parsa.extractWords('thisadkfdlfkdisdsstringdfjdkwithdkfdfkldsomefdfdfkdflkwordsjfgjkfg', words)
Returns:
[
'this',
'some',
'words'
]
The extractPhone
function takes a string and returns an array
of matched phone numbers.
parsa.extractPhone('thisadkfdlfkdisdsstringdfjdkwithdkfdfkldsomefdfdfkdflkwordsjfgjkfg', words)
Returns:
[
'this',
'some',
'words'
]
The securePassword
function takes a password string returns a boolean
whether it's a secure password.
parsa.securePassword('Testing193!')
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
The removeAlpha
function takes a string and removes all non number characters.
parsa.removeAlpha('some1number')
Returns:
1
The removeNumeric
function takes a string and removes all numbers.
parsa.removeNumeric('some1number')
Returns:
somenumber
The firstUppercase
function takes a string and makes the first character of each word uppercase.
parsa.firstUppercase('this is a test string')
Returns:
This Is A Test String
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.
isAlpha
isNumeric
isString
minLength
maxLength
isBetweenLength
isObject
isArray
isRequired
securePassword
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.validateObject(schema, object)
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
}
The isAlpha
function takes a value and returns a boolean
whether it contains only alpha characters.
parsa.isAlpha('this is a test string')
Returns:
true
The isNumeric
function takes value and returns a boolean
whether it contains only alpha numbers.
parsa.isNumeric(1234)
Returns:
true
The isObject
function takes value and returns a boolean
whether it is a Object
.
parsa.isObject({"test": "Object"})
Returns:
true
The isArray
function takes value and returns a boolean
whether it is a Array
.
parsa.isArray(['abcd', '1234'])
Returns:
true
The isString
function takes value and returns a boolean
whether it is a String
.
parsa.isString('fkdlfkdl3233')
Returns:
true
The isDefined
function takes value and returns a boolean
whether the value is null or undefined.
parsa.isDefined('')
Returns:
false
The isFunction
function takes value and returns a boolean
whether it is a Function
.
parsa.isFunction(function test(){})
Returns:
true
The minLength
function takes value and a desired length and returns a boolean
whether it's is greater than supplied value.
parsa.minLength('23434fdfdfd', 5)
Returns:
true
The maxLength
function takes value and a desired length and returns a boolean
whether it's is less than supplied value.
parsa.maxLength('23434fdfdfd', 5)
Returns:
false
The isBetweenLength
function takes value, a min length and a max length and returns a boolean
whether the value is between the range.
parsa.maxLength('23434fdf', 5, 10)
Returns:
true