shared.esm-browser.js 8.5 KB

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