shared.cjs.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*!
  2. * @intlify/shared v9.1.9
  3. * (c) 2021 kazuya kawaguchi
  4. * Released under the MIT License.
  5. */
  6. 'use strict';
  7. Object.defineProperty(exports, '__esModule', { value: true });
  8. /**
  9. * Original Utilities
  10. * written by kazuya kawaguchi
  11. */
  12. const inBrowser = typeof window !== 'undefined';
  13. exports.mark = void 0;
  14. exports.measure = void 0;
  15. {
  16. const perf = inBrowser && window.performance;
  17. if (perf &&
  18. perf.mark &&
  19. perf.measure &&
  20. perf.clearMarks &&
  21. perf.clearMeasures) {
  22. exports.mark = (tag) => perf.mark(tag);
  23. exports.measure = (name, startTag, endTag) => {
  24. perf.measure(name, startTag, endTag);
  25. perf.clearMarks(startTag);
  26. perf.clearMarks(endTag);
  27. };
  28. }
  29. }
  30. const RE_ARGS = /\{([0-9a-zA-Z]+)\}/g;
  31. /* eslint-disable */
  32. function format(message, ...args) {
  33. if (args.length === 1 && isObject(args[0])) {
  34. args = args[0];
  35. }
  36. if (!args || !args.hasOwnProperty) {
  37. args = {};
  38. }
  39. return message.replace(RE_ARGS, (match, identifier) => {
  40. return args.hasOwnProperty(identifier) ? args[identifier] : '';
  41. });
  42. }
  43. const hasSymbol = typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol';
  44. const makeSymbol = (name) => hasSymbol ? Symbol(name) : 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. function warn(msg, err) {
  55. if (typeof console !== 'undefined') {
  56. console.warn(`[intlify] ` + msg);
  57. /* istanbul ignore if */
  58. if (err) {
  59. console.warn(err.stack);
  60. }
  61. }
  62. }
  63. const assign = Object.assign;
  64. let _globalThis;
  65. const getGlobalThis = () => {
  66. // prettier-ignore
  67. return (_globalThis ||
  68. (_globalThis =
  69. typeof globalThis !== 'undefined'
  70. ? globalThis
  71. : typeof self !== 'undefined'
  72. ? self
  73. : typeof window !== 'undefined'
  74. ? window
  75. : typeof global !== 'undefined'
  76. ? global
  77. : {}));
  78. };
  79. function escapeHtml(rawText) {
  80. return rawText
  81. .replace(/</g, '&lt;')
  82. .replace(/>/g, '&gt;')
  83. .replace(/"/g, '&quot;')
  84. .replace(/'/g, '&apos;');
  85. }
  86. const hasOwnProperty = Object.prototype.hasOwnProperty;
  87. function hasOwn(obj, key) {
  88. return hasOwnProperty.call(obj, key);
  89. }
  90. /* eslint-enable */
  91. /**
  92. * Useful Utilities By Evan you
  93. * Modified by kazuya kawaguchi
  94. * MIT License
  95. * https://github.com/vuejs/vue-next/blob/master/packages/shared/src/index.ts
  96. * https://github.com/vuejs/vue-next/blob/master/packages/shared/src/codeframe.ts
  97. */
  98. const isArray = Array.isArray;
  99. const isFunction = (val) => typeof val === 'function';
  100. const isString = (val) => typeof val === 'string';
  101. const isBoolean = (val) => typeof val === 'boolean';
  102. const isSymbol = (val) => typeof val === 'symbol';
  103. const isObject = (val) => // eslint-disable-line
  104. val !== null && typeof val === 'object';
  105. const isPromise = (val) => {
  106. return isObject(val) && isFunction(val.then) && isFunction(val.catch);
  107. };
  108. const objectToString = Object.prototype.toString;
  109. const toTypeString = (value) => objectToString.call(value);
  110. const isPlainObject = (val) => toTypeString(val) === '[object Object]';
  111. // for converting list and named values to displayed strings.
  112. const toDisplayString = (val) => {
  113. return val == null
  114. ? ''
  115. : isArray(val) || (isPlainObject(val) && val.toString === objectToString)
  116. ? JSON.stringify(val, null, 2)
  117. : String(val);
  118. };
  119. const RANGE = 2;
  120. function generateCodeFrame(source, start = 0, end = source.length) {
  121. const lines = source.split(/\r?\n/);
  122. let count = 0;
  123. const res = [];
  124. for (let i = 0; i < lines.length; i++) {
  125. count += lines[i].length + 1;
  126. if (count >= start) {
  127. for (let j = i - RANGE; j <= i + RANGE || end > count; j++) {
  128. if (j < 0 || j >= lines.length)
  129. continue;
  130. const line = j + 1;
  131. res.push(`${line}${' '.repeat(3 - String(line).length)}| ${lines[j]}`);
  132. const lineLength = lines[j].length;
  133. if (j === i) {
  134. // push underline
  135. const pad = start - (count - lineLength) + 1;
  136. const length = Math.max(1, end > count ? lineLength - pad : end - start);
  137. res.push(` | ` + ' '.repeat(pad) + '^'.repeat(length));
  138. }
  139. else if (j > i) {
  140. if (end > count) {
  141. const length = Math.max(Math.min(end - count, lineLength), 1);
  142. res.push(` | ` + '^'.repeat(length));
  143. }
  144. count += lineLength + 1;
  145. }
  146. }
  147. break;
  148. }
  149. }
  150. return res.join('\n');
  151. }
  152. /**
  153. * Event emitter, forked from the below:
  154. * - original repository url: https://github.com/developit/mitt
  155. * - code url: https://github.com/developit/mitt/blob/master/src/index.ts
  156. * - author: Jason Miller (https://github.com/developit)
  157. * - license: MIT
  158. */
  159. /**
  160. * Create a event emitter
  161. *
  162. * @returns An event emitter
  163. */
  164. function createEmitter() {
  165. const events = new Map();
  166. const emitter = {
  167. events,
  168. on(event, handler) {
  169. const handlers = events.get(event);
  170. const added = handlers && handlers.push(handler);
  171. if (!added) {
  172. events.set(event, [handler]);
  173. }
  174. },
  175. off(event, handler) {
  176. const handlers = events.get(event);
  177. if (handlers) {
  178. handlers.splice(handlers.indexOf(handler) >>> 0, 1);
  179. }
  180. },
  181. emit(event, payload) {
  182. (events.get(event) || [])
  183. .slice()
  184. .map(handler => handler(payload));
  185. (events.get('*') || [])
  186. .slice()
  187. .map(handler => handler(event, payload));
  188. }
  189. };
  190. return emitter;
  191. }
  192. exports.assign = assign;
  193. exports.createEmitter = createEmitter;
  194. exports.escapeHtml = escapeHtml;
  195. exports.format = format;
  196. exports.friendlyJSONstringify = friendlyJSONstringify;
  197. exports.generateCodeFrame = generateCodeFrame;
  198. exports.generateFormatCacheKey = generateFormatCacheKey;
  199. exports.getGlobalThis = getGlobalThis;
  200. exports.hasOwn = hasOwn;
  201. exports.inBrowser = inBrowser;
  202. exports.isArray = isArray;
  203. exports.isBoolean = isBoolean;
  204. exports.isDate = isDate;
  205. exports.isEmptyObject = isEmptyObject;
  206. exports.isFunction = isFunction;
  207. exports.isNumber = isNumber;
  208. exports.isObject = isObject;
  209. exports.isPlainObject = isPlainObject;
  210. exports.isPromise = isPromise;
  211. exports.isRegExp = isRegExp;
  212. exports.isString = isString;
  213. exports.isSymbol = isSymbol;
  214. exports.makeSymbol = makeSymbol;
  215. exports.objectToString = objectToString;
  216. exports.toDisplayString = toDisplayString;
  217. exports.toTypeString = toTypeString;
  218. exports.warn = warn;