@kofijs/utils v0.2.0
@kofijs/utils
A tasty micro utilities library
Installation
Use npm to install this module:
npm install --save @kofijs/utilsUse it in your browser:
<!-- Develop version (not minified) -->
<script type="text/javascript" src="./node_modules/@kofijs/utils/kofi-utils.js"></script>
<!-- Minified version -->
<script type="text/javascript" src="./node_modules/@kofijs/utils/kofi-utils.min.js"></script>Use it with your ES6 modules:
//Load the full library
import * as kofi from "@kofijs/utils";
//Load single methods
import {max, min} from "@kofijs/utils";API
kofi.average(array)
Returns the average of the values in array.
kofi.average([1, 2, 3, 4, 5]); // -> 3kofi.camelCase(str)
Returns the camel-case format of str.
kofi.camelCase("hello world"); // -> "helloWorld"kofi.capitalize(str)
Returns the capitalized format of str.
kofi.capitalize("hello world"); // -> "Hello world"kofi.concat(array, *values)
Returns a new array concatenating array with other arrays or values passed.
kofi.concat([1, 2, 3, 4], [5, 6], [7]); // -> [1, 2, 3, 4, 5, 6, 7]
kofi.concat([1], 2, [3, 4], null); // -> [1, 2, 3, 4, null]kofi.deepClone(obj)
Added in v0.1.0
Returns a deep clone of obj. Supports cloning arrays, objects, strings, booleans and numbers.
let obj = [{"foo": true}, {"bar": false}];
let clonedObj = kofi.deepClone(obj);
console.log(clonedObj[0] === obj[0]); // -> false
console.log(clonedObj[1] === obj[1]); // -> falsekofi.delay(time, fn)
This is just setTimeout but with the arguments reverted (first the delay time in ms, then the callback fn function).
kofi.delay(1000, function () {
console.log("Hello after 1 second!!");
});kofi.digits(num)
Counts the number of digits of num.
kofi.digits(12345); // -> 5kofi.each(array, fn)
Iterates over an array or an object.
items:arrayorobjectyou want to iterate.fn: function that will be called with each item of theitemsarray or object with the following arguments:- First argument: the property name if
itemsis an object, or the index ifitemsis an array. - Second argument: the property value if
itemsis an object, or the value ifitemsis an array.
- First argument: the property name if
You can stop the iteration by returning false in the iterator function
//Iterate over an array
kofi.each([1, 2, 3], function (index, value) {
console.log(index + " -> " + value);
});
// 0 -> 1
// 1 -> 2
// 2 -> 3
//Iterate over an object
kofi.each({"key1": "value1", "key2": "value2"}, function (key, value) {
console.log(key + " -> " + value);
});
// key1 -> value1
// key2 -> value2kofi.equal(value1, value2)
Added in v0.2.0
Determines if two values are equal. Works with strings, numbers, booleans, objects and arrays.
let obj1 = {"key": "value"};
let obj2 = {"key": "value"};
let obj3 = {"key": null};
kofi.equal(obj1, obj2); // --> true
kofi.equal(obj1, obj3); // --> falsekofi.extract(obj, keys)
Creates a new object with only the provided keys of obj.
kofi.extract({a: 1, b: 2, c: 3}, ["a", "c"]) // -> {a: 1, c: 3}kofi.fill(length, value)
Returns a new array with size length filled with value. Only string or number values are allowed.
//Fill an array with a number
kofi.fill(5, 0); // -> [0, 0, 0, 0, 0]
//Fill an array with a string
kofi.fill(3, "abc"); // -> ["abc", "abc", "abc"]kofi.format(str, obj)
Replace all handlebars expressions from str with values of obj.
kofi.format('My car is {{ color }}!', { color: 'blue' }); // --> "My car is blue!"kofi.isEmpty(value)
Check if value is an empty object, array or string.
//Empty array
kofi.isEmpty([]); // -> true
kofi.isEmpty([null]); // -> false
//Empty string
kofi.isEmpty(""); // -> true
kofi.isEmpty(" "); // -> false
//Empty object
kofi.isEmpty({}); // -> true
kofi.isEmpty({"key": null}); // -> falsekofi.kebabCase(str)
Returns the kebab-case form of the string str.
kofi.kebabCase("hello world"); // -> "hello-world"kofi.keys(obj)
This is just Object.keys.
let obj = {
a: 1,
b: 2,
c: "hello"
};
let keys = kofi.keys(obj); // --> keys = ["a", "b", "c"]kofi.max(array)
Returns the maximum value in array.
kofi.max([1, 2, 3, 4, 5]); // -> 5kofi.min(array)
Returns the minimum value in array.
kofi.min([1, 2, 3, 4, 5]); // -> 1kofi.omit(obj, keys)
Creates an object with all the keys of obj that are not in keys.
kofi.omit({a: 1, b: 2, c: 3}, ["b"]); // -> {a: 1, c: 3}kofi.pad(num, length, chars)
Pad a number num adding zeros on the left side if it has less digits than length. You can also specify the characters used for padding.
kofi.pad(1234, 5); // -> "01234"
kofi.pad(1234, 3); // -> "1234"
kofi.pad(1234, 6, "-"); // -> "--1234"kofi.random(min, max)
Returns a random number between min and max (not included). If this functions is called only with one argumet, it returns a random number between 0 and that number.
kofi.random(0, 5); // -> 3.7561160836655425kofi.range(start, end[, step])
Returns a new array with values starting in start to end (included). You can specify the distance between each number in the sequence by providing a step value. Default step value is 1.
kofi.range(0, 5); // -> [0, 1, 2, 3, 4, 5]
kofi.range(0, 4, 2); // -> [0, 2, 4] kofi.repeat(str, n)
Repeats a string n times.
kofi.repeat("x", 5); // -> "xxxxx"kofi.sign(num)
Returns the sign of num.
kofi.sign(-45); // -> -1
kofi.sign(62); // -> 1kofi.snakeCase(str)
Returns the snake-case form of the string str.
kofi.snakeCase("hello world"); // -> "hello_world"kofi.timer(time, fn)
This is just setInterval but with the arguments reverted (first the delay time in ms and then the callback fn function).
let counter = 0;
kofi.timer(1000, function () {
counter = counter + 1;
console.log(counter);
});kofi.timestamp(pattern)
Returns a formatted timestamp. The pattern argument is a string where the following matches will be replaced:
YYYY: replaced with the current full year.MM: replaced with the current month.DD: replaced with the current day.hh: replaced with the current hours.mm: replaced with the current minutes.ss: replaced with the current seconds.
kofi.timestamp("Current year: YYYY")
// -> "Current year: 2018"kofi.truncate(str, opt)
Truncates the provided str text if is longer than a provided length. The opt argument is an object with the following entries:
length: (mandatory) anumberwith the maximum length ofstr.separator: astringused to truncate the stringstr.omission: thestringto be used to represent clipped text. Default is"...". This text is added to the returned string, so the ammount of text displayed fromstrwill be decreased.
truncate("Lorem ipsum dolor sit amet", {length: 11})
// -> "Lorem ip..."
truncate("Lorem ipsum dolor sit amet", {length: 11, omission: ""})
// -> "Lorem ipsum"
truncate("Lorem ipsum dolor sit amet", {length: 15, separator: " "});
// -> "Lorem ipsum..."kofi.uniqueId()
Generates a unique random string of 15 characters.
kofi.uniqueId(); // -> str = "wv1ufiqj5e6xd3k"kofi.values(obj)
Returns an array of a given object's own enumerable property values. It's a ponyfill of the Object.values method.
let obj = {
a: 1,
b: 2,
c: "hello"
};
let values = kofi.values(obj); // -> values = [1, 2, "hello"]License
Under the MIT LICENSE.