@rypen-dev/helpers
v1.0.24
Published
Javascript utilities for Rypen projects.
Downloads
132
Readme
@rypen-dev/helpers
Javascript helpers for rypen projects.
Install
$ npm install @rypen-dev/helpers
Helper methods
Credit card
import { creditCard } from "@rypen-dev/helpers";
creditCard.formatCreditCardType('MasterCard'); // 'MasterCard'
creditCard.formatCreditCardType('AmericanExpress'); // 'AMEX'
creditCard.formatMaskedCreditCard('XXXX1234'); // '**** 1234'
Currency
import { currency } from "@rypen-dev/helpers";
currency(1234.56); // '$1,234.56'
Error processing
A wrapper around displaying errors from api requests that culls out the error message.
import { processError } from "@rypen-dev/helpers";
const serviceErrorResponse = {
data: {
ErrorMessage: 'The request was invalid.',
...
}
}
processError(serviceErrorResponse); // 'The request was invalid.'
Object manipulation
Mostly used to lookup enum keys by value, and to prettify the enum.
import { getKeyByValue, transformEnumForDisplay } from "@rypen-dev/helpers";
const testEnumObject = {
FIRST_VALUE: 0,
SECOND_VALUE: 1,
}
getKeyByValue(0); // 'FIRST_VALUE'
transformEnumForDisplay(0); // 'First Value'
Querystring reading
import { querystring } from "@rypen-dev/helpers";
// rypen.com/test?id=123
querystring('id'); // '123'
// rypen.com/test?id=123&value=something
querystring('value'); // 'something'
querystring('anotherthing'); // false
Scroll To
Animated scroll to position or element. Takes in an HTML element, selector string, or position number. Default duration is 500ms. Default offset is 0.
import { scroll } from "@rypen-dev/helpers";
// scroll([element, selector or position], [duration], [offset])
scroll(document.getElementById('#element-to-scroll-to'), 500); // scroll to element's position
scroll('#element-to-scroll-to', 1000, 200); // scroll to element's position plus 200px
scroll(1539, 2000); // scroll to exact position
Validation
Validation helpers.
Method | Description | Parameters
--- | --- | ---
empty | Check whether input is empty, can be used on string
, number
, array
, and object
types. Option to count 0
as non-empty. | value: [any]
, zeroIsNotEmpty: bool
(default false
)
validEmail | Basic validation for a valid email address. | email: string
validZip | Validation for US and Canadian postal codes. | zip: string
, canadian: bool
(default false
)
validPhone | Validation for US phone numbers. | phone: string
validCardNumber | Validation for credit card numbers, uses the Luhn Check | card: string
validCardCode | Validation for credit card code number. | code: string
validExpirationDate | Validation for credit card expiration date (valid date and not in past). | dateString: string
(format ####
, e.g. 0122
)
import { validation } from "@rypen-dev/helpers";
// empty
validation.empty(''); // true
validation.empty(0); // true
validation.empty(' '); // true
validation.empty([]); // true
validation.empty('foo'); // false
validation.empty(0, true); // false
// validEmail
validation.validEmail('gibberish'); // false
validation.validEmail('[email protected]'); //true
// validZip
validation.validZip('55512'); // true
validation.validZip('123'); // false
validation.validZip('C4N1X3'); // false
validation.validZip('C4N1X3', true); // true
// validPhone
validation.validPhone('6125551234'); // true
validation.validPhone('xxx123'); // false
// validCardNumber
validation.validCardNumber('bar'); // false
// validCardCode
validation.validCardCode('12'); // false
validation.validCardCode('123'); // true
validation.validCardCode('1234'); // true
// validExpirationDate
validation.validExpirationDate('0126'); // true
validation.validExpirationDate('0199'); // false
validation.validExpirationDate('foo'); // false