Utils

During the framework writing, we have accumulated many functions that you could also use.

import utils from 'akili/src/services/utils';
console.log(utils === Akili.services.utils); // true

Also you have access to utils object in the scope.

<my-component class="${ this.__utils.class({active: true}) }"></my-component>

utils.class(object) string

  • object Object - object with classes and states

Create html classes string from an object

let classes = {
  active: true,
  red: 1,
  disabled: false
}

console.log(utils.class(classes));
// 'active red'

utils.style(object) string

  • object Object - object with styles and values

Create html styles string from an object

let styles = {
  background: false,
  color: 'red',
  borderColor: 'black'
}

console.log(utils.style(styles));
// 'color:red;border-color:black'

utils.filter(array, handler, keys) Array

  • array Array - array to filter
  • handler string | RegExp | function - condition for filtering
  • [keys=[]] string[] | string - array item property keys

Filter an array

console.log(utils.filter([1, 2, 3, 11], item => item.match('1')));
// [1, 11]
console.log(utils.filter([{x:1}, {x: 2}, {x: 3}, {x: 11}], '1', ['x']));
console.log(utils.filter([{x:1}, {x: 2}, {x: 3}, {x: 11}], '1', 'x'));
// [{x: 1}, {x: 11}]
console.log(utils.filter([{x: {y: 1}}, {x: {y: 2}}], '1', ['x', 'y']));
// [{x: {y: 1}}]

utils.sort(array, keys, direction) Array

  • array Array - array to filter
  • [keys=true] boolean | Array[] | string[] | string - condition for sorting
  • [direction=[]] boolean | boolean[] - direction of the sorting

Sort an array

console.log(utils.sort([3, 2, 1], true));
// [1, 2, 3]
console.log(utils.sort([1, 2, 3], false));
// [3, 2, 1]
console.log(utils.sort([{x: 3}, {x: 2}, {x: 1}], [['x']], [true]));
console.log(utils.sort([{x: 3}, {x: 2}, {x: 1}], ['x'], true));
console.log(utils.sort([{x: 3}, {x: 2}, {x: 1}], 'x'));
// [{x: 1}, {x: 2}, {x: 3}]
console.log(utils.sort([{x: 1}, {x: 2}, {x: 3}], [['x']], [false]));
console.log(utils.sort([{x: 1}, {x: 2}, {x: 3}], ['x'], false));
console.log(utils.sort([{x: 1}, {x: 2}, {x: 3}], 'x', false));
// [{x: 3}, {x: 2}, {x: 1}]
console.log(utils.sort([{x: 2, y: 2}, {x: 2, y: 1}, {x: 2, y: 3}], [['x'], ['y']], [true, true]));
// [{x: 1, y: 3}, {x: 2, y: 1}, {x: 2, y: 2}]
console.log(utils.sort([{x: {y: 2}}, {x: {y: 1}}], [['x', 'y']], [true]));
// [{x: {y: 1}}, {x: {y: 2}}]

utils.isPlainObject(value) boolean

  • value * - value to check

Check an object is plain (has constructor Object or Array)

utils.copy(value, nested, enumerable) boolean

  • value * - value to copy
  • [nested=true] boolean - nested copying for an object
  • [enumerable=false] boolean - including enumerable properties

Copy a value

utils.compare(a, b) boolean

  • a * - first value
  • b * - second value

Compare two values. Objects will be compared for identity with all properties.

utils.encodeHtmlEntities(string) string

  • string string - value to convert

Convert a string to HTML entities

console.log(utils.encodeHtmlEntities('you & me'));
// 'you &amp; me'

utils.decodeHtmlEntities(string) string

  • string string - value to convert

Decode HTML entities

console.log(utils.decodeHtmlEntities('you &amp; me'));
// 'you & me'

utils.toCamelCase(string) string

  • string string - value to convert

Convert a string to camel case

utils.toDashCase(string) string

  • string string - value to convert

Convert a string to dash case

utils.getPropertyByKeys(keys, object) *

  • keys string[] - keys of an object
  • object Object - object to find

Get nested object property by array keys

console.log(utils.getPropertyByKeys(['x', 'y'], {x: {y: 5}}));
// 5

utils.hasPropertyByKeys(keys, object) boolean

  • keys string[] - keys of an object
  • object Object - object to check

Check nested objects tree has property or not

console.log(utils.hasPropertyByKeys(['x', 'y'], {x: {y: 5}}));
// true

utils.setPropertyByKeys(keys, object, fn) *

  • keys string[] - keys of an object
  • object Object - object to set
  • fn function - function to set value

Set nested objects tree property

let obj = {x: {y: 5}};

console.log(utils.setPropertyByKeys(['x', 'y'], obj, (last, val) => last? 6: (val || {})));
// {y: 6}
console.log(obj.x.y);
// 6

utils.deletePropertyByKeys(keys, object, fn) boolean

  • keys string[] - keys of an object
  • object Object - object to delete
  • [fn] function - function to check

Delete property from nested objects tree

let obj = {x: {y: 5}};

console.log(utils.deletePropertyByKeys(['x', 'y'], obj));
console.log(utils.deletePropertyByKeys(['x', 'y'], obj, val => val != 5));
// 5
console.log(obj.x.hasOwnProperty('y'));
// false

utils.createRandomString(length, fn) string

  • [length=16] integer - length of the string
  • [fn] function - Function to check for uniqueness. If you return true, the line will be regenerated and so on until the function returns a negative value.

Generate random string

There are a lot of other, but less popular functions in utils, you can see them in the source code.