shared.esm-bundler.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*!
  2. * @intlify/shared v9.1.9
  3. * (c) 2021 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. if ((process.env.NODE_ENV !== 'production')) {
  14. const perf = inBrowser && window.performance;
  15. if (perf &&
  16. perf.mark &&
  17. perf.measure &&
  18. perf.clearMarks &&
  19. perf.clearMeasures) {
  20. mark = (tag) => perf.mark(tag);
  21. measure = (name, startTag, endTag) => {
  22. perf.measure(name, startTag, endTag);
  23. perf.clearMarks(startTag);
  24. perf.clearMarks(endTag);
  25. };
  26. }
  27. }
  28. const RE_ARGS = /\{([0-9a-zA-Z]+)\}/g;
  29. /* eslint-disable */
  30. function format(message, ...args) {
  31. if (args.length === 1 && isObject(args[0])) {
  32. args = args[0];
  33. }
  34. if (!args || !args.hasOwnProperty) {
  35. args = {};
  36. }
  37. return message.replace(RE_ARGS, (match, identifier) => {
  38. return args.hasOwnProperty(identifier) ? args[identifier] : '';
  39. });
  40. }
  41. const hasSymbol = typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol';
  42. const makeSymbol = (name) => hasSymbol ? Symbol(name) : name;
  43. const generateFormatCacheKey = (locale, key, source) => friendlyJSONstringify({ l: locale, k: key, s: source });
  44. const friendlyJSONstringify = (json) => JSON.stringify(json)
  45. .replace(/\u2028/g, '\\u2028')
  46. .replace(/\u2029/g, '\\u2029')
  47. .replace(/\u0027/g, '\\u0027');
  48. const isNumber = (val) => typeof val === 'number' && isFinite(val);
  49. const isDate = (val) => toTypeString(val) === '[object Date]';
  50. const isRegExp = (val) => toTypeString(val) === '[object RegExp]';
  51. const isEmptyObject = (val) => isPlainObject(val) && Object.keys(val).length === 0;
  52. function warn(msg, err) {
  53. if (typeof console !== 'undefined') {
  54. console.warn(`[intlify] ` + msg);
  55. /* istanbul ignore if */
  56. if (err) {
  57. console.warn(err.stack);
  58. }
  59. }
  60. }
  61. const assign = Object.assign;
  62. let _globalThis;
  63. const getGlobalThis = () => {
  64. // prettier-ignore
  65. return (_globalThis ||
  66. (_globalThis =
  67. typeof globalThis !== 'undefined'
  68. ? globalThis
  69. : typeof self !== 'undefined'
  70. ? self
  71. : typeof window !== 'undefined'
  72. ? window
  73. : typeof global !== 'undefined'
  74. ? global
  75. : {}));
  76. };
  77. function escapeHtml(rawText) {
  78. return rawText
  79. .replace(/</g, '&lt;')
  80. .replace(/>/g, '&gt;')
  81. .replace(/"/g, '&quot;')
  82. .replace(/'/g, '&apos;');
  83. }
  84. const hasOwnProperty = Object.prototype.hasOwnProperty;
  85. function hasOwn(obj, key) {
  86. return hasOwnProperty.call(obj, key);
  87. }
  88. /* eslint-enable */
  89. /**
  90. * Useful Utilities By Evan you
  91. * Modified by kazuya kawaguchi
  92. * MIT License
  93. * https://github.com/vuejs/vue-next/blob/master/packages/shared/src/index.ts
  94. * https://github.com/vuejs/vue-next/blob/master/packages/shared/src/codeframe.ts
  95. */
  96. const isArray = Array.isArray;
  97. const isFunction = (val) => typeof val === 'function';
  98. const isString = (val) => typeof val === 'string';
  99. const isBoolean = (val) => typeof val === 'boolean';
  100. const isSymbol = (val) => typeof val === 'symbol';
  101. const isObject = (val) => // eslint-disable-line
  102. val !== null && typeof val === 'object';
  103. const isPromise = (val) => {
  104. return isObject(val) && isFunction(val.then) && isFunction(val.catch);
  105. };
  106. const objectToString = Object.prototype.toString;
  107. const toTypeString = (value) => objectToString.call(value);
  108. const isPlainObject = (val) => toTypeString(val) === '[object Object]';
  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. 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. /**
  151. * Event emitter, forked from the below:
  152. * - original repository url: https://github.com/developit/mitt
  153. * - code url: https://github.com/developit/mitt/blob/master/src/index.ts
  154. * - author: Jason Miller (https://github.com/developit)
  155. * - license: MIT
  156. */
  157. /**
  158. * Create a event emitter
  159. *
  160. * @returns An event emitter
  161. */
  162. function createEmitter() {
  163. const events = new Map();
  164. const emitter = {
  165. events,
  166. on(event, handler) {
  167. const handlers = events.get(event);
  168. const added = handlers && handlers.push(handler);
  169. if (!added) {
  170. events.set(event, [handler]);
  171. }
  172. },
  173. off(event, handler) {
  174. const handlers = events.get(event);
  175. if (handlers) {
  176. handlers.splice(handlers.indexOf(handler) >>> 0, 1);
  177. }
  178. },
  179. emit(event, payload) {
  180. (events.get(event) || [])
  181. .slice()
  182. .map(handler => handler(payload));
  183. (events.get('*') || [])
  184. .slice()
  185. .map(handler => handler(event, payload));
  186. }
  187. };
  188. return emitter;
  189. }
  190. export { assign, createEmitter, escapeHtml, format, friendlyJSONstringify, generateCodeFrame, generateFormatCacheKey, getGlobalThis, hasOwn, inBrowser, isArray, isBoolean, isDate, isEmptyObject, isFunction, isNumber, isObject, isPlainObject, isPromise, isRegExp, isString, isSymbol, makeSymbol, mark, measure, objectToString, toDisplayString, toTypeString, warn };