UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

264 lines (256 loc) • 9.39 kB
/** * DevExtreme (cjs/__internal/data/utils.js) * Version: 26.1.6 * Build date: Fri Oct 16 2026 * * Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.errorMessageFromXhr = exports.base64_encode = exports.aggregators = exports.XHR_ERROR_UNLOAD = void 0; exports.isConjunctiveOperator = isConjunctiveOperator; exports.isDisjunctiveOperator = isDisjunctiveOperator; exports.rejectedPromise = exports.processRequestResultLock = exports.normalizeSortingInfo = exports.normalizeBinaryCriterion = exports.keysEqual = exports.isUniformEqualsByOr = exports.isUnaryOperation = exports.isGroupCriterion = void 0; exports.throttleChanges = throttleChanges; exports.trivialPromise = void 0; var _dom_adapter = _interopRequireDefault(require("../../core/dom_adapter")); var _common = require("../../core/utils/common"); var _deferred = require("../../core/utils/deferred"); var _iterator = require("../../core/utils/iterator"); var _ready_callbacks = _interopRequireDefault(require("../../core/utils/ready_callbacks")); var _type = require("../../core/utils/type"); var _window = require("../../core/utils/window"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e } } const ready = _ready_callbacks.default.add; const XHR_ERROR_UNLOAD = exports.XHR_ERROR_UNLOAD = "DEVEXTREME_XHR_ERROR_UNLOAD"; const normalizeBinaryCriterion = function(crit) { return [crit[0], crit.length < 3 ? "=" : String(crit[1]).toLowerCase(), crit.length < 2 ? true : crit[crit.length - 1]] }; exports.normalizeBinaryCriterion = normalizeBinaryCriterion; const normalizeSortingInfo = function(info) { const infoArray = Array.isArray(info) ? info : [info]; return infoArray.map(item => { const descriptor = (0, _type.isObject)(item) ? item : {}; const result = { selector: (0, _type.isFunction)(item) || (0, _type.isString)(item) ? item : descriptor.getter || descriptor.field || descriptor.selector, desc: !!(descriptor.desc || "d" === String(descriptor.dir).charAt(0).toLowerCase()) }; if (descriptor.compare) { result.compare = descriptor.compare } return result }) }; exports.normalizeSortingInfo = normalizeSortingInfo; const errorMessageFromXhr = exports.errorMessageFromXhr = function() { const textStatusMessages = { timeout: "Network connection timeout", error: "Unspecified network error", parsererror: "Unexpected server response" }; let unloading = false; ready(() => { const window = (0, _window.getWindow)(); _dom_adapter.default.listen(window, "beforeunload", () => { unloading = true }) }); return function(xhr, textStatus) { if (unloading) { return XHR_ERROR_UNLOAD } const status = (0, _type.isObject)(xhr) && "status" in xhr ? Number(xhr.status) : NaN; if (status < 400) { return function(textStatus) { let result = textStatusMessages[textStatus]; if (!result) { return textStatus } return result }(String(textStatus)) } const statusText = (0, _type.isObject)(xhr) && "statusText" in xhr ? xhr.statusText : void 0; return (0, _type.isString)(statusText) ? statusText : "" } }(); const aggregators = exports.aggregators = { count: { seed: 0, step: count => 1 + count }, sum: { seed: 0, step: (sum, item) => sum + item }, min: { step: (min, item) => item < min ? item : min }, max: { step: (max, item) => item > max ? item : max }, avg: { seed: [0, 0], step: (pair, value) => [pair[0] + value, pair[1] + 1], finalize: pair => pair[1] ? pair[0] / pair[1] : NaN } }; const processRequestResultLock = exports.processRequestResultLock = function() { let lockCount = 0; let lockDeferred; return { obtain: function() { if (0 === lockCount) { lockDeferred = (0, _deferred.Deferred)() } lockCount += 1 }, release: function() { lockCount -= 1; if (lockCount < 1) { var _lockDeferred; null === (_lockDeferred = lockDeferred) || void 0 === _lockDeferred || _lockDeferred.resolve() } }, promise: function() { const deferred = 0 === lockCount || !lockDeferred ? (0, _deferred.Deferred)().resolve() : lockDeferred; return deferred.promise() }, reset: function() { var _lockDeferred2; lockCount = 0; null === (_lockDeferred2 = lockDeferred) || void 0 === _lockDeferred2 || _lockDeferred2.resolve() } } }(); function isDisjunctiveOperator(condition) { return /^(or|\|\||\|)$/i.test(String(condition)) } function isConjunctiveOperator(condition) { return /^(and|&&|&)$/i.test(String(condition)) } const isRecord = value => (0, _type.isObject)(value) || Array.isArray(value); const keysEqual = function(keyExpr, key1, key2) { if (Array.isArray(keyExpr)) { const values1 = isRecord(key1) ? key1 : {}; const values2 = isRecord(key2) ? key2 : {}; const names = (0, _iterator.map)(values1, (value, name) => name); for (const name of names) { if (!(0, _common.equalByValue)(values1[name], values2[name], { strict: false })) { return false } } return true } return (0, _common.equalByValue)(key1, key2, { strict: false }) }; exports.keysEqual = keysEqual; const BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; function stringToByteArray(str) { const bytes = []; for (let i = 0; i < str.length; i += 1) { const 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 } const base64_encode = function(input) { const bytes = Array.isArray(input) ? input : stringToByteArray(String(input)); let result = ""; function getBase64Char(index) { return BASE64_CHARS.charAt(index) } for (let i = 0; i < bytes.length; i += 3) { const octet1 = bytes[i]; const octet2 = bytes[i + 1]; const octet3 = bytes[i + 2]; result += [octet1 >> 2, (3 & octet1) << 4 | octet2 >> 4, isNaN(octet2) ? 64 : (15 & octet2) << 2 | octet3 >> 6, isNaN(octet3) ? 64 : 63 & octet3].map(getBase64Char).join("") } return result }; exports.base64_encode = base64_encode; const isUnaryOperation = function(crit) { return Array.isArray(crit) && "!" === crit[0] && Array.isArray(crit[1]) }; exports.isUnaryOperation = isUnaryOperation; const isGroupOperator = function(value) { return "and" === value || "or" === value }; const isUniformEqualsByOr = function(crit) { if (crit.length > 2 && Array.isArray(crit[0]) && "or" === crit[1] && "string" === typeof crit[0][0] && "=" === crit[0][1]) { const [prop] = crit[0]; return !crit.find((el, i) => i % 2 !== 0 ? "or" !== el : !Array.isArray(el) || 3 !== el.length || el[0] !== prop || "=" !== el[1]) } return false }; exports.isUniformEqualsByOr = isUniformEqualsByOr; const isGroupCriterion = function(crit) { if (!Array.isArray(crit)) { return false } const first = crit[0]; const second = crit[1]; if (Array.isArray(first)) { return true } if ((0, _type.isFunction)(first)) { if (Array.isArray(second) || (0, _type.isFunction)(second) || isGroupOperator(second)) { return true } } return false }; exports.isGroupCriterion = isGroupCriterion; const trivialPromise = function() { const d = (0, _deferred.Deferred)(); return d.resolve(...arguments).promise() }; exports.trivialPromise = trivialPromise; const rejectedPromise = function() { const d = (0, _deferred.Deferred)(); return d.reject(...arguments).promise() }; exports.rejectedPromise = rejectedPromise; function throttle(func, timeout) { let timeoutId; return function() { if (!timeoutId) { timeoutId = setTimeout(() => { timeoutId = void 0; func.call(this) }, (0, _type.isFunction)(timeout) ? timeout() : timeout) } return timeoutId } } function throttleChanges(func, timeout) { let cache = []; const throttled = throttle(function() { func.call(this, cache); cache = [] }, timeout); return function(changes) { if (Array.isArray(changes)) { cache.push(...changes) } return throttled.call(this) } }