Namespace: fnNumberStringUtil

fnNumberStringUtil

The fnNumberStringUtil namespace provides functions for working with numbers in String format.

To use this API alone by itself, include fnNumberStringUtil.js. Otherwise, include faiNumber-include-all.js.

This namespace provide functions for validating strings to see whether if they are valid number and for comparing strings of number.

This namespace has the assume**** functions, of which operate on strings with the assumption that the strings are valid number values. For comparing string of numbers there are the assumeCompare**** functions.

Under the right circumstance, the assume**** functions are very powerful and fast functions. However, under that use case, the strings must be valid number strings for the functions to produce the correct result. Thus, the assume**** functions of this namespace are good for circumstances where the input data have been validated before usage and there isn't a need for revalidating the data.

For an example of one of the assume**** function of this namespace API, the assumeCompare function of this namespace API besides being able to compare numbers, it can also compare dates with fix format. Format like YYYY-MM-DD, YYYY/MM/DD, or similar can be compared. However, for correct results, the month, day, or year value must consistenly be in the same amount of digits(i.e. year 500 and 2000 have to be written as 0500 and 1000, or month 1 and 12 have to be written as 01 and 12). Also, year always have to be first, then month, then finally day.

Example usage of the assumeCompare function.

var a = "1208925819614629174706176";
var b = "1208925819614629174706177";
console.log(fnNumberStringUtil.assumeCompare(a,b));

var d1 = "2018-01-23";
var d2 = "2018-12-24";
console.log(fnNumberStringUtil.assumeCompare(d1,d2));

var d3 = "2018-12-24";
var d4 = "2018-12-03";
console.log(fnNumberStringUtil.assumeCompare(d3,d4));

For comparing strings of numbers, besides the assumeCompare or the assumeCompareAllBase function, this namespace API also provide the compare and the compareAsBase function for comparing strings of number where there is a need to validate the strings when comparing.

For speed-wise, the assumeCompare**** and compare**** functions of this namespace API excel in circumstance where the strings do not have to be compared up until the last digit to know which string is larger or smaller.

Another advantage of the compare**** and assumeCompare**** functions is that, there isn't a upper bound on the length of the input strings. That is as long as memory allow.

Since:
  • 1.0.0.f
Author:
  • Khang Hoang Nguyen
Source:

Members

(static) fileversion

File version number.
Source:

Methods

(static) assumeCompare(firstString, secondString) → {number}

Compare two strings with the assumption that both strings are valid decimal integers.

This function evaluates both, negative and positive values. This function may produce unwanted result if the previous is not met or if the number formatting is incorrect.

This function considers empty strings as smallest. This function disregards leading zeroes. This function considers a string with only a '-' or a '+' sign as 0.

Since:
  • 1.0.0.f
Source:
Parameters:
NameTypeDescription
firstStringstringA string to be compared to the secondString string.
secondStringstringA string to be compared to the firstString string.
Throws:
TypeDescription
string
"Not a string" if either the firstString or the secondString is not of string type.
Returns:
TypeDescription
number
A value of 1 if the firstString is larger than the secondString, -1 if the firstString is smaller than the secondString, or 0 if both strings are equal.

 
 

(static) assumeCompareAllBase(firstString, secondString) → {number}

Compare two strings with the assumption that both strings are valid integers of any radix between 2 and 36.

This function evaluates both, negative and positive values. This function will work, if both strings are integers of the same radix and the radix is from 2 to 36. This function may produce unwanted result if the previous are not met or if the formatting is incorrect.

Digits of radix larger than 10 are 'a' to 'z' with 'a' being 10, and 'z' being 35. A is equal to a, Z is equal to z, and so forth.

This function considers empty strings as smallest. This function disregards leading zeroes. This function considers a string with only a '-' or a '+' sign as 0.

Since:
  • 1.0.0.f
Source:
Parameters:
NameTypeDescription
firstStringstringA string to be compared to the secondString string.
secondStringstringA string to be compared to the firstString string.
Throws:
TypeDescription
string
"Not a string" if either the firstString or the secondString is not of string type.
Returns:
TypeDescription
number
A value of 1 if the firstString is larger than the secondString, -1 if the firstString is smaller than the secondString, or 0 if both strings are equal.

 
 

(static) assumeIsEven(input) → {boolean|undefined}

Check if a string is an even number with the assumption that the string is a valid decimal integer in string representation.
Since:
  • 1.0.0.f
Source:
Parameters:
NameTypeDescription
inputstringA string.
Returns:
TypeDescription
boolean
A value of true if the last character in the string is an odd value, or otherwise, false .
undefined
An undefined value is returned if the string is empty.

 
 

(static) assumeIsOdd(input) → {boolean|undefined}

Check if a string is an odd number with the assumption that the string is a valid decimal integer in string representation.
Since:
  • 1.0.0.f
Source:
Parameters:
NameTypeDescription
inputstringA string.
Returns:
TypeDescription
boolean
A value of true if the last character in the string is an odd value, or otherwise, false .
undefined
An undefined value is returned if the string is empty.

 
 

(static) compare(firstString, secondString) → {number|undefined}

Compare two strings as decimal integers. This function evaluates both, negative and positive values. This function return an undefined value if either one of the strings is an invalid decimal integer. Nonetheless, the strings can be very large in length. This function disregard leading zeroes.

Since:
  • 1.0.0.f
Source:
Parameters:
NameTypeDescription
firstStringstringA string to be compared to the secondString string.
secondStringstringA string to be compared to the firstString string.
Throws:
TypeDescription
string
"Not a string" if either the firstString or the secondString is not of string type.
Returns:
TypeDescription
number
A value of 1 if the firstString is larger than the secondString, -1 if the firstString is smaller than the secondString, or 0 if both strings are equal.
undefined
An undefined value is returned if either one of strings is not a valid decimal integer, or if either one of the strings is empty.

 
 

