[javascript] Validate that a string is a positive integer

Most of the time you need this type of check for database usage, like checking if string valid userId. Bacause of that there can't be any strange symbols that can be parced as integer. Also integer should be in database range of integer. You just need normal int like 1,2,3 and so on.

const isStrNormPosInt = (str: string) => {
  return /^([1-9]\d*)$/.test(str) && Number(str) <= 2147483647 // postgres max int
}

If check is passed, you can just convert it to number Number(str)