Choices/src/scripts/lib/utils.js

153 lines
3.6 KiB
JavaScript
Raw Normal View History

export const getRandomNumber = (min, max) =>
Math.floor(Math.random() * (max - min) + min);
2018-05-28 17:22:22 +02:00
export const generateChars = length => {
2017-08-16 10:47:22 +02:00
let chars = '';
2017-08-16 10:47:22 +02:00
for (let i = 0; i < length; i++) {
const randomChar = getRandomNumber(0, 36);
chars += randomChar.toString(36);
}
return chars;
};
export const generateId = (element, prefix) => {
2018-05-28 16:50:16 +02:00
let id =
element.id ||
(element.name && `${element.name}-${generateChars(2)}`) ||
generateChars(4);
id = id.replace(/(:|\.|\[|\]|,)/g, '');
2018-05-28 18:56:36 +02:00
id = `${prefix}-${id}`;
return id;
};
export const getType = obj => Object.prototype.toString.call(obj).slice(8, -1);
2016-04-04 22:44:32 +02:00
export const isType = (type, obj) =>
obj !== undefined && obj !== null && getType(obj) === type;
2016-04-04 22:44:32 +02:00
export const wrap = (element, wrapper = document.createElement('div')) => {
if (element.nextSibling) {
element.parentNode.insertBefore(wrapper, element.nextSibling);
} else {
element.parentNode.appendChild(wrapper);
}
return wrapper.appendChild(element);
2016-03-16 21:24:11 +01:00
};
/**
* @param {HTMLElement} el
* @param {string} attr
*/
export const findAncestorByAttrName = (el, attr) => el.closest(`[${attr}]`);
2017-03-12 14:17:46 +01:00
export const getAdjacentEl = (startEl, className, direction = 1) => {
if (!startEl || !className) {
return;
}
const parent = startEl.parentNode.parentNode;
const children = Array.from(parent.querySelectorAll(className));
const startPos = children.indexOf(startEl);
const operatorDirection = direction > 0 ? 1 : -1;
2016-08-14 23:14:37 +02:00
return children[startPos + operatorDirection];
};
export const isScrolledIntoView = (el, parent, direction = 1) => {
if (!el) {
return;
}
let isVisible;
if (direction > 0) {
// In view from bottom
2018-05-28 16:50:16 +02:00
isVisible =
parent.scrollTop + parent.offsetHeight >= el.offsetTop + el.offsetHeight;
} else {
// In view from top
isVisible = el.offsetTop >= parent.scrollTop;
}
2016-08-14 23:14:37 +02:00
return isVisible;
2016-08-14 23:14:37 +02:00
};
2016-05-02 16:29:05 +02:00
export const sanitise = value => {
if (typeof value !== 'string') {
return value;
}
return value
2018-05-28 16:50:16 +02:00
.replace(/&/g, '&amp;')
.replace(/>/g, '&rt;')
.replace(/</g, '&lt;')
.replace(/"/g, '&quot;');
};
2016-03-16 21:24:11 +01:00
export const strToEl = (() => {
2017-08-16 10:47:22 +02:00
const tmpEl = document.createElement('div');
return str => {
2017-08-16 10:47:22 +02:00
const cleanedInput = str.trim();
tmpEl.innerHTML = cleanedInput;
const firldChild = tmpEl.children[0];
while (tmpEl.firstChild) {
tmpEl.removeChild(tmpEl.firstChild);
}
return firldChild;
};
2018-05-28 16:50:16 +02:00
})();
2016-04-07 20:44:16 +02:00
2019-10-29 18:09:49 +01:00
export const sortByAlpha =
/**
* @param {{ label?: string, value: string }} a
* @param {{ label?: string, value: string }} b
* @returns {number}
*/
({ value, label = value }, { value: value2, label: label2 = value2 }) =>
label.localeCompare(label2, [], {
sensitivity: 'base',
ignorePunctuation: true,
numeric: true,
});
2017-08-16 10:47:22 +02:00
export const sortByScore = (a, b) => a.score - b.score;
2017-01-01 16:32:09 +01:00
export const dispatchEvent = (element, type, customArgs = null) => {
2017-12-20 13:38:16 +01:00
const event = new CustomEvent(type, {
2017-01-01 16:32:09 +01:00
detail: customArgs,
bubbles: true,
2017-08-16 10:47:22 +02:00
cancelable: true,
2017-01-01 16:32:09 +01:00
});
return element.dispatchEvent(event);
};
2017-08-10 12:57:17 +02:00
export const isIE11 = userAgent =>
!!(userAgent.match(/Trident/) && userAgent.match(/rv[ :]11/));
2018-05-27 18:22:58 +02:00
2018-05-28 18:56:36 +02:00
export const existsInArray = (array, value, key = 'value') =>
2018-05-28 17:22:22 +02:00
array.some(item => {
if (typeof value === 'string') {
2018-05-28 18:56:36 +02:00
return item[key] === value.trim();
2018-05-27 18:22:58 +02:00
}
2018-05-28 18:56:36 +02:00
return item[key] === value;
2018-05-28 16:50:16 +02:00
});
2018-05-28 17:22:22 +02:00
export const cloneObject = obj => JSON.parse(JSON.stringify(obj));
export const diff = (a, b) => {
const aKeys = Object.keys(a).sort();
const bKeys = Object.keys(b).sort();
return aKeys.filter(i => bKeys.indexOf(i) < 0);
};