shared.prod.cjs 8.7 KB

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