UNPKG

@pafa/shared

Version:

shared utils of paf

433 lines (423 loc) 12.1 kB
"use strict"; var __defProp = Object.defineProperty; var __defProps = Object.defineProperties; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropDescs = Object.getOwnPropertyDescriptors; var __getOwnPropNames = Object.getOwnPropertyNames; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { bigCamelize: () => bigCamelize, call: () => call, callInterceptor: () => callInterceptor, camelize: () => camelize, cancelAnimationFrame: () => cancelAnimationFrame, clamp: () => clamp, clampArrayRange: () => clampArrayRange, classes: () => classes, createNamespaceFn: () => createNamespaceFn, debounce: () => debounce, doubleRaf: () => doubleRaf, find: () => find, getGlobalThis: () => getGlobalThis, getRect: () => getRect, getScrollLeft: () => getScrollLeft, getScrollTop: () => getScrollTop, getStyle: () => getStyle, hasOwn: () => hasOwn, inBrowser: () => inBrowser, inMobile: () => inMobile, inViewport: () => inViewport, isArray: () => isArray, isBoolean: () => isBoolean, isDef: () => isDef, isEmpty: () => isEmpty, isFunction: () => isFunction, isNullish: () => isNullish, isNumber: () => isNumber, isNumeric: () => isNumeric, isObject: () => isObject, isPlainObject: () => isPlainObject, isPromise: () => isPromise, isString: () => isString, isURL: () => isURL, isWindow: () => isWindow, kebabCase: () => kebabCase, mergeWith: () => mergeWith, noop: () => noop, normalizeToArray: () => normalizeToArray, preventDefault: () => preventDefault, raf: () => raf, removeArrayBlank: () => removeArrayBlank, removeItem: () => removeItem, requestAnimationFrame: () => requestAnimationFrame, slash: () => slash, supportTouch: () => supportTouch, throttle: () => throttle, toDataURL: () => toDataURL, toNumber: () => toNumber, toggleItem: () => toggleItem, uniq: () => uniq }); module.exports = __toCommonJS(index_exports); // src/is.ts function noop() { } var isString = (val) => typeof val === "string"; var isBoolean = (val) => typeof val === "boolean"; var isNumber = (val) => typeof val === "number"; var isNumeric = (val) => isNumber(val) || isString(val) && /^[-+]?\d+$/.test(val); var isPlainObject = (val) => Object.prototype.toString.call(val) === "[object Object]"; var isObject = (val) => typeof val === "object" && val !== null; var isFunction = (val) => typeof val === "function"; var isPromise = (val) => isObject(val) && isFunction(val.then) && isFunction(val.catch); var isArray = (val) => Array.isArray(val); var isURL = (val) => { if (!val) { return false; } return /^(http)|(\.*\/)/.test(val); }; var isEmpty = (val) => val === void 0 || val === null || val === "" || isArray(val) && !val.length; var isWindow = (val) => val === window; var supportTouch = () => inBrowser() && "ontouchstart" in window; var inBrowser = () => typeof window !== "undefined"; var inMobile = () => inBrowser() && /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent); var { hasOwnProperty } = Object.prototype; var hasOwn = (val, key) => hasOwnProperty.call(val, key); var isNullish = (value) => { return value === null || value === void 0; }; var isDef = (val) => val !== void 0 && val !== null; // src/array.ts var uniq = (arr) => [...new Set(arr)]; var normalizeToArray = (value) => isArray(value) ? value : [value]; var removeItem = (arr, item) => { if (arr.length) { const index = arr.indexOf(item); if (index > -1) { return arr.splice(index, 1); } } }; var toggleItem = (arr, item) => { arr.includes(item) ? removeItem(arr, item) : arr.push(item); return arr; }; var removeArrayBlank = (arr) => arr.filter((item) => item != null); var find = (arr, callback, from = "start") => { let i = from === "start" ? 0 : arr.length - 1; while (arr.length > 0 && i >= 0 && i <= arr.length - 1) { const flag = callback(arr[i], i, arr); if (flag) { return [arr[i], i]; } from === "start" ? i++ : i--; } return [null, -1]; }; var classes = (...classes2) => classes2.map((className) => { if (isArray(className)) { const [condition, truthy, falsy = null] = className; return condition ? truthy : falsy; } return className; }); // src/elements.ts var getGlobalThis = () => { if (typeof globalThis !== "undefined") { return globalThis; } if (inBrowser()) { return window; } return typeof global !== "undefined" ? global : self; }; var requestAnimationFrame = (fn) => { const globalThis2 = getGlobalThis(); return globalThis2.requestAnimationFrame ? globalThis2.requestAnimationFrame(fn) : globalThis2.setTimeout(fn); }; var cancelAnimationFrame = (handle) => { const globalThis2 = getGlobalThis(); globalThis2.cancelAnimationFrame ? globalThis2.cancelAnimationFrame(handle) : globalThis2.clearTimeout(handle); }; var raf = () => new Promise((resolve) => { requestAnimationFrame(resolve); }); var doubleRaf = () => new Promise((resolve) => { requestAnimationFrame(() => { requestAnimationFrame(resolve); }); }); var getStyle = (element) => window.getComputedStyle(element); var getRect = (element) => { if (isWindow(element)) { const width = element.innerWidth; const height = element.innerHeight; const rect = { x: 0, y: 0, top: 0, left: 0, right: width, bottom: height, width, height }; return __spreadProps(__spreadValues({}, rect), { toJSON: () => rect }); } return element.getBoundingClientRect(); }; var inViewport = (element) => { const { top, bottom, left, right } = getRect(element); const { width, height } = getRect(window); const xInViewport = left <= width && right >= 0; const yInViewport = top <= height && bottom >= 0; return xInViewport && yInViewport; }; var toDataURL = (file) => new Promise((resolve) => { const fileReader = new FileReader(); fileReader.onload = () => { resolve(fileReader.result); }; fileReader.readAsDataURL(file); }); var preventDefault = (event) => { if (event.cancelable === false) { return; } event.preventDefault(); }; var getScrollTop = (element) => { const top = "scrollTop" in element ? element.scrollTop : element.scrollY; return Math.max(top, 0); }; var getScrollLeft = (element) => { const left = "scrollLeft" in element ? element.scrollLeft : element.scrollX; return Math.max(left, 0); }; // src/function.ts var debounce = (fn, delay = 0) => { let timer; return function(...args) { if (timer) { clearTimeout(timer); } timer = setTimeout(() => { fn.apply(this, args); }, delay); }; }; var throttle = (fn, delay = 200) => { let timer; let start = 0; return function loop(...args) { const now = Date.now(); const elapsed = now - start; if (!start) { start = now; } if (timer) { clearTimeout(timer); } if (elapsed >= delay) { fn.apply(this, args); start = now; } else { timer = setTimeout(() => { loop.apply(this, args); }, delay - elapsed); } }; }; function call(fn, ...args) { if (isArray(fn)) { return fn.map((f) => f(...args)); } if (fn) { return fn(...args); } } // src/number.ts var toNumber = (val) => { if (val === null || val === void 0) return 0; if (isString(val)) { val = parseFloat(val); val = Number.isNaN(val) ? 0 : val; return val; } if (isBoolean(val)) return Number(val); return val; }; var clamp = (num, min, max) => Math.min(max, Math.max(min, num)); var clampArrayRange = (index, arr) => clamp(index, 0, arr.length - 1); // src/string.ts var bigCamelize = (s) => camelize(s).replace(s.charAt(0), s.charAt(0).toUpperCase()); var camelize = (s) => s.replace(/-(\w)/g, (_, p) => p.toUpperCase()); var kebabCase = (s) => { const ret = s.replace(/([A-Z])/g, " $1").trim(); return ret.split(" ").join("-").toLowerCase(); }; var slash = (path) => { const isExtendedLengthPath = path.startsWith("\\\\?\\"); if (isExtendedLengthPath) { return path; } return path.replace(/\\/g, "/"); }; function createNamespaceFn(namespace) { return (name) => { const componentName = `${namespace}-${name}`; const createBEM = (suffix) => { if (!suffix) { return componentName; } if (suffix[0] === "$") { return suffix.replace("$", namespace); } return suffix.startsWith("--") ? `${componentName}${suffix}` : `${componentName}__${suffix}`; }; return { name: bigCamelize(componentName), n: createBEM, classes }; }; } // src/merge.ts function mergeWith(object, source, customizer) { const isObject2 = (obj) => obj !== null && typeof obj === "object"; function baseMerge(target, src) { for (const key in src) { if (Object.prototype.hasOwnProperty.call(src, key)) { const srcValue = src[key]; const targetValue = target[key]; const customResult = customizer(targetValue, srcValue, key, object, source); if (customResult !== void 0) { target[key] = customResult; } else if (isObject2(srcValue)) { if (isObject2(targetValue)) { target[key] = baseMerge(targetValue, srcValue); } else { target[key] = baseMerge(isArray(srcValue) ? [] : {}, srcValue); } } else { target[key] = srcValue; } } } return target; } return baseMerge(object, source); } // src/interceptor.ts function callInterceptor(interceptor, { args = [], done, canceled, error }) { if (interceptor) { const returnVal = interceptor.apply(null, args); if (isPromise(returnVal)) { returnVal.then((value) => { if (value) { done(); } else if (canceled) { canceled(); } }).catch(error || noop); } else if (returnVal) { done(); } else if (canceled) { canceled(); } } else { done(); } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { bigCamelize, call, callInterceptor, camelize, cancelAnimationFrame, clamp, clampArrayRange, classes, createNamespaceFn, debounce, doubleRaf, find, getGlobalThis, getRect, getScrollLeft, getScrollTop, getStyle, hasOwn, inBrowser, inMobile, inViewport, isArray, isBoolean, isDef, isEmpty, isFunction, isNullish, isNumber, isNumeric, isObject, isPlainObject, isPromise, isString, isURL, isWindow, kebabCase, mergeWith, noop, normalizeToArray, preventDefault, raf, removeArrayBlank, removeItem, requestAnimationFrame, slash, supportTouch, throttle, toDataURL, toNumber, toggleItem, uniq });