shared.cjs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*!
  2. * shared v11.1.3
  3. * (c) 2025 kazuya kawaguchi
  4. * Released under the MIT License.
  5. */
  6. 'use strict';
  7. /**
  8. * Original Utilities
  9. * written by kazuya kawaguchi
  10. */
  11. const inBrowser = typeof window !== 'undefined';
  12. exports.mark = void 0;
  13. exports.measure = void 0;
  14. {
  15. const perf = inBrowser && window.performance;
  16. if (perf &&
  17. perf.mark &&
  18. perf.measure &&
  19. perf.clearMarks &&
  20. // @ts-ignore browser compat
  21. perf.clearMeasures) {
  22. exports.mark = (tag) => {
  23. perf.mark(tag);
  24. };
  25. exports.measure = (name, startTag, endTag) => {
  26. perf.measure(name, startTag, endTag);
  27. perf.clearMarks(startTag);
  28. perf.clearMarks(endTag);
  29. };
  30. }
  31. }
  32. const RE_ARGS = /\{([0-9a-zA-Z]+)\}/g;
  33. /* eslint-disable */
  34. function format(message, ...args) {
  35. if (args.length === 1 && isObject(args[0])) {
  36. args = args[0];
  37. }
  38. if (!args || !args.hasOwnProperty) {
  39. args = {};
  40. }
  41. return message.replace(RE_ARGS, (match, identifier) => {
  42. return args.hasOwnProperty(identifier) ? args[identifier] : '';
  43. });
  44. }
  45. const makeSymbol = (name, shareable = false) => !shareable ? Symbol(name) : Symbol.for(name);
  46. const generateFormatCacheKey = (locale, key, source) => friendlyJSONstringify({ l: locale, k: key, s: source });
  47. const friendlyJSONstringify = (json) => JSON.stringify(json)
  48. .replace(/\u2028/g, '\\u2028')
  49. .replace(/\u2029/g, '\\u2029')
  50. .replace(/\u0027/g, '\\u0027');
  51. const isNumber = (val) => typeof val === 'number' && isFinite(val);
  52. const isDate = (val) => toTypeString(val) === '[object Date]';
  53. const isRegExp = (val) => toTypeString(val) === '[object RegExp]';
  54. const isEmptyObject = (val) => isPlainObject(val) && Object.keys(val).length === 0;
  55. const assign = Object.assign;
  56. const _create = Object.create;
  57. const create = (obj = null) => _create(obj);
  58. let _globalThis;
  59. const getGlobalThis = () => {
  60. // prettier-ignore
  61. return (_globalThis ||
  62. (_globalThis =
  63. typeof globalThis !== 'undefined'
  64. ? globalThis
  65. : typeof self !== 'undefined'
  66. ? self
  67. : typeof window !== 'undefined'
  68. ? window
  69. : typeof global !== 'undefined'
  70. ? global
  71. : create()));
  72. };
  73. function escapeHtml(rawText) {
  74. return rawText
  75. .replace(/</g, '&lt;')
  76. .replace(/>/g, '&gt;')
  77. .replace(/"/g, '&quot;')
  78. .replace(/'/g, '&apos;');
  79. }
  80. const hasOwnProperty = Object.prototype.hasOwnProperty;
  81. function hasOwn(obj, key) {
  82. return hasOwnProperty.call(obj, key);
  83. }
  84. /* eslint-enable */
  85. /**
  86. * Useful Utilities By Evan you
  87. * Modified by kazuya kawaguchi
  88. * MIT License
  89. * https://github.com/vuejs/vue-next/blob/master/packages/shared/src/index.ts
  90. * https://github.com/vuejs/vue-next/blob/master/packages/shared/src/codeframe.ts
  91. */
  92. const isArray = Array.isArray;
  93. const isFunction = (val) => typeof val === 'function';
  94. const isString = (val) => typeof val === 'string';
  95. const isBoolean = (val) => typeof val === 'boolean';
  96. const isSymbol = (val) => typeof val === 'symbol';
  97. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  98. const isObject = (val) => val !== null && typeof val === 'object';
  99. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  100. const isPromise = (val) => {
  101. return isObject(val) && isFunction(val.then) && isFunction(val.catch);
  102. };
  103. const objectToString = Object.prototype.toString;
  104. const toTypeString = (value) => objectToString.call(value);
  105. const isPlainObject = (val) => toTypeString(val) === '[object Object]';
  106. // for converting list and named values to displayed strings.
  107. const toDisplayString = (val) => {
  108. return val == null
  109. ? ''
  110. : isArray(val) || (isPlainObject(val) && val.toString === objectToString)
  111. ? JSON.stringify(val, null, 2)
  112. : String(val);
  113. };
  114. function join(items, separator = '') {
  115. return items.reduce((str, item, index) => (index === 0 ? str + item : str + separator + item), '');
  116. }
  117. const RANGE = 2;
  118. function generateCodeFrame(source, start = 0, end = source.length) {
  119. const lines = source.split(/\r?\n/);
  120. let count = 0;
  121. const res = [];
  122. for (let i = 0; i < lines.length; i++) {
  123. count += lines[i].length + 1;
  124. if (count >= start) {
  125. for (let j = i - RANGE; j <= i + RANGE || end > count; j++) {
  126. if (j < 0 || j >= lines.length)
  127. continue;
  128. const line = j + 1;
  129. res.push(`${line}${' '.repeat(3 - String(line).length)}| ${lines[j]}`);
  130. const lineLength = lines[j].length;
  131. if (j === i) {
  132. // push underline
  133. const pad = start - (count - lineLength) + 1;
  134. const length = Math.max(1, end > count ? lineLength - pad : end - start);
  135. res.push(` | ` + ' '.repeat(pad) + '^'.repeat(length));
  136. }
  137. else if (j > i) {
  138. if (end > count) {
  139. const length = Math.max(Math.min(end - count, lineLength), 1);
  140. res.push(` | ` + '^'.repeat(length));
  141. }
  142. count += lineLength + 1;
  143. }
  144. }
  145. break;
  146. }
  147. }
  148. return res.join('\n');
  149. }
  150. function warn(msg, err) {
  151. if (typeof console !== 'undefined') {
  152. console.warn(`[intlify] ` + msg);
  153. /* istanbul ignore if */
  154. if (err) {
  155. console.warn(err.stack);
  156. }
  157. }
  158. }
  159. const hasWarned = {};
  160. function warnOnce(msg) {
  161. if (!hasWarned[msg]) {
  162. hasWarned[msg] = true;
  163. warn(msg);
  164. }
  165. }
  166. /**
  167. * Event emitter, forked from the below:
  168. * - original repository url: https://github.com/developit/mitt
  169. * - code url: https://github.com/developit/mitt/blob/master/src/index.ts
  170. * - author: Jason Miller (https://github.com/developit)
  171. * - license: MIT
  172. */
  173. /**
  174. * Create a event emitter
  175. *
  176. * @returns An event emitter
  177. */
  178. function createEmitter() {
  179. const events = new Map();
  180. const emitter = {
  181. events,
  182. on(event, handler) {
  183. const handlers = events.get(event);
  184. const added = handlers && handlers.push(handler);
  185. if (!added) {
  186. events.set(event, [handler]);
  187. }
  188. },
  189. off(event, handler) {
  190. const handlers = events.get(event);
  191. if (handlers) {
  192. handlers.splice(handlers.indexOf(handler) >>> 0, 1);
  193. }
  194. },
  195. emit(event, payload) {
  196. (events.get(event) || [])
  197. .slice()
  198. .map(handler => handler(payload));
  199. (events.get('*') || [])
  200. .slice()
  201. .map(handler => handler(event, payload));
  202. }
  203. };
  204. return emitter;
  205. }
  206. const isNotObjectOrIsArray = (val) => !isObject(val) || isArray(val);
  207. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  208. function deepCopy(src, des) {
  209. // src and des should both be objects, and none of them can be a array
  210. if (isNotObjectOrIsArray(src) || isNotObjectOrIsArray(des)) {
  211. throw new Error('Invalid value');
  212. }
  213. const stack = [{ src, des }];
  214. while (stack.length) {
  215. const { src, des } = stack.pop();
  216. // using `Object.keys` which skips prototype properties
  217. Object.keys(src).forEach(key => {
  218. if (key === '__proto__') {
  219. return;
  220. }
  221. // if src[key] is an object/array, set des[key]
  222. // to empty object/array to prevent setting by reference
  223. if (isObject(src[key]) && !isObject(des[key])) {
  224. des[key] = Array.isArray(src[key]) ? [] : create();
  225. }
  226. if (isNotObjectOrIsArray(des[key]) || isNotObjectOrIsArray(src[key])) {
  227. // replace with src[key] when:
  228. // src[key] or des[key] is not an object, or
  229. // src[key] or des[key] is an array
  230. des[key] = src[key];
  231. }
  232. else {
  233. // src[key] and des[key] are both objects, merge them
  234. stack.push({ src: src[key], des: des[key] });
  235. }
  236. });
  237. }
  238. }
  239. exports.assign = assign;
  240. exports.create = create;
  241. exports.createEmitter = createEmitter;
  242. exports.deepCopy = deepCopy;
  243. exports.escapeHtml = escapeHtml;
  244. exports.format = format;
  245. exports.friendlyJSONstringify = friendlyJSONstringify;
  246. exports.generateCodeFrame = generateCodeFrame;
  247. exports.generateFormatCacheKey = generateFormatCacheKey;
  248. exports.getGlobalThis = getGlobalThis;
  249. exports.hasOwn = hasOwn;
  250. exports.inBrowser = inBrowser;
  251. exports.isArray = isArray;
  252. exports.isBoolean = isBoolean;
  253. exports.isDate = isDate;
  254. exports.isEmptyObject = isEmptyObject;
  255. exports.isFunction = isFunction;
  256. exports.isNumber = isNumber;
  257. exports.isObject = isObject;
  258. exports.isPlainObject = isPlainObject;
  259. exports.isPromise = isPromise;
  260. exports.isRegExp = isRegExp;
  261. exports.isString = isString;
  262. exports.isSymbol = isSymbol;
  263. exports.join = join;
  264. exports.makeSymbol = makeSymbol;
  265. exports.objectToString = objectToString;
  266. exports.toDisplayString = toDisplayString;
  267. exports.toTypeString = toTypeString;
  268. exports.warn = warn;
  269. exports.warnOnce = warnOnce;