Skip to content
/ parsa Public

Parsa ya data! An all purpose module to parse, sanitize, extract and validate data.

License

Notifications You must be signed in to change notification settings

mrvautin/parsa

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

parsa is only 8KB compare to Moment.js which is ~51KB. This is handy if using in the browser.

Installation

Browser

<script type="text/javascript" src="dist/parsa.min.js" charset="utf-8"></script>
<script>
    console.log('parseDate: 20121125 = ', parsa.parseDate('20121125', 'YYYYMMDD'));
</script>
CDN
<script type="text/javascript" src="https://cdn.rawgit.com/mrvautin/parsa/dist/parsa.min.js" charset="utf-8"></script>

Node

const parsa = require('parsa');
parsa.parseDate('20121125', 'YYYYMMDD');

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.parseDate('20121125', 'YYYYMMDD')

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.validateIp('115.42.150.37')

Returns:

true

validateIpv6

The validateIpv6 function takes an IP address string and returns a boolean value whether it is valid or invalid.

Usage

parsa.validateIpv6('2001:db8:3:4::')

Returns:

true

parseQuery

The parseQuery function takes a URL and returns an Object of the Query string parameters.

Usage

parsa.parseQuery('http://example.com/product.php?category=4&product_id=2140&query=lcd+tv')

Returns:

{
    "category": "4",
    "product_id": "2140",
    "query": "lcd+tv"
}

parseUrl

The parseUrl function takes a URL and returns an Object of the URL section.

Usage

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"
}

validateUrl

The validateUrl function takes a URL and returns a boolean result.

Usage

parsa.validateUrl('https://www.google.com')

Returns: true

validateEmail

The validateEmail function takes a email address string and returns a boolean value whether it is valid or invalid.

Usage

parsa.validateEmail('hi@gmail.com')

Returns:

true

extractNum

The extractNum function takes a string and returns an array of numbers/decimals found in that string.

Usage

parsa.extractNum('This is a10 string with3.14decimals6 and numbers.')

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.extractWords('thisadkfdlfkdisdsstringdfjdkwithdkfdfkldsomefdfdfkdflkwordsjfgjkfg', words)

Returns:

[
    'this',
    'some',
    'words'
]

extractPhone

The extractPhone function takes a string and returns an array of matched phone numbers.

Usage

parsa.extractPhone('thisadkfdlfkdisdsstringdfjdkwithdkfdfkldsomefdfdfkdflkwordsjfgjkfg', words)

Returns:

[
    'this',
    'some',
    'words'
]

securePassword

The securePassword function takes a password string returns a boolean whether it's a secure password.

Usage

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

removeAlpha

The removeAlpha function takes a string and removes all non number characters.

Usage

parsa.removeAlpha('some1number')

Returns:

1

removeNumeric

The removeNumeric function takes a string and removes all numbers.

Usage

parsa.removeNumeric('some1number')

Returns:

somenumber

firstUppercase

The firstUppercase function takes a string and makes the first character of each word uppercase.

Usage

parsa.firstUppercase('this is a test string')

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.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
}

isAlpha

The isAlpha function takes a value and returns a boolean whether it contains only alpha characters.

Usage

parsa.isAlpha('this is a test string')

Returns:

true

isNumeric

The isNumeric function takes value and returns a boolean whether it contains only alpha numbers.

Usage

parsa.isNumeric(1234)

Returns:

true

isObject

The isObject function takes value and returns a boolean whether it is a Object.

Usage

parsa.isObject({"test": "Object"})

Returns:

true

isArray

The isArray function takes value and returns a boolean whether it is a Array.

Usage

parsa.isArray(['abcd', '1234'])

Returns:

true

isString

The isString function takes value and returns a boolean whether it is a String.

Usage

parsa.isString('fkdlfkdl3233')

Returns:

true

isDefined

The isDefined function takes value and returns a boolean whether the value is null or undefined.

Usage

parsa.isDefined('')

Returns:

false

isFunction

The isFunction function takes value and returns a boolean whether it is a Function.

Usage

parsa.isFunction(function test(){})

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.minLength('23434fdfdfd', 5)

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

parsa.maxLength('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

parsa.maxLength('23434fdf', 5, 10)

Returns:

true