Skip to content

Latest commit

 

History

History
1015 lines (704 loc) · 30.7 KB

ilibCommon.md

File metadata and controls

1015 lines (704 loc) · 30.7 KB

Modules

ModuleDescription
JSUtils
MathUtils
Path
SearchUtils
Utils

Classes

ISet

A polyfill for Set in older browsers.

JSUtils


JSUtils.isArray(object) ⇒ boolean

Polyfill to test whether an object is an javascript array.

Kind: static method of JSUtils
Returns: boolean - return true if the object is an array and false otherwise

Param Type Description
object * The object to test

JSUtils.shallowCopy(source, target)

Perform a shallow copy of the source object to the target object. This only copies the assignments of the source properties to the target properties, but not recursively from there.

Kind: static method of JSUtils

Param Type Description
source Object the source object to copy properties from
target Object the target object to copy properties into

JSUtils.deepCopy(from, to) ⇒ Object

Perform a recursive deep copy from the "from" object to the "deep" object.

Kind: static method of JSUtils
Returns: Object - a reference to the the "to" object

Param Type Description
from Object the object to copy from
to Object the object to copy to

JSUtils.mapString(str, map) ⇒ string

Map a string to the given set of alternate characters. If the target set does not contain a particular character in the input string, then that character will be copied to the output unmapped.

Kind: static method of JSUtils
Returns: string - the source string where each character is mapped to alternate characters

Param Type Description
str string a string to map to an alternate set of characters
map Array.<string> | Object a mapping to alternate characters

JSUtils.indexOf(array, obj) ⇒ number

Check if an object is a member of the given array. This is a polyfill for Array.indexOf. If this javascript engine support indexOf, it is used directly. Otherwise, this function implements it itself. The idea is to make sure that you can use the quick indexOf if it is available, but use a slower implementation in older engines as well.

Kind: static method of JSUtils
Returns: number - index of the object in the array, or -1 if it is not in the array.

Param Type Description
array Array.<(Object|string|number)> array to search
obj Object | string | number object being sought. This should be of the same type as the members of the array being searched. If not, this function will not return any results.

JSUtils.pad(str, length, [right])

Pad the str with zeros to the given length of digits.

Kind: static method of JSUtils

Param Type Description
str string | number the string or number to pad
length number the desired total length of the output string, padded
[right] boolean if true, pad on the right side of the number rather than the left. Default is false.

JSUtils.toHexString(string, [limit]) ⇒ string

Convert a string into the hexadecimal representation of the Unicode characters in that string.

Kind: static method of JSUtils
Returns: string - a hexadecimal representation of the Unicode characters in the input string

Param Type Description
string string The string to convert
[limit] number the number of digits to use to represent the character (1 to 8)

JSUtils.isDate(object) ⇒ boolean

Test whether an object in a Javascript Date.

Kind: static method of JSUtils
Returns: boolean - return true if the object is a Date and false otherwise

Param Type Description
object Object | null | undefined The object to test

JSUtils.merge(object1, object2, [replace], [name1], [name2]) ⇒ Object

Merge the properties of object2 into object1 in a deep manner and return a merged object. If the property exists in both objects, the value in object2 will overwrite the value in object1. If a property exists in object1, but not in object2, its value will not be touched. If a property exists in object2, but not in object1, it will be added to the merged result.

Name1 and name2 are for creating debug output only. They are not necessary.

Kind: static method of JSUtils
Returns: Object - the merged object

Param Type Description
object1 * the object to merge into
object2 * the object to merge
[replace] boolean if true, replace the array elements in object1 with those in object2. If false, concatenate array elements in object1 with items in object2.
[name1] string name of the object being merged into
[name2] string name of the object being merged in

JSUtils.isEmpty(obj) ⇒ boolean

Return true if the given object has no properties.

Kind: static method of JSUtils
Returns: boolean - true if the given object has no properties, false otherwise

Param Type Description
obj Object the object to check

JSUtils.hashCode()

Kind: static method of JSUtils


