Repository URL to install this package:
|
Version:
3.0.12 ▾
|
import { isNumber, isString, isEmpty } from 'exotic'
const isNumberOrString = (x: any): x is string | number =>
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 = /\s/g
const matchAlphaNumericSpecialCharacters = /^[ A-Za-z0-9_@./#*!&+-]*$/
const matchAlphaNumericMinusCharacter = /^[ A-Za-z0-9-]*$/
const matchAlphaNumericPlusMinusCharacter = /^[ A-Za-z0-9+-]*$/
export type AlphaNumericDataType = 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 (
isNumber(value) ||
(isNumberOrString(value) && matchAlphaNumeric.test(value))
)
}
function isNonEmptyString(value: AlphaNumericDataType): value is string {
return value !== '' && isString(value)
}
function isEmptyOrAlphaNumeric(value: AlphaNumericDataType): boolean {
if (isEmpty(value)) {
return true
} else {
return isAlphaNumeric(value)
}
}
function isAlphaNumericWithSpace(value: AlphaNumericDataType): boolean {
const withoutSpaces = String(value).replace(matchSpaces, '')
return value !== '' && isAlphaNumeric(withoutSpaces)
}
function isEmptyOrAlphaNumericWithSpace(value: AlphaNumericDataType): boolean {
if (isEmpty(value)) {
return true
} else {
const withoutSpaces = String(value).replace(matchSpaces, '')
return isAlphaNumeric(withoutSpaces)
}
}
function isAlphaNumericWithPlusMinus(value: AlphaNumericDataType): boolean {
return (
isNonEmptyString(value) && matchAlphaNumericPlusMinusCharacter.test(value)
)
}
function isEmptyOrAlphaNumericWithMinus(value: AlphaNumericDataType): boolean {
if (isEmpty(value)) {
return true
} else {
return matchAlphaNumericMinusCharacter.test(value as string)
}
}
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 isNonEmptyString(value) && onlyAlphabets.test(value)
}
function isValidCity(value: AlphaNumericDataType): boolean {
return isNonEmptyString(value) && matchCity.test(value)
}
function isAlphaNumericSpecialCharacters(value: AlphaNumericDataType): boolean {
return (
isNonEmptyString(value) && matchAlphaNumericSpecialCharacters.test(value)
)
}
export { matchLetters }
export { matchNumbers }
export { isAlphaNumeric }
export { isEmptyOrAlphaNumeric }
export { isAlphaNumeric as isNumberOrStringOnly }
export { isAlphaNumericWithSpace }
export { isEmptyOrAlphaNumericWithSpace }
export { isAlphaNumericWithPlusMinus }
export { isEmptyOrAlphaNumericWithMinus }
export { isNumeric }
export { isValidCity }
export { isAlphaNumericSpecialCharacters }
export { isAlphabet }
export { isEmptyOrValidNumber }
export default isAlphaNumeric