2.0.3 • Published 6 years ago
util-ma v2.0.3
Some utility functions.
Install
browser - global variable
<script src="util-ma.js"></script>
The global variable u
is used, you can change it from here.
browser - requirejs
define(['util-ma'], function (util) {
util.isObj([]) // false
});
node
const util = require('util-ma');
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:
function newPerson(name, age) {
var inst = {}; // the instance (this)
inst.name = name || "no_name";
inst.age = age || false;
return inst;
}
function newEmployee(name, age, jobTitle, id) {
var inst = {}, // the instance (this)
person = newPerson(name, age),
idCounter = 0;
inst = util.extend( person, inst );
inst.jobTitle = jobTitle || "uknown_title";
inst.employeId = id || "employe_"+(idCounter+=1);
return inst;
}
var employee = newEmployee("ali", 26, "product_manager");
employee.name // "ali"
employee.age // 26
employee.jobTitle // "product_manager"
employee .id // "employe_1"
// You can think of it this way:
class Employee extends Person {
constructor(name, age, jobTitle, id) {
super(name, age);
}
}
2.0.3
6 years ago
2.0.2
6 years ago
2.0.1
6 years ago
2.0.0
6 years ago
1.1.20
6 years ago
1.1.19
6 years ago
1.1.18
6 years ago
1.1.17
6 years ago
1.1.16
6 years ago
1.1.15
6 years ago
1.1.14
7 years ago
1.1.13
7 years ago
1.1.12
8 years ago
1.1.11
8 years ago
1.1.10
8 years ago
1.1.9
8 years ago
1.1.8
8 years ago
1.1.7
8 years ago
1.1.6
8 years ago
1.1.5
8 years ago
1.1.4
8 years ago
1.1.3
8 years ago
1.1.2
8 years ago
1.1.1
8 years ago
1.1.0
8 years ago
1.0.9
8 years ago
1.0.8
8 years ago
1.0.7
8 years ago
1.0.6
8 years ago
1.0.5
8 years ago
1.0.4
8 years ago
1.0.3
8 years ago
1.0.2
8 years ago
1.0.1
8 years ago
1.0.0
8 years ago