UNPKG

devextreme

Version:

HTML5 JavaScript Component Suite for Responsive Web Development

273 lines (266 loc) • 7.99 kB
/** * DevExtreme (esm/data/utils.js) * Version: 21.1.4 * Build date: Mon Jun 21 2021 * * Copyright (c) 2012 - 2021 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ import { isFunction } from "../core/utils/type"; import domAdapter from "../core/dom_adapter"; import readyCallbacks from "../core/utils/ready_callbacks"; import { getWindow } from "../core/utils/window"; import { map } from "../core/utils/iterator"; import { Deferred } from "../core/utils/deferred"; import { equalByValue } from "../core/utils/common"; var ready = readyCallbacks.add; var XHR_ERROR_UNLOAD = "DEVEXTREME_XHR_ERROR_UNLOAD"; var normalizeBinaryCriterion = function(crit) { return [crit[0], crit.length < 3 ? "=" : String(crit[1]).toLowerCase(), crit.length < 2 ? true : crit[crit.length - 1]] }; var normalizeSortingInfo = function(info) { if (!Array.isArray(info)) { info = [info] } return map(info, (function(i) { var result = { selector: isFunction(i) || "string" === typeof i ? i : i.getter || i.field || i.selector, desc: !!(i.desc || "d" === String(i.dir).charAt(0).toLowerCase()) }; if (i.compare) { result.compare = i.compare } return result })) }; var errorMessageFromXhr = function() { var textStatusMessages = { timeout: "Network connection timeout", error: "Unspecified network error", parsererror: "Unexpected server response" }; var unloading; ready((function() { var window = getWindow(); domAdapter.listen(window, "beforeunload", (function() { unloading = true })) })); return function(xhr, textStatus) { if (unloading) { return XHR_ERROR_UNLOAD } if (xhr.status < 400) { return function(textStatus) { var result = textStatusMessages[textStatus]; if (!result) { return textStatus } return result }(textStatus) } return xhr.statusText } }(); var aggregators = { count: { seed: 0, step: function(count) { return 1 + count } }, sum: { seed: 0, step: function(sum, item) { return sum + item } }, min: { step: function(min, item) { return item < min ? item : min } }, max: { step: function(max, item) { return item > max ? item : max } }, avg: { seed: [0, 0], step: function(pair, value) { return [pair[0] + value, pair[1] + 1] }, finalize: function(pair) { return pair[1] ? pair[0] / pair[1] : NaN } } }; var processRequestResultLock = function() { var lockCount = 0; var lockDeferred; return { obtain: function() { if (0 === lockCount) { lockDeferred = new Deferred } lockCount++ }, release: function() { lockCount--; if (lockCount < 1) { lockDeferred.resolve() } }, promise: function() { var deferred = 0 === lockCount ? (new Deferred).resolve() : lockDeferred; return deferred.promise() }, reset: function() { lockCount = 0; if (lockDeferred) { lockDeferred.resolve() } } } }(); function isDisjunctiveOperator(condition) { return /^(or|\|\||\|)$/i.test(condition) } function isConjunctiveOperator(condition) { return /^(and|&&|&)$/i.test(condition) } var keysEqual = function(keyExpr, key1, key2) { if (Array.isArray(keyExpr)) { var names = map(key1, (function(v, k) { return k })); var name; for (var i = 0; i < names.length; i++) { name = names[i]; if (!equalByValue(key1[name], key2[name], 0, false)) { return false } } return true } return equalByValue(key1, key2, 0, false) }; var BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; var base64_encode = function(input) { if (!Array.isArray(input)) { input = stringToByteArray(String(input)) } var result = ""; function getBase64Char(index) { return BASE64_CHARS.charAt(index) } for (var i = 0; i < input.length; i += 3) { var octet1 = input[i]; var octet2 = input[i + 1]; var octet3 = input[i + 2]; result += map([octet1 >> 2, (3 & octet1) << 4 | octet2 >> 4, isNaN(octet2) ? 64 : (15 & octet2) << 2 | octet3 >> 6, isNaN(octet3) ? 64 : 63 & octet3], getBase64Char).join("") } return result }; function stringToByteArray(str) { var bytes = []; var code; var i; for (i = 0; i < str.length; i++) { code = str.charCodeAt(i); if (code < 128) { bytes.push(code) } else if (code < 2048) { bytes.push(192 + (code >> 6), 128 + (63 & code)) } else if (code < 65536) { bytes.push(224 + (code >> 12), 128 + (code >> 6 & 63), 128 + (63 & code)) } else if (code < 2097152) { bytes.push(240 + (code >> 18), 128 + (code >> 12 & 63), 128 + (code >> 6 & 63), 128 + (63 & code)) } } return bytes } var isUnaryOperation = function(crit) { return "!" === crit[0] && Array.isArray(crit[1]) }; var isGroupOperator = function(value) { return "and" === value || "or" === value }; var isGroupCriterion = function(crit) { var first = crit[0]; var second = crit[1]; if (Array.isArray(first)) { return true } if (isFunction(first)) { if (Array.isArray(second) || isFunction(second) || isGroupOperator(second)) { return true } } return false }; var trivialPromise = function() { var d = new Deferred; return d.resolve.apply(d, arguments).promise() }; var rejectedPromise = function() { var d = new Deferred; return d.reject.apply(d, arguments).promise() }; function throttle(func, timeout) { var timeoutId; var lastArgs; return function() { lastArgs = arguments; if (!timeoutId) { timeoutId = setTimeout(() => { timeoutId = void 0; if (lastArgs) { func.call(this, lastArgs) } }, isFunction(timeout) ? timeout() : timeout) } return timeoutId } } function throttleChanges(func, timeout) { var cache = []; var throttled = throttle((function() { func.call(this, cache); cache = [] }), timeout); return function(changes) { if (Array.isArray(changes)) { cache.push(...changes) } return throttled.call(this, cache) } } var utils = { XHR_ERROR_UNLOAD: XHR_ERROR_UNLOAD, normalizeBinaryCriterion: normalizeBinaryCriterion, normalizeSortingInfo: normalizeSortingInfo, errorMessageFromXhr: errorMessageFromXhr, aggregators: aggregators, keysEqual: keysEqual, throttleChanges: throttleChanges, trivialPromise: trivialPromise, rejectedPromise: rejectedPromise, isDisjunctiveOperator: isDisjunctiveOperator, isConjunctiveOperator: isConjunctiveOperator, processRequestResultLock: processRequestResultLock, isUnaryOperation: isUnaryOperation, isGroupCriterion: isGroupCriterion, base64_encode: base64_encode }; export default utils;