JSUtils.callAll(arr, action, callback)

Calls the given action function on each element in the given array arr asynchronously and in order and finally call the given callback when they are all done. The action function should take the array to process as its parameter, and a callback function. It should process the first element in the array and then call its callback function with the result of processing that element (if any).

Kind: static method of JSUtils

Param Type Description
arr Array.<Object> the array to process
action function the action to perform on each element of the array
callback function the callback function to call with the results of processing each element of the array.

JSUtils.extend(object1, [object2]) ⇒ Object

Extend object1 by mixing in everything from object2 into it. The objects are deeply extended, meaning that this method recursively descends the tree in the objects and mixes them in at each level. Arrays are extended by concatenating the elements of object2 onto those of object1.

Kind: static method of JSUtils
Returns: Object - returns object1

Param Type Description
object1 Object the target object to extend
[object2] Object the object to mix in to object1

JSUtils.fromCodePoint(codepoint) ⇒ string

Convert a UCS-4 code point to a Javascript string. The codepoint can be any valid UCS-4 Unicode character, including supplementary characters. Standard Javascript only supports supplementary characters using the UTF-16 encoding, which has values in the range 0x0000-0xFFFF. String.fromCharCode() will only give you a string containing 16-bit characters, and will not properly convert the code point for a supplementary character (which has a value > 0xFFFF) into two UTF-16 surrogate characters. Instead, it will just just give you whatever single character happens to be the same as your code point modulo 0x10000, which is almost never what you want.

Similarly, that means if you use String.charCodeAt() you will only retrieve a 16-bit value, which may possibly be a single surrogate character that is part of a surrogate pair representing a character in the supplementary plane. It will not give you a code point. Use IString.codePointAt() to access code points in a string, or use an iterator to walk through the code points in a string.

Kind: static method of JSUtils
Returns: string - a string containing the character represented by the codepoint

Param Type Description
codepoint number UCS-4 code point to convert to a character

JSUtils.toCodePoint(str, index) ⇒ number

Convert the character or the surrogate pair at the given index into the intrinsic Javascript string to a Unicode UCS-4 code point.

Kind: static method of JSUtils
Returns: number - code point of the character at the given index into the string

Param Type Description
str string string to get the code point from
index number index into the string

MathUtils


MathUtils.signum(num) ⇒ number

Return the sign of the given number. If the sign is negative, this function returns -1. If the sign is positive or zero, this function returns 1.

Kind: static method of MathUtils
Returns: number - -1 if the number is negative, and 1 otherwise

Param Type Description
num number the number to test

MathUtils.floor(num) ⇒ number

Kind: static method of MathUtils
Returns: number - rounded number
Access: protected

Param Type Description
num number number to round

MathUtils.ceiling(num) ⇒ number

Kind: static method of MathUtils
Returns: number - rounded number
Access: protected

Param Type Description
num number number to round

MathUtils.down(num) ⇒ number

Round a number towards 0.

Kind: static method of MathUtils
Returns: number - rounded number

Param Type Description
num number number to round

MathUtils.up(num) ⇒ number

Round a number away from 0.

Kind: static method of MathUtils
Returns: number - rounded number

Param Type Description
num number number to round

MathUtils.halfup(num) ⇒ number

Round a number up away from 0 if it is half way or larger, otherwise round down.

Kind: static method of MathUtils
Returns: number - rounded number

Param Type Description
num number number to round

MathUtils.halfdown(num) ⇒ number

Round a number down towards 0 if it is half way or smaller, otherwise round up.

Kind: static method of MathUtils
Returns: number - rounded number

Param Type Description
num number number to round

MathUtils.halfeven(num) ⇒ number

Round numbers up to the closest even number.

Kind: static method of MathUtils
Returns: number - rounded number

Param Type Description
num number number to round

MathUtils.halfodd(num) ⇒ number

Round numbers up to the closest odd number.

Kind: static method of MathUtils
Returns: number - rounded number

Param Type Description
num number number to round

MathUtils.mod(dividend, modulus) ⇒

