JavaScript Utility Functions for Object Manipulation, Event Handling, and Query String Encoding
javascript/n/'use strict/';/nconst _export_sfc = (sfc, props) => {/n const target = sfc.__vccOpts || sfc;/n for (const [key, val] of props) {/n target[key] = val;/n }/n return target;/n};/nfunction makeMap(str, expectsLowerCase) {/n const map = /* @__PURE__ */ Object.create(null);/n const list = str.split(/',/');/n for (let i = 0; i < list.length; i++) {/n map[list[i]] = true;/n }/n return expectsLowerCase ? (val) => !!map[val.toLowerCase()] : (val) => !!map[val];/n}/nfunction normalizeStyle(value) {/n if (isArray(value)) {/n const res = {};/n for (let i = 0; i < value.length; i++) {/n const item = value[i];/n const normalized = isString(item) ? parseStringStyle(item) : normalizeStyle(item);/n if (normalized) {/n for (const key in normalized) {/n res[key] = normalized[key];/n }/n }/n }/n return res;/n } else if (isString(value)) {/n return value;/n } else if (isObject(value)) {/n return value;/n }/n}/nconst listDelimiterRE = /;(?![^(]*/))/g;/nconst propertyDelimiterRE = /:([^]+)/;/nconst styleCommentRE = ////*.*?/*///gs;/nfunction parseStringStyle(cssText) {/n const ret = {};/n cssText.replace(styleCommentRE, /'/').split(listDelimiterRE).forEach((item) => {/n if (item) {/n const tmp = item.split(propertyDelimiterRE);/n tmp.length > 1 && (ret[tmp[0].trim()] = tmp[1].trim());/n }/n });/n return ret;/n}/nfunction normalizeClass(value) {/n let res = /'/';/n if (isString(value)) {/n res = value;/n } else if (isArray(value)) {/n for (let i = 0; i < value.length; i++) {/n const normalized = normalizeClass(value[i]);/n if (normalized) {/n res += normalized + /' /';/n }/n }/n } else if (isObject(value)) {/n for (const name in value) {/n if (value[name]) {/n res += name + /' /';/n }/n }/n }/n return res.trim();/n}/nconst toDisplayString = (val) => {/n return isString(val) ? val : val == null ? /'/' : isArray(val) || isObject(val) && (val.toString === objectToString || !isFunction(val.toString)) ? JSON.stringify(val, replacer, 2) : String(val);/n};/nconst replacer = (_key, val) => {/n if (val && val.__v_isRef) {/n return replacer(_key, val.value);/n } else if (isMap(val)) {/n return {/n [`Map(${val.size})`]: [...val.entries()].reduce((entries, [key, val2]) => {/n entries[`${key} =>`] = val2;/n return entries;/n }, {})/n };/n } else if (isSet(val)) {/n return {/n [`Set(${val.size})`]: [...val.values()]/n };/n } else if (isObject(val) && !isArray(val) && !isPlainObject(val)) {/n return String(val);/n }/n return val;/n};/nconst EMPTY_OBJ = Object.freeze({});/nconst EMPTY_ARR = Object.freeze([]);/nconst NOOP = () => {};/nconst NO = () => false;/nconst onRE = /^on[^a-z]/;/nconst isOn = (key) => onRE.test(key);/nconst isModelListener = (key) => key.startsWith(/'onUpdate:/');/nconst extend = Object.assign;/nconst remove = (arr, el) => {/n const i = arr.indexOf(el);/n if (i > -1) {/n arr.splice(i, 1);/n }/n};/nconst hasOwnProperty$1 = Object.prototype.hasOwnProperty;/nconst hasOwn = (val, key) => hasOwnProperty$1.call(val, key);/nconst isArray = Array.isArray;/nconst isMap = (val) => toTypeString(val) === /'[object Map]/';/nconst isSet = (val) => toTypeString(val) === /'[object Set]/';/nconst isFunction = (val) => typeof val === /'function/';/nconst isString = (val) => typeof val === /'string/';/nconst isSymbol = (val) => typeof val === /'symbol/';/nconst isObject = (val) => val !== null && typeof val === /'object/';/nconst isPromise = (val) => {/n return isObject(val) && isFunction(val.then) && isFunction(val.catch);/n};/nconst objectToString = Object.prototype.toString;/nconst toTypeString = (value) => objectToString.call(value);/nconst toRawType = (value) => {/n return toTypeString(value).slice(8, -1);/n};/nconst isPlainObject = (val) => toTypeString(val) === /'[object Object]/';/nconst isIntegerKey = (key) => isString(key) && key !== /'NaN/' && key[0] !== /'-/' && /'/' + parseInt(key, 10) === key;/nconst isReservedProp = /* @__PURE__ */ makeMap(/n // the leading comma is intentional so empty string /'/' is also included/n /',key,ref,ref_for,ref_key,onVnodeBeforeMount,onVnodeMounted,onVnodeBeforeUpdate,onVnodeUpdated,onVnodeBeforeUnmount,onVnodeUnmounted/'/n);/nconst isBuiltInDirective = /* @__PURE__ */ makeMap(/'bind,cloak,else-if,else,for,html,if,model,on,once,pre,show,slot,text,memo/');/nconst cacheStringFunction = (fn) => {/n const cache = /* @__PURE__ */ Object.create(null);/n return (str) => {/n const hit = cache[str];/n return hit || (cache[str] = fn(str));/n };/n};/nconst camelizeRE = /-(/w)/g;/nconst camelize = cacheStringFunction((str) => {/n return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : /'/');/n});/nconst hyphenateRE = //B([A-Z])/g;/nconst hyphenate = cacheStringFunction((str) => str.replace(hyphenateRE, /'-$1/').toLowerCase());/nconst capitalize = cacheStringFunction((str) => str.charAt(0).toUpperCase() + str.slice(1));/nconst toHandlerKey = cacheStringFunction((str) => str ? `on${capitalize(str)}` : ``);/nconst hasChanged = (value, oldValue) => !Object.is(value, oldValue);/nconst invokeArrayFns$1 = (fns, arg) => {/n for (let i = 0; i < fns.length; i++) {/n fns[i](arg);/n }/n};/nconst def = (obj, key, value) => {/n Object.defineProperty(obj, key, {/n configurable: true,/n enumerable: false,/n value/n });/n};/nconst looseToNumber = (val) => {/n const n2 = parseFloat(val);/n return isNaN(n2) ? val : n2;/n};/n};/nconst LINEFEED = /'//n/';/nconst SLOT_DEFAULT_NAME = /'d/';/nconst ON_SHOW = /'onShow/';/nconst ON_HIDE = /'onHide/';/nconst ON_LAUNCH = /'onLaunch/';/nconst ON_ERROR = /'onError/';/nconst ON_THEME_CHANGE = /'onThemeChange/';/nconst ON_PAGE_NOT_FOUND = /'onPageNotFound/';/nconst ON_UNHANDLE_REJECTION = /'onUnhandledRejection/';/nconst ON_LOAD = /'onLoad/';/nconst ON_READY = /'onReady/';/nconst ON_UNLOAD = /'onUnload/';/nconst ON_INIT = /'onInit/';/nconst ON_SAVE_EXIT_STATE = /'onSaveExitState/';/nconst ON_RESIZE = /'onResize/';/nconst ON_BACK_PRESS = /'onBackPress/';/nconst ON_PAGE_SCROLL = /'onPageScroll/';/nconst ON_TAB_ITEM_TAP = /'onTabItemTap/';/nconst ON_REACH_BOTTOM = /'onReachBottom/';/nconst ON_PULL_DOWN_REFRESH = /'onPullDownRefresh/';/nconst ON_SHARE_TIMELINE = /'onShareTimeline/';/nconst ON_ADD_TO_FAVORITES = /'onAddToFavorites/';/nconst ON_SHARE_APP_MESSAGE = /'onShareAppMessage/';/nconst ON_NAVIGATION_BAR_BUTTON_TAP = /'onNavigationBarButtonTap/';/nconst ON_NAVIGATION_BAR_SEARCH_INPUT_CLICKED = /'onNavigationBarSearchInputClicked/';/nconst ON_NAVIGATION_BAR_SEARCH_INPUT_CHANGED = /'onNavigationBarSearchInputChanged/';/nconst ON_NAVIGATION_BAR_SEARCH_INPUT_CONFIRMED = /'onNavigationBarSearchInputConfirmed/';/nconst ON_NAVIGATION_BAR_SEARCH_INPUT_FOCUS_CHANGED = /'onNavigationBarSearchInputFocusChanged/';/nconst customizeRE = /:/g;/nfunction customizeEvent(str) {/n return camelize(str.replace(customizeRE, /'-/'));/n}/nfunction hasLeadingSlash(str) {/n return str.indexOf(/'//') === 0;/n}/nfunction addLeadingSlash(str) {/n return hasLeadingSlash(str) ? str : /'//' + str;/n}/nconst invokeArrayFns = (fns, arg) => {/n let ret;/n for (let i = 0; i < fns.length; i++) {/n ret = fns[i](arg);/n }/n return ret;/n};/nfunction once(fn, ctx = null) {/n let res;/n return (...args) => {/n if (fn) {/n res = fn.apply(ctx, args);/n fn = null;/n }/n return res;/n };/n}/nfunction getValueByDataPath(obj, path) {/n if (!isString(path)) {/n return;/n }/n path = path.replace(//[(/d+)/]/g, /'$/' + 1);/n const parts = path.split(/'./');/n let key = parts[0];/n if (!obj) {/n obj = {};/n }/n if (parts.length === 1) {/n return obj[key];/n }/n return getValueByDataPath(obj[key], parts.slice(1).join(/'./'));/n}/nfunction sortObject(obj) {/n let sortObj = {};/n if (isPlainObject(obj)) {/n Object.keys(obj).sort().forEach((key) => {/n const _key = key;/n sortObj[_key] = obj[_key];/n });/n }/n return !Object.keys(sortObj) ? obj : sortObj;/n}/nconst encode = encodeURIComponent;/nfunction stringifyQuery(obj, encodeStr = encode) {/n const res = obj ? Object.keys(obj).map((key) => {/n let val = obj[key];/n if (typeof val === void 0 || val === null) {/n val = /'/';/n } else if (isPlainObject(val)) {/n val = JSON.stringify(val);/n }/n return encodeStr(key) + /'=/' + encodeStr(val);/n }).filter((x) => x.length > 0).join(/'&/') : null;/n return res ? `?${res}` : /'/';/n}/n/n
原文地址: https://www.cveoy.top/t/topic/lRSL 著作权归作者所有。请勿转载和采集!