shared.cjs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*!
  2. * shared v9.5.0
  3. * (c) 2023 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. let _globalThis;
  57. const getGlobalThis = () => {
  58. // prettier-ignore
  59. return (_globalThis ||
  60. (_globalThis =
  61. typeof globalThis !== 'undefined'
  62. ? globalThis
  63. : typeof self !== 'undefined'
  64. ? self
  65. : typeof window !== 'undefined'
  66. ? window
  67. : typeof global !== 'undefined'
  68. ? global
  69. : {}));
  70. };
  71. function escapeHtml(rawText) {
  72. return rawText
  73. .replace(/</g, '&lt;')
  74. .replace(/>/g, '&gt;')
  75. .replace(/"/g, '&quot;')
  76. .replace(/'/g, '&apos;');
  77. }
  78. const hasOwnProperty = Object.prototype.hasOwnProperty;
  79. function hasOwn(obj, key) {
  80. return hasOwnProperty.call(obj, key);
  81. }
  82. /* eslint-enable */
  83. /**
  84. * Useful Utilities By Evan you
  85. * Modified by kazuya kawaguchi
  86. * MIT License
  87. * https://github.com/vuejs/vue-next/blob/master/packages/shared/src/index.ts
  88. * https://github.com/vuejs/vue-next/blob/master/packages/shared/src/codeframe.ts
  89. */
  90. const isArray = Array.isArray;
  91. const isFunction = (val) => typeof val === 'function';
  92. const isString = (val) => typeof val === 'string';
  93. const isBoolean = (val) => typeof val === 'boolean';
  94. const isSymbol = (val) => typeof val === 'symbol';
  95. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  96. const isObject = (val) => val !== null && typeof val === 'object';
  97. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  98. const isPromise = (val) => {
  99. return isObject(val) && isFunction(val.then) && isFunction(val.catch);
  100. };
  101. const objectToString = Object.prototype.toString;
  102. const toTypeString = (value) => objectToString.call(value);
  103. const isPlainObject = (val) => {
  104. if (!isObject(val))
  105. return false;
  106. const proto = Object.getPrototypeOf(val);
  107. return proto === null || proto.constructor === Object;
  108. };
  109. // for converting list and named values to displayed strings.
  110. const toDisplayString = (val) => {
  111. return val == null
  112. ? ''
  113. : isArray(val) || (isPlainObject(val) && val.toString === objectToString)
  114. ? JSON.stringify(val, null, 2)
  115. : String(val);
  116. };
  117. function join(items, separator = '') {
  118. return items.reduce((str, item, index) => (index === 0 ? str + item : str + separator + item), '');
  119. }
  120. const RANGE = 2;
  121. function generateCodeFrame(source, start = 0, end = source.length) {
  122. const lines = source.split(/\r?\n/);
  123. let count = 0;
  124. const res = [];
  125. for (let i = 0; i < lines.length; i++) {
  126. count += lines[i].length + 1;
  127. if (count >= start) {
  128. for (let j = i - RANGE; j <= i + RANGE || end > count; j++) {
  129. if (j < 0 || j >= lines.length)
  130. continue;
  131. const line = j + 1;
  132. res.push(`${line}${' '.repeat(3 - String(line).length)}| ${lines[j]}`);
  133. const lineLength = lines[j].length;
  134. if (j === i) {
  135. // push underline
  136. const pad = start - (count - lineLength) + 1;
  137. const length = Math.max(1, end > count ? lineLength - pad : end - start);
  138. res.push(` | ` + ' '.repeat(pad) + '^'.repeat(length));
  139. }
  140. else if (j > i) {
  141. if (end > count) {
  142. const length = Math.max(Math.min(end - count, lineLength), 1);
  143. res.push(` | ` + '^'.repeat(length));
  144. }
  145. count += lineLength + 1;
  146. }
  147. }
  148. break;
  149. }
  150. }
  151. return res.join('\n');
  152. }
  153. function incrementer(code) {
  154. let current = code;
  155. return () => ++current;
  156. }
  157. function warn(msg, err) {
  158. if (typeof console !== 'undefined') {
  159. console.warn(`[intlify] ` + msg);
  160. /* istanbul ignore if */
  161. if (err) {
  162. console.warn(err.stack);
  163. }
  164. }
  165. }
  166. const hasWarned = {};
  167. function warnOnce(msg) {
  168. if (!hasWarned[msg]) {
  169. hasWarned[msg] = true;
  170. warn(msg);
  171. }
  172. }
  173. /**
  174. * Event emitter, forked from the below:
  175. * - original repository url: https://github.com/developit/mitt
  176. * - code url: https://github.com/developit/mitt/blob/master/src/index.ts
  177. * - author: Jason Miller (https://github.com/developit)
  178. * - license: MIT
  179. */
  180. /**
  181. * Create a event emitter
  182. *
  183. * @returns An event emitter
  184. */
  185. function createEmitter() {
  186. const events = new Map();
  187. const emitter = {
  188. events,
  189. on(event, handler) {
  190. const handlers = events.get(event);
  191. const added = handlers && handlers.push(handler);
  192. if (!added) {
  193. events.set(event, [handler]);
  194. }
  195. },
  196. off(event, handler) {
  197. const handlers = events.get(event);
  198. if (handlers) {
  199. handlers.splice(handlers.indexOf(handler) >>> 0, 1);
  200. }
  201. },
  202. emit(event, payload) {
  203. (events.get(event) || [])
  204. .slice()
  205. .map(handler => handler(payload));
  206. (events.get('*') || [])
  207. .slice()
  208. .map(handler => handler(event, payload));
  209. }
  210. };
  211. return emitter;
  212. }
  213. exports.assign = assign;
  214. exports.createEmitter = createEmitter;
  215. exports.escapeHtml = escapeHtml;
  216. exports.format = format;
  217. exports.friendlyJSONstringify = friendlyJSONstringify;
  218. exports.generateCodeFrame = generateCodeFrame;
  219. exports.generateFormatCacheKey = generateFormatCacheKey;
  220. exports.getGlobalThis = getGlobalThis;
  221. exports.hasOwn = hasOwn;
  222. exports.inBrowser = inBrowser;
  223. exports.incrementer = incrementer;
  224. exports.isArray = isArray;
  225. exports.isBoolean = isBoolean;
  226. exports.isDate = isDate;
  227. exports.isEmptyObject = isEmptyObject;
  228. exports.isFunction = isFunction;
  229. exports.isNumber = isNumber;
  230. exports.isObject = isObject;
  231. exports.isPlainObject = isPlainObject;
  232. exports.isPromise = isPromise;
  233. exports.isRegExp = isRegExp;
  234. exports.isString = isString;
  235. exports.isSymbol = isSymbol;
  236. exports.join = join;
  237. exports.makeSymbol = makeSymbol;
  238. exports.objectToString = objectToString;
  239. exports.toDisplayString = toDisplayString;
  240. exports.toTypeString = toTypeString;
  241. exports.warn = warn;
  242. exports.warnOnce = warnOnce;