number/nth.ts

/** @module Number */

import { equalsNaN } from './equalsNaN'
import { isNum } from './isNum'
import { getNums } from './getNums'
import { toNum } from './toNum'

/**
 * Finds the number ext base on the passed in number.
 * @example
 * nth(1)
 * // Returns 'st'
 * @example
 * nth(2)
 * // Returns 'nd'
 * @example
 * nth(5)
 * // Returns 'th'
 * @function
 * @param {Number} num - value to check
 * @return {String} ext of the number
 */
export const nth = (num: number): string => {
  if (!isNum(num)) {
    const strNum = getNums(num)
    if (!strNum) return ''
    num = toNum(strNum)
    if (equalsNaN(num)) return ''
  }

  const mod = num % 100
  if (mod >= 10 && mod <= 20) return 'th'

  switch (num % 10) {
    case 1:
      return 'st'
    case 2:
      return 'nd'
    case 3:
      return 'rd'
    default:
      return 'th'
  }
}