(static) compareAsBase(firstString, secondString, base) → {number|undefined}

Compare two strings as integers of a define radix. This function evaluates both, negative and positive values. This function return an undefined value if either one of the strings is an invalid integer of the defined radix or if one of the strings is empty. The radix of the strings can only be in between of 2 to 36. There isn't a maximum length for the strings. Nonetheless, the strings can't be empty.

Since:
  • 1.0.0.f
Source:
Parameters:
NameTypeDescription
firstStringstringA string to be compared to the secondString string.
secondStringstringA string to be compared to the firstString string.
basenumberA value that defines the number radix for both strings.
Throws:
TypeDescription
string
"Not a string" if either firstString or secondString is not of string type.
string
"Not a number" if base is not of number type.
string
"Unsupported base" if base value is smaller than 2 or larger than 36.
Returns:
TypeDescription
number
A value of 1 if the firstString is larger than the secondString, -1 if the firstString is smaller than the secondString, or 0 if both strings are equal.
undefined
An undefined value is returned if the either one of the strings is not a valid integer of the defined radix. A single '+' or '-' sign without any digit is an invalid value. An undefined value is also returned if either one of the strings is empty.

 
 

(static) isBase(input, base) → {boolean}

Check if a string is a valid signed integer of the defined radix.
Since:
  • 1.0.0.f
Source:
Parameters:
NameTypeDescription
inputstringA string.
basenumberA value that defines the number radix.
Throws:
TypeDescription
string
"Not a string" if input is not of string type.
string
"Not a number" if base is not of number type.
string
"Unsupported base" if base value is smaller than 2 or larger than 36.
Returns:
TypeDescription
boolean
A value of true if the input string is a valid signed integer of the radix that defined by the base's value, or otherwise, false.

 
 

(static) isBinary(input) → {boolean}

Check if a string is a valid signed binary integer.
Since:
  • 1.0.0.f
Source:
Parameters:
NameTypeDescription
inputstringA string.
Throws:
TypeDescription
string
"Not a string" if input is not of string type.
Returns:
TypeDescription
boolean
A value of true if the input string is a valid signed binary integer, or otherwise, false.

 
 

(static) isHex(input) → {boolean}

Check if a string is a valid signed hexadecimal integer.
Since:
  • 1.0.0.f
Source:
Parameters:
NameTypeDescription
inputstringA string.
Throws:
TypeDescription
string
"Not a string" if input is not of string type.
Returns:
TypeDescription
boolean
A value of true if the input string is a valid signed hexadecimal integer, or otherwise, false.

 
 

(static) isInteger(input) → {boolean}

Check if a string is a valid signed decimal integer.
Since:
  • 1.0.0.f
Source:
Parameters:
NameTypeDescription
inputstringA string.
Throws:
TypeDescription
string
"Not a string" if input is not of string type.
Returns:
TypeDescription
boolean
A value of true if the input string is a valid signed decimal integer, or otherwise, false.

 
 

(static) isOctal(input) → {boolean}

Check if a string is a valid signed octal integer.
Since:
  • 1.0.0.f
Source:
Parameters:
NameTypeDescription
inputstringA string.
Throws:
TypeDescription
string
"Not a string" if input is not of string type.
Returns:
TypeDescription
boolean
A value of true if the input string is a valid signed octal integer, or otherwise, false.

 
 

(static) isUnsignedBase(input, base) → {boolean}

Check if a string is a valid unsigned integer of the defined radix.
Since:
  • 1.0.0.f
Source:
Parameters:
NameTypeDescription
inputstringA string.
basenumberA value that defines the number radix.
Throws:
TypeDescription
string
"Not a string" if input is not of string type.
string
"Not a number" if base is not of number type.
string
"Unsupported base" if base value is smaller than 2 or larger than 36.
Returns:
TypeDescription
boolean
A value of true if the input string is a valid unsigned integer of the radix that defined by the base's value, or otherwise, false.

 
 

(static) isUnsignedBinary(input) → {boolean}

Check if a string is a valid unsigned binary integer.
Since:
  • 1.0.0.f
Source:
Parameters:
NameTypeDescription
inputstringA string.
Throws:
TypeDescription
string
"Not a string" if input is not of string type.
Returns:
TypeDescription
boolean
A value of true if the input string is a valid unsigned binary integer, or otherwise, false.

 
 

(static) isUnsignedHex(input) → {boolean}

Check if a string is a valid unsigned hexadecimal integer.
Since:
  • 1.0.0.f
Source:
Parameters:
NameTypeDescription
inputstringA string.
Throws:
TypeDescription
string
"Not a string" if input is not of string type.
Returns:
TypeDescription
boolean
A value of true if the input string is a valid unsigned hexadecimal integer, or otherwise, false.

 
 

(static) isUnsignedInteger(input) → {boolean}

Check if a string is a valid unsigned decimal integer.
Since:
  • 1.0.0.f
Source:
Parameters:
NameTypeDescription
inputstringA string.
Throws:
TypeDescription
string
"Not a string" if input is not of string type.
Returns:
TypeDescription
boolean
A value of true if the input string is a valid unsigned decimal integer, or otherwise, false.

 
 

(static) isUnsignedOctal(input) → {boolean}

Check if a string is a valid unsigned octal integer.
Since:
  • 1.0.0.f
Source:
Parameters:
NameTypeDescription
inputstringA string.
Throws:
TypeDescription
string
"Not a string" if input is not of string type.
Returns:
TypeDescription
boolean
A value of true if the input string is a valid unsigned octal integer, or otherwise, false.