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 expression.

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

utils.class(object) string

  • object Object - object with classes and states
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
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
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']));
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}}]
console.log(utils.filter([{x: 1, y: 2}, {x: 3, y: 3}, {x: 2, y: 1}], '1', ['x', 'y']));
console.log(utils.filter([{x: 1, y: 2}, {x: 3, y: 3}, {x: 2, y: 1}], '1', [['x'], ['y']]));
// [{x: 1, y: 2}, {x: 2, 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
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.split(str, del, exclude) string[]

  • str string - value to split
  • [del] string|RegExp - delimiter
  • [exclude] string[] - list of excluded boundaries to spilt
console.log(utils.split("Hello World", " "));
// ["Hello", "World"]
console.log(utils.split('x = 5; y = "1;2;3"', ";", ['"']));
// ['x = 5', ' y = "1;2;3"']

utils.includeKeys(obj, keys) object

  • obj object - actual object
  • keys string[] - necessary keys
console.log(utils.includeKeys({x: 1, y: 1, z: 1}, ['x', 'z']));
// {x: 1, z: 1}

utils.excludeKeys(obj, keys) object

  • obj object - actual object
  • keys string[] - necessary keys
console.log(utils.excludeKeys({x: 1, y: 1, z: 1}, ['y']));
// {x: 1, z: 1}

utils.isPlainObject(value) boolean

  • value * - value to check

utils.copy(value, options) boolean

  • value * - value to copy
  • [options={ nested: true, enumerable: true }] object - options

utils.compare(a, b, options) boolean

  • a * - first value
  • b * - second value
  • [options={ enumerable: true }] object - options

utils.encodeHtmlEntities(string) string

  • string string - value to convert
console.log(utils.encodeHtmlEntities('you & me'));
// 'you &amp; me'

utils.decodeHtmlEntities(string) string

  • string string - value to convert
console.log(utils.decodeHtmlEntities('you &amp; me'));
// 'you & me'

utils.toCamelCase(string) string

  • string string - value to convert

utils.toDashCase(string) string

  • string string - value to convert

utils.getPropertyByKeys(keys, object) *

  • keys string[] - keys of an object
  • object Object - object to find
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 it
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
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 decide whether to delete it or not
let obj = {x: {y: 5}};

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

utils.deletePropertyByKeys(['x', 'y'], obj);
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.

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