Do a proper modulo function. The Javascript % operator will give the truncated division algorithm, but for calendrical calculations, we need the Euclidean division algorithm where the remainder of any division, whether the dividend is negative or not, is always a positive number in the range [0, modulus).

Kind: static method of MathUtils
Returns: the remainder of dividing the dividend by the modulus.

Param Type Description
dividend number the number being divided
modulus number the number dividing the dividend. This should always be a positive number.

MathUtils.amod(dividend, modulus) ⇒

Do a proper adjusted modulo function. The Javascript % operator will give the truncated division algorithm, but for calendrical calculations, we need the Euclidean division algorithm where the remainder of any division, whether the dividend is negative or not, is always a positive number in the range (0, modulus]. The adjusted modulo function differs from the regular modulo function in that when the remainder is zero, the modulus should be returned instead.

Kind: static method of MathUtils
Returns: the remainder of dividing the dividend by the modulus.

Param Type Description
dividend number the number being divided
modulus number the number dividing the dividend. This should always be a positive number.

MathUtils.shiftDecimal(number, precision) ⇒ number

Return the number with the decimal shifted by the given precision. Positive precisions shift the decimal to the right giving larger numbers, and negative ones shift the decimal to the left giving smaller numbers.

Kind: static method of MathUtils
Returns: number - the number with the decimal point shifted by the given number of decimals

Param Type Description
number number the number to shift
precision number the number of places to move the decimal point

MathUtils.log10(num) ⇒ number

Returns the base 10 logarithm of a number. For platforms that support Math.log10() it is used directly. For plaforms that do not, such as Qt/QML, it will be calculated using a polyfill.

Kind: static method of MathUtils
Returns: number - the base-10 logarithm of the given number

Param Type Description
num number the number to take the logarithm of

MathUtils.significant(number, digits, round) ⇒ number

Return the given number with only the given number of significant digits. The number of significant digits can start with the digits greater than 1 and straddle the decimal point, or it may start after the decimal point. If the number of digits requested is less than 1, the original number will be returned unchanged.

Kind: static method of MathUtils
Returns: number - the given number with only the requested number of significant digits

Param Type Description
number number the number to return with only significant digits
digits number the number of significant digits to include in the returned number
round function a rounding function to use

Path


SearchUtils


SearchUtils.bsearch(target, arr, [comparator]) ⇒

Binary search a sorted array for a particular target value. If the exact value is not found, it returns the index of the smallest entry that is greater than the given target value.

The comparator parameter is a function that knows how to compare elements of the array and the target. The function should return a value greater than 0 if the array element is greater than the target, a value less than 0 if the array element is less than the target, and 0 if the array element and the target are equivalent.

If the comparator function is not specified, this function assumes the array and the target are numeric values and should be compared as such.

Kind: static method of SearchUtils
Returns: the index of the array into which the value would fit if inserted, or -1 if given array is not an array or the target is not a number

Param Type Description
target * element being sought
arr Array the array being searched
[comparator] function a comparator that is appropriate for comparing two entries in the array

SearchUtils.bisectionSearch(target, low, high, precision, [func]) ⇒

Do a bisection search of a function for a particular target value.

The function to search is a function that takes a numeric parameter, does calculations, and returns gives a numeric result. The function should should be smooth and not have any discontinuities between the low and high values of the parameter.

Kind: static method of SearchUtils
Returns: an approximation of the input value to the function that gives the desired target output value, correct to within the error range of Javascript floating point arithmetic, or NaN if there was some error

Param Type Description
target number value being sought
low number the lower bounds to start searching
high number the upper bounds to start searching
precision number minimum precision to support. Use 0 if you want to use the default.
[func] function function to search

Utils


Utils.getSublocales(locale) ⇒ Array.<string>

Return an array of locales that represent the sublocales of the given locale. These sublocales are intended to be used to load locale data. Each sublocale might be represented separately by files on disk in order to share them with other locales that have the same sublocales. The sublocales are given in the order that they should be loaded, which is least specific to most specific.

