@haensl/services
Version:
Assorted JavaScript services.
144 lines (135 loc) • 3.98 kB
JavaScript
// https://github.com/haensl/services#readme v1.5.3 Copyright 2025 HP Dietz <h.p.dietz@gmail.com>
;
Object.defineProperty(exports, '__esModule', { value: true });
const className = (states, basename, separator = '--') => `${basename} ${Object.keys(states).filter(state => states[state]).map(state => `${basename}${separator}${state}`).join(' ')}`.trim();
const setInputValue = (input, value) => {
Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value').set.call(input, value);
input.dispatchEvent(new Event('input', {
bubbles: true
}));
};
var _component = {
className,
setInputValue
};
/**
* Attaches HTTP Response meta data to an error.
*
* @param error Error: The error to attach metainformation to.
* @param response Response: HTTP response data to extrapolate.
*
* @return The updated Error object.
*/
const attachResponseToError = async (response, error) => {
if (response) {
error.response = {
headers: {},
status: response.status,
statusText: response.statusText,
body: typeof response.text === 'function' ? await response.text() : undefined,
...error.response
};
try {
for (const entry of response.headers.entries()) {
error.response.headers[entry[0]] = entry[1];
}
} catch (error) {
error.response.headers = JSON.parse(JSON.stringify(response.headers));
}
}
return error;
};
var _error = {
attachResponseToError
};
const rand = ({
min = 0,
max = 1
} = {}) => Math.random() * (max - min) + min;
const randInt = ({
min,
max
}) => Math.floor(Math.random() * (max + 1 - min) + min);
var _numbers = {
rand,
randInt
};
const hasWindow = (() => {
try {
return typeof window === 'object' && window !== null;
} catch {
return false;
}
})();
const hasDocument = (() => {
try {
return typeof document === 'object' && document !== null;
} catch {
return false;
}
})();
const hasDocumentElement = hasDocument && typeof document.documentElement === 'object' && document.documentElement !== null;
const hasSessionStorage = hasWindow && typeof window.sessionStorage === 'object' && window.sessionStorage !== null && typeof window.sessionStorage.setItem === 'function' && typeof window.sessionStorage.getItem === 'function';
const hasLocalStorage = hasWindow && typeof window.localStorage === 'object' && window.localStorage !== null && typeof window.localStorage.setItem === 'function' && typeof window.localStorage.getItem === 'function';
const scrollPosition = () => {
if (hasWindow && typeof window.scrollX === 'number' && !Number.isNaN(window.scrollX)) {
return {
x: window.scrollX,
y: window.scrollY
};
} else if (hasDocumentElement && typeof document.documentElement.scrollLeft === 'number' && !Number.isNaN(document.documentElement.scrollLeft)) {
return {
x: document.documentElement.scrollLeft,
y: document.documentElement.scrollTop
};
}
return null;
};
var _platform = {
hasDocument,
hasDocumentElement,
hasLocalStorage,
hasSessionStorage,
hasWindow,
scrollPosition
};
const debounce = function (fun, waitMs, immediate) {
let timeout;
return function (...args) {
const context = this;
const later = () => {
timeout = null;
if (!immediate) {
fun.apply(context, args);
}
};
const callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, waitMs);
if (callNow) {
fun.apply(context, args);
}
};
};
var _throttle = {
debounce
};
const component = _component;
const error = _error;
const numbers = _numbers;
const platform = _platform;
const throttle = _throttle;
var index = {
component,
error,
numbers,
platform,
throttle
};
exports.component = component;
exports.default = index;
exports.error = error;
exports.numbers = numbers;
exports.platform = platform;
exports.throttle = throttle;
//# sourceMappingURL=services.cjs.js.map