JavaScript helpful functions
- Add a file mint-utils.js (does not have any dependencies) on the page.
<head>
</head>
<body>
<script src="../src/mint-utils.js"></script>
</body>Functionality is tested in Chrome >= 46, Opera >= 33.0, Firefox >= 31.0, IE >= 11
This library is delivered with Jasmine tests. To run tests open SpecRunner.html in your favorite browser.
- Mint.utils.createGuid()
- Mint.utils.getRandomInt(min, max)
- Mint.utils.isArray(value)
- Mint.utils.isFunction(value)
- Mint.utils.isString(value)
- Mint.utils.isInteger(value)
- Mint.utils.isFloat(value)
- Mint.utils.trimString(value)
- Mint.utils.arrayFirst(array, predicate)
- Mint.utils.arrayRemoveItem(array, itemToRemove)
- Mint.utils.dictionaryForEach(dictionary, callback)
- Mint.utils.clone(obj)
The createGuid() method generates new random Guid.
var guid = Mint.utils.createGuid();New random Guid (string).
Mint.utils.createGuid(); // "0ef73963-bea8-ebf9-af71-897958421e2e"The getRandomInt() method returns a random number within a specified range.
var random = Mint.utils.getRandomInt([min[, max]]);- min - (int, optional) The inclusive lower bound of the random number returned. Default value is 0.
- max - (int, optional) The exclusive upper bound of the random number returned. Max must be greater than or equal to min. Default value is Number.MAX_VALUE.
A signed integer greater than or equal to min and less or equal than max; that is, the range of return values includes min and max. If min equals max, min is returned.
Mint.utils.getRandomInt(1, 8); // 6
Mint.utils.getRandomInt(1); // 1.794066025907234e+308
Mint.utils.getRandomInt(); // 1.0024528559121937e+308
Mint.utils.getRandomInt("12"); // ErrorThe isArray() method determines whether the tested object is Array.
var isArray = Mint.utils.isArray([]);- value - (object) An object for check.
true if the tested object is Array; otherwise, false.
Mint.utils.isArray([1, 2, 3]); // true
Mint.utils.isArray(new array()); // true
Mint.utils.isArray("a"); // false
Mint.utils.isArray(null); // falseThe isFunction() method determines whether the tested object is Function.
var testedObject = new function(){};
var isFunction = Mint.utils.isFunction(testedObject);- value - (object) An object for check.
true if the tested object is Function; otherwise, false.
function test(){ };
Mint.utils.isFunction(test); // true
Mint.utils.isFunction("a"); // false
Mint.utils.isFunction(null); // false
Mint.utils.isFunction([1, 2, 3]); // falseThe isString() method determines whether the tested object is string.
var testedObject = "lorem impus";
var isString = Mint.utils.isString(testedObject);- value - (object) An object for check.
true if the tested object is string; otherwise, false.
Mint.utils.isString("lorem impus"); // true
Mint.utils.isString(12); // false
Mint.utils.isString(function(){ }); // false
Mint.utils.isString(null); // false
Mint.utils.isString([1, 2, 3]); // falseThe isInteger() method determines whether the passed value is an integer.
For more information visit developer.mozilla.org
var isInteger = Mint.utils.isInteger(12);- value - The value to be tested for being an integer.
true if the tested object is integer; otherwise, false.
Mint.utils.isInteger(0.1); // false
Mint.utils.isInteger(1); // true
Mint.utils.isInteger(Math.PI); // false
Mint.utils.isInteger(-100000); // true
Mint.utils.isInteger(0); // true
Mint.utils.isInteger("10"); // false
Mint.utils.isInteger(null); // falseThe isFloat() method determines whether the passed value is an float.
var isFloat = Mint.utils.isFloat(1.2);- value - The value to be tested for being a float.
true if the tested object is integer; otherwise, false.
Mint.utils.isFloat(0.1); // true
Mint.utils.isFloat(-10.1); // true
Mint.utils.isFloat(1); // true
Mint.utils.isFloat(Math.PI); // true
Mint.utils.isFloat(-100000); // true
Mint.utils.isFloat(NaN); // false
Mint.utils.isFloat(0); // true
Mint.utils.isFloat("10"); // falseThe trimString() method removes spaces from the beginning and the ending of the string but not from the middle of the string.
var trimmedStr = Mint.utils.trimString(" Lorem impus ");- value - string for trim.
The string that remains after all spaces are removed from the start and end of the inner string.
Mint.utils.trimString(" Lorem impus "); // "Lorem impus"
Mint.utils.trimString("Lorem impus"); // "Lorem impus"
Mint.utils.trimString(" Lorem ipsum dolor sit amet"); // "Lorem ipsum dolor sit amet"
Mint.utils.trimString(" "); // ""
Mint.utils.trimString(""); // ""
Mint.utils.trimString(null); // error
Mint.utils.trimString(12); // errorThe arrayFirst() method returns the first item of an array that is matched the predicate function.
var first = Mint.utils.arrayFirst(array, predicate);- array - array in which item is searching.
- predicate - (function) function that returns true for searching item.
The first item of an array that is matched the predicate function; null if item is not found.
var array = [1, 3, 8];
var first = Mint.utils.arrayFirst(array, function (item){
return item > 2;
});
// first equal 3
first = Mint.utils.arrayFirst(array, function (item){
return item > 10;
});
// first is nullThe arrayRemoveItem() method removes first accurance of item from an array. The arrayRemoveItem() method removes item from array that is passed as first parameter.
Mint.utils.arrayRemoveItem(array, itemToRemove);- array - array in which item is searching.
- itemToRemove - (object) item of array that should be removed.
var array = [1, 3, 8];
Mint.utils.arrayRemoveItem(array, 3); // array == [1, 8]The dictionaryForEach() method executes a provided function once per dictionary element. The dictionaryForEach() method executes only for own properties of an object.
- dictionary - object that structured like a dictionary, for example, {a: 1, b: 2}.
- callback - function to execute for each element, taking two arguments:
- key - the current element's key being processed in the dictionary, and
- value - the current element's value.
var dict = {a: 1, b: 2};
function logDictionaryElements(key, value) {
console.log("key = " + key + "; value = " + value);
}
Mint.utils.dictionaryForEach(dict, logDictionaryElements);
// logs:
// key = a; value = 1
// key = b; value = 2
### 12. Mint.utils.clone()
> The **clone()** method returns a clone of the object (only data fields, not function).
#### Syntax
```js
var obj = {a: 1};
var clone = Mint.utils.clone(obj);object that is clone of the passed object.
var value = { a: { b: 1 } };
var clone = Mint.utils.clone(); // { a: { b: 1 } }
value = {
a: {
b: 1,
foo: function(){}
},
c: "lorem"
};
clone = Mint.utils.clone(); // { a: { b: 1 }, c: "lorem" }
value = "Lorem";
clone = Mint.utils.clone(); // "Lorem"