For example, the locale "en-US" would have the sublocales "root", "en", "und-US", and "en-US".

Variations

With only language and region specified, the following sequence of sublocales will be generated:

root
language
und-region
language-region

With only language and script specified:

root
language
language-script

With only script and region specified:

root
und-region

With only region and variant specified:

root
und-region
region-variant

With only language, script, and region specified:

root
language
und-region
language-script
language-region
language-script-region

With only language, region, and variant specified:

root
language
und-region
language-region
und-region-variant
language-region-variant

With all parts specified:

root
language
und-region
language-script
language-region
und-region-variant
language-script-region
language-region-variant
language-script-region-variant

Kind: static method of Utils
Returns: Array.<string> - An array of locale specifiers that are the sublocales of the given on

Param Type Description
locale Locale | String the locale to find the sublocales for

Utils.getLocFiles(locale, name) ⇒ Array.<string>

Return an array of relative path names for the files that represent the data for the given locale.

Note that to prevent the situation where a directory for a language exists next to the directory for a region where the language code and region code differ only by case, the plain region directories are located under the special "undefined" language directory which has the ISO code "und". The reason is that some platforms have case-insensitive file systems, and you cannot have 2 directories with the same name which only differ by case. For example, "es" is the ISO 639 code for the language "Spanish" and "ES" is the ISO 3166 code for the region "Spain", so both the directories cannot exist underneath "locale". The region therefore will be loaded from "und/ES" instead.

Variations

With only language and region specified, the following sequence of paths will be generated:

language
und/region
language/region

With only language and script specified:

language
language/script

With only script and region specified:

und/region

With only region and variant specified:

und/region
region/variant

With only language, script, and region specified:

language
und/region
language/script
language/region
language/script/region

With only language, region, and variant specified:

language
und/region
language/region
region/variant
language/region/variant

With all parts specified:

language
und/region
language/script
language/region
region/variant
language/script/region
language/region/variant
language/script/region/variant

Kind: static method of Utils
Returns: Array.<string> - An array of relative path names for the files that contain the locale data

Param Type Description
locale Locale load the files for this locale
name string the file name of each file to load without any path

ISet

A polyfill for Set in older browsers.

Kind: global class


new ISet([elements])

Create a new set with elements in the given array. The type of the set is gleaned from the type of the first element in the elements array, or the first element added to the set. The type may be "string" or "number", and all elements will be returned as elements of that type.

Param Type Description
[elements] Array.<(string|number)> initial elements to add to the set

iSet.add(element) ⇒ boolean

Adds the specified element or array of elements to this set if it is or they are not already present.

Kind: instance method of ISet
Returns: boolean - true if this set did not already contain the specified element[s]

Param Type Description
element * | Array.<*> element or array of elements to add

iSet.clear()

Removes all of the elements from this set.

Kind: instance method of ISet


iSet.contains(element) ⇒ boolean

Returns true if this set contains the specified element.

Kind: instance method of ISet

Param Type Description
element * the element to test

iSet.isEmpty() ⇒ boolean

Returns true if this set contains no elements.

Kind: instance method of ISet


iSet.remove(element) ⇒ boolean

Removes the specified element from this set if it is present.

Kind: instance method of ISet
Returns: boolean - true if the set contained the specified element

Param Type Description
element * the element to remove

iSet.asArray() ⇒ Array.<*>

Return the set as a javascript array.

Kind: instance method of ISet
Returns: Array.<*> - the set represented as a javascript array


iSet.toJson() ⇒ string

Represents the current set as json.

Kind: instance method of ISet
Returns: string - the current set represented as json


iSet.toJS() ⇒ *

Convert to a javascript representation of this object. In this case, it is a normal JS array.

Kind: instance method of ISet
Returns: * - the JS representation of this object


iSet.fromJS() ⇒ ISet | undefined

Convert from a js representation to an internal one.

Kind: instance method of ISet
Returns: ISet | undefined - the current object, or undefined if the conversion did not work