Cool features missing in JavaScript
The getRandomArrayValue()
method returns a random value from the given array.
const array1 = [1, 2, 3, 4];
console.log(Coolfeatures.getRandomArrayValue(array1));
// expected output: 1, 2, 3, or 4
var randomElement = Coolfeatures.getRandomArrayValue(array)
array
The array where a random element should be selected from.
A random selected element from array
.
The Coolfeatures.filterObject()
method creates a new object with all elements that pass the test implemented by the provided function.
const personAges = {
hans: 45,
peter: 33,
jens: 68,
mike: 21
};
console.log(Coolfeatures.filterObject(personAges, age => age < 40));
// expected output: {peter: 33, mike: 21}
var newObject = Coolfeatures.filterObject(object, callback([element[, key[, object]]]))
object
The object to filter.
callback
Function is a predicate, to test each element of the object. Return true to keep the element, false otherwise. It accepts three arguments:
element
The current element being processed in the object.
key
The key of the current element being processed in the object.
object
The object Coolfeatures.filterObject
was provided with.
A new object with the elements that pass the test. If no elements pass the test, an empty object will be returned.
The isObjectEmpty()
checks an object for being empty.
const emptyObject = {};
console.log(Coolfeatures.isObjectEmpty(emptyObject));
// expected output: true
const filledObject = {empty: false};
console.log(Coolfeatures.isObjectEmpty(filledObject));
// expected output: false
var objectIsEmpty = Coolfeatures.isObjectEmpty(object)
object
The object to check for being empty.
true
if the object is empty, otherwise false
.
The getRandomIntBetween()
method returns a random number between two given numbers.
console.log(Coolfeatures.getRandomIntBetween(5, 10));
// expected output: a number between 5 and 10
var randomInt = Coolfeatures.getRandomIntBetween(min, max)
min
The lowest possible random-generated number.
max
The highest possible random-generated number.
A random number between the given min and max number.
The htmlStringToElement()
method parses a HTML string to an Element
.
const html = '<div id="my-div">Hello World!</div>'
console.log(Coolfeatures.htmlStringToElement(html));
// expected output: a new HTMLDivElement whose outerHTML is like html
var element = Coolfeatures.htmlStringToElement(htmlString)
htmlString
The HTML string to convert to an element.
A new Element
that got parsed from htmlString
.