Repository URL to install this package:
Version:
0.14.1 ▾
|
// import { isNumberOrString } from 'exotic'
import { isNumber, isString, isEmpty } from 'exotic'
const isNumberOrString = x => isNumber(x) || isString(x)
// @note - this old credit card match does global match
// and has no begin/end so it will trigger falsy
// /[a-zA-Z0-9]/g
const matchLetters = /([a-zA-Z])/
const matchNumbers = /([0-9])/
const matchAlphaNumeric = /^[A-Za-z0-9]+$/
const onlyAlphabets = /^[a-zA-Z]*$/
const matchCity = /^[A-Za-z -]*$/
const matchSpaces = /\W+/gm
const matchAlphaNumericSpecialCharacters = /^[ A-Za-z0-9_@./#*!&+-]*$/
export type AlphaNumericDataTypes = string | number
/**
* @alias isNumberOrStringOnly
* @name isAlphaNumeric
*
* @description has only numbers & letters
* @param {String | Number} value
* @return {Boolean} is valid
*/
function isAlphaNumeric(value: AlphaNumericDataType): boolean {
return isNumberOrString(value) && matchAlphaNumeric.test(value)
}
function isAddress(value: AlphaNumericDataType): boolean {
const withoutSpaces = value.replace(matchSpaces, '')
return isAlphaNumeric(withoutSpaces)
}
function isNumeric(value: AlphaNumericDataType): boolean {
return isNumber(value)
}
function isEmptyOrValidNumber(value: AlphaNumericDataType): boolean {
if (isEmpty(value)) {
return true
} else {
return isNumber(value)
}
}
function isAlphabet(value: AlphaNumericDataType): boolean {
return value && isString(value) && onlyAlphabets.test(value)
}
function isValidCity(value: AlphaNumericDataType): boolean {
return value && matchCity.test(value)
}
function isAlphaNumericSpecialCharacters(value: AlphaNumericDataType): boolean {
return value && matchAlphaNumericSpecialCharacters.test(value)
}
export { matchLetters }
export { matchNumbers }
export { isAlphaNumeric }
export { isAlphaNumeric as isNumberOrStringOnly }
export { isAddress }
export { isNumeric }
export { isValidCity }
export { isAlphaNumericSpecialCharacters }
export { isAlphabet }
export { isEmptyOrValidNumber }
export default isAlphaNumeric