snappymail/dev/Common/Utils.js

32 lines
1.2 KiB
JavaScript
Raw Normal View History

export const
isArray = Array.isArray,
2021-07-22 03:34:17 +08:00
arrayLength = array => isArray(array) && array.length,
2021-08-20 21:40:07 +08:00
isFunction = v => typeof v === 'function',
pString = value => null != value ? '' + value : '',
forEachObjectValue = (obj, fn) => Object.values(obj).forEach(fn),
2021-12-01 20:54:35 +08:00
forEachObjectEntry = (obj, fn) => Object.entries(obj).forEach(([key, value]) => fn(key, value)),
2021-08-20 21:40:07 +08:00
pInt = (value, defaultValue = 0) => {
value = parseInt(value, 10);
2024-04-08 23:22:27 +08:00
return isFinite(value) ? value : defaultValue;
2021-08-20 21:40:07 +08:00
},
defaultOptionsAfterRender = (domItem, item) =>
2022-09-02 17:52:07 +08:00
item && undefined !== item.disabled && domItem?.classList.toggle('disabled', domItem.disabled = item.disabled),
2016-06-07 05:57:52 +08:00
2021-12-07 19:40:55 +08:00
// unescape(encodeURIComponent()) makes the UTF-16 DOMString to an UTF-8 string
2023-02-16 00:29:47 +08:00
b64Encode = data => btoa(unescape(encodeURIComponent(data))),
2021-12-07 19:40:55 +08:00
/* // Without deprecated 'unescape':
2023-02-16 00:29:47 +08:00
b64Encode = data => btoa(encodeURIComponent(data).replace(
2021-12-07 19:40:55 +08:00
/%([0-9A-F]{2})/g, (match, p1) => String.fromCharCode('0x' + p1)
2022-02-24 19:43:44 +08:00
)),
2021-12-07 19:40:55 +08:00
*/
2023-02-16 00:29:47 +08:00
b64EncodeJSON = data => b64Encode(JSON.stringify(data)),
2021-12-07 19:40:55 +08:00
b64EncodeJSONSafe = data => b64EncodeJSON(data).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, ''),
getKeyByValue = (o, v) => Object.keys(o).find(key => o[key] === v);