Some utility functions.
Install
browser - global variable
The global variable u
is used, you can change it from here.
browser - requirejs
;
node
const util = ;
Doc
Function | Description |
---|---|
isObj(v) |
Is value an object? |
isNull(v) |
Is value null ? |
isInt(n) |
Is number an integer? |
isOdd(n) |
Is number odd? |
isEmptyObj(v) |
Is value an empty object? |
moveArrItem(arr, from, to) |
Move array item from to. |
negateNum(n) |
Make a positive number negative. |
positNum(n) |
Make a negative number positive. |
reverseNumSign(n) |
Reverse a number's sign. |
randInt(min, max) |
Generate a random integer, between min and max arguments. (default between 0 and 10) |
randFloat(min, max) |
Generate a random floating-point, between min and max arguments. (default between 0 and 10) |
toDecimalPlace(n) |
Filter a floating-point decimal places to a specific amount. |
substrBeforeLast(char, str) |
Get the substring before the last occurrence of char in str . |
substrAfterLast(char, str) |
Get the substring after the last occurrence of char in str . |
substrBeforeFirst(char, str) |
Get the substring before the first occurrence of char in str . |
substrAfterFirst(char, str) |
Get the substring after the last occurrence of char in str . |
extend( obj1, obj2 [, obj3, ...] ) |
Make the right-most object argument inherit from the previous left object arguments. (obj2 inherits from obj1 , obj3 inherits from obj2 .) |
util.extend() example:
{ var inst = {}; // the instance (this) instname = name || "no_name"; instage = age || false; return inst;} { var inst = {} // the instance (this) person = idCounter = 0; inst = util; instjobTitle = jobTitle || "uknown_title"; instemployeId = id || "employe_"+idCounter+=1; return inst;} var employee = ; employeename // "ali"employeeage // 26employeejobTitle // "product_manager"employee id // "employe_1" // You can think of it this way: { supername age; }