Skip to content

roysharon/uninums

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Javascript supports Unicode strings, but parsing such strings to numbers is unsupported (e.g., the user enters a phone number using Chinese numerals).
uninums.js is a small utility script that implements five methods for handling non-ASCII numerals in Javascript:

Function Description
normalDigits(s) Normalizes string s by replacing all non-ASCII digits with ASCII digits.

  • normalDigits(‘٠۴६’) == ’046′
  • normalDigits(’123′) == ’123′
normalSpaces(s) Normalizes string s by replacing all whitespace characters with either a space (‘\x20′) or a newline (‘\n’) as appropriate:

  • normalSpaces(‘Hello\t\rWorld’) == ‘Hello\x20\nWorld’
  • normalSpaces(‘\xA0\u2003′) == ‘\x20\x20′
  • normalSpaces(‘\u2028) == ‘\n’

As a special case, normalSpaces() also replaces CRLF to a single newline character. So normalSpaces(‘\r\n’) == ‘\n’.

parseUniInt(s,r) Returns the integer value at the start of string s, ignoring leading spaces and using radix r. This is equivalent to the behavior of Javascript’s internal parseInt() function, but also handles non-ASCII digits:

  • parseUniInt(‘٠۴६’, 10) == parseInt(’046′, 10) == 46
  • parseUniInt(‘٠۴६’) == parseInt(’046′) == 38 // assumes radix=8 due to leading zero
  • parseUniInt(‘٠۴६hello’) == parseInt(’046hello’) == 38
  • parseUniInt(‘hello’) == parseInt(‘hello’) == NaN
parseUniFloat(s) Returns the float value at the start of string s, ignoring leading spaces. This is equivalent to the behavior of Javascript’s internal parseFloat() function, but also handles non-ASCII digits:

  • parseUniFloat(‘٠۴.६’) == parseFloat(’04.6′) == 4.6
  • parseUniFloat(‘٠۴.६hello’) == parseFloat(’04.6hello’) == 4.6
  • parseUniFloat(‘hello’) == parseFloat(‘hello’) == NaN
sortNumeric(a) Sorts array a in place according to the numeric float values of its items:

  • sortNumeric(['3 dogs','10 cats','2 mice']) == ['2 mice','3 dogs','10 cats']
  • sortNumeric(['٣ dogs','١٠ cats','٢ mice']) == ['٢ mice','٣ dogs','١٠ cats']

Note that using Javascript’s internal sort() function will order ’10 cats’ before ’2 mice’ because it is string based rather than numeric.

For further information on how these functions are implemented see here.

Using or modifying this project is subject to the MIT License.

About

A Javascript micro-library for handling non-ASCII numerals.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published