FnService - Function Service
FnService is an Angular Factory in the Util module with the name fn.js
. It provides general purpose functions that are useful throughout the client-side application. To use these functions, see the documentation on injecting Angular services.
Note: some of these functions are repeats of jQuery functions. Since we are not using jQuery, we wrote our own implementations.
Name | Summary |
---|---|
isF | Checks if argument is a function. |
isA | Checks if argument is an array. |
isS | Checks if argument is a string. |
isO | Checks if argument is an object. |
contains | Checks if an array contains a value. |
areFunctions | Checks if given strings are names of functions on an API and whether the API has functions that are not listed. |
areFunctionsNonStrict | Checks if given strings are names of functions on an API. (Doesn't care about unlisted functions.) |
windowSize | Returns inner dimensions of the window minus given values. |
isMobile | Returns true if the device viewing the GUI is a mobile device. |
debugOn | Returns true if the debug flag is turned on in query params. |
find | Searches through an array of objects looking for the object with a specific property name and property value and returns its index. |
inArray | Finds an item's index in the array. |
removeFromArray | Removes the first occurrence of the specified item from the array. |
isEmptyObject | Returns true if the object is empty (has no non-default properties). |
cap | Returns the given string with only the first letter capitalized. |
noPx | Return the argument without the 'px' suffix. |
noPxStyle | Return the element's style property without the 'px' suffix. |
endsWith | Checks if the given string ends with a given suffix. |
parseBitRate | Returns a Number version of the given string bit rate. |
Function Descriptions
Note: Green Text means that the return value is truthy. Red Text means that the return value is falsy.
isF
Short for "is Function". Checks if argument is a function.
Example Usage | Arguments | Return Values |
---|---|---|
fs.isF(f ); | f - the variable to check if it is a function |
|
isA
Short for "is Array". Checks if argument is an array.
Example Usage | Arguments | Return Values |
---|---|---|
fs.isA(a ); | a - the variable to check if it is an array |
|
isS
Short for "is String". Checks if argument is a string.
Example Usage | Arguments | Return Values |
---|---|---|
fs.isS(s ); | s - the variable to check if it is a string |
|
isO
Short for "is Object". Checks if argument is an object.
Example Usage | Arguments | Return Values |
---|---|---|
fs.isO(o ); | o - the variable to check if it is an object |
|
contains
Checks if an array contains an item.
Example Usage | Arguments | Return Values |
---|---|---|
fs.contains(a , x ); |
| the index of false if -1 if |
areFunctions
Checks if given strings are names of functions on an API and whether the API has functions that are not listed. This is used for unit testing.
Example Usage | Arguments | Return Values |
---|---|---|
fs.areFunctions(api , fnNames ); |
| true if
false if
|
areFunctionsNonStrict
Checks if given strings are names of functions on an API. This function is similar to areFunctions, but doesn't care about unlisted functions.
Example Usage | Arguments | Return Values |
---|---|---|
fs.areFunctionsNonStrict(api, fnNames); |
| true if
false if
|
windowSize
Returns inner dimensions of the browser window minus given values.
Example Usage | Arguments | Default Params | Return Value |
---|---|---|---|
fs.windowSize(offH , offW ); |
|
| an object with members:
|
isMobile
Returns true if the device viewing the GUI is a mobile device.
Example Usage | Arguments | Return Values |
---|---|---|
fs.isMobile(); | none | true if the device viewing the GUI is a mobile device false otherwise |
debugOn
Returns true if the debug flag is turned on in query params.
Example Usage | Arguments | Return Values |
---|---|---|
fs.debugOn(tag ); | tag : string of which debug flag you are looking for | true if debug flag is turned on for
|
find
Searches through an array of objects looking for the object with a specific property name (tag) and property value (key) and returns its index.
Example Usage | Arguments | Default Params | Return Values |
---|---|---|---|
fs.find(key , array , tag ); |
| tag : 'id' | the index of the object in the array, if found (the index may be 0 – not truthy!) -1 if it wasn't found |
Note: this function searches for a specific name-value pair in an object, as in:
'tag': 'key'
'tag' defaults to 'id' for searching through objects because 'id' is usually unique.
inArray
Finds an item's index in the array.
Example Usage | Arguments | Return Values |
---|---|---|
fs.inArray(item , array ); |
| the index of the item in the array, if found (the index may be 0 – not truthy!) -1 if it wasn't found |
removeFromArray
Removes the first occurrence of the specified item from the array.
Example Usage | Arguments | Return Values |
---|---|---|
fs.removeFromArray(item , array ); |
| true if the removal was made false otherwise |
isEmptyObject
Returns true if the object is empty (has no non-default properties / looks like this: {}
).
Example Usage | Arguments | Return Values |
---|---|---|
fs.isEmptyObject(obj ); | obj : the object to check to see if it is empty | true if it is empty false otherwise |
cap
Returns the given string with only the first letter capitalized.
Example Usage | Arguments | Return Value |
---|---|---|
fs.cap(s ); | s : the string to properly capitalize | s properly capitalized |
Example: fs.cap('sOme STRing'); --> 'Some string'
noPx
Return the argument without the 'px' suffix.
Example Usage | Arguments | Return Values |
---|---|---|
fs.noPx(num ); | num : string number with a 'px' you want to be turned into a number value | the number from the pixel string |
Example: fs.noPx('50px'); --> 50
noPxStyle
Return the element's style property without the 'px' suffix.
Example Usage | Arguments | Return Values |
---|---|---|
fs.noPxStyle(elem , prop ); |
| the number value of the style property |
Example: fs.noPxStyle(d3.select('h2'), 'height'); --> 100
endsWith
Checks if the given string ends with a given suffix.
Example Usage | Arguments | Return Values |
---|---|---|
fs.endsWith(str , suffix ); |
| true if the false otherwise |
parseBitRate
Returns a Number version of the given string bit rate.
Example Usage | Arguments | Return Values |
---|---|---|
fs.parseBitRate(str); | str : the string that you want the bit rate from | the number value of the bit rate |
Example: fs.parseBitRate('4Mbps'); --> 4