1.0.24 • Published 5 months ago

@rypen-dev/helpers v1.0.24

Weekly downloads
3
License
-
Repository
-
Last release
5 months ago

@rypen-dev/helpers

npm (scoped) npm bundle size (minified)

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('test@rypen.com'); //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
1.0.22

5 months ago

1.0.21

5 months ago

1.0.20

8 months ago

1.0.24

5 months ago

1.0.23

5 months ago

1.0.19

1 year ago

1.0.18

1 year ago

1.0.17

1 year ago

1.0.16

3 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago