Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.format = format;
  6. exports.default = exports.plugins = exports.DEFAULT_OPTIONS = void 0;
  7. var _ansiStyles = _interopRequireDefault(require('ansi-styles'));
  8. var _collections = require('./collections');
  9. var _AsymmetricMatcher = _interopRequireDefault(
  10. require('./plugins/AsymmetricMatcher')
  11. );
  12. var _ConvertAnsi = _interopRequireDefault(require('./plugins/ConvertAnsi'));
  13. var _DOMCollection = _interopRequireDefault(require('./plugins/DOMCollection'));
  14. var _DOMElement = _interopRequireDefault(require('./plugins/DOMElement'));
  15. var _Immutable = _interopRequireDefault(require('./plugins/Immutable'));
  16. var _ReactElement = _interopRequireDefault(require('./plugins/ReactElement'));
  17. var _ReactTestComponent = _interopRequireDefault(
  18. require('./plugins/ReactTestComponent')
  19. );
  20. function _interopRequireDefault(obj) {
  21. return obj && obj.__esModule ? obj : {default: obj};
  22. }
  23. /**
  24. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  25. *
  26. * This source code is licensed under the MIT license found in the
  27. * LICENSE file in the root directory of this source tree.
  28. */
  29. /* eslint-disable local/ban-types-eventually */
  30. const toString = Object.prototype.toString;
  31. const toISOString = Date.prototype.toISOString;
  32. const errorToString = Error.prototype.toString;
  33. const regExpToString = RegExp.prototype.toString;
  34. /**
  35. * Explicitly comparing typeof constructor to function avoids undefined as name
  36. * when mock identity-obj-proxy returns the key as the value for any key.
  37. */
  38. const getConstructorName = val =>
  39. (typeof val.constructor === 'function' && val.constructor.name) || 'Object';
  40. /* global window */
  41. /** Is val is equal to global window object? Works even if it does not exist :) */
  42. const isWindow = val => typeof window !== 'undefined' && val === window;
  43. const SYMBOL_REGEXP = /^Symbol\((.*)\)(.*)$/;
  44. const NEWLINE_REGEXP = /\n/gi;
  45. class PrettyFormatPluginError extends Error {
  46. constructor(message, stack) {
  47. super(message);
  48. this.stack = stack;
  49. this.name = this.constructor.name;
  50. }
  51. }
  52. function isToStringedArrayType(toStringed) {
  53. return (
  54. toStringed === '[object Array]' ||
  55. toStringed === '[object ArrayBuffer]' ||
  56. toStringed === '[object DataView]' ||
  57. toStringed === '[object Float32Array]' ||
  58. toStringed === '[object Float64Array]' ||
  59. toStringed === '[object Int8Array]' ||
  60. toStringed === '[object Int16Array]' ||
  61. toStringed === '[object Int32Array]' ||
  62. toStringed === '[object Uint8Array]' ||
  63. toStringed === '[object Uint8ClampedArray]' ||
  64. toStringed === '[object Uint16Array]' ||
  65. toStringed === '[object Uint32Array]'
  66. );
  67. }
  68. function printNumber(val) {
  69. return Object.is(val, -0) ? '-0' : String(val);
  70. }
  71. function printBigInt(val) {
  72. return String(`${val}n`);
  73. }
  74. function printFunction(val, printFunctionName) {
  75. if (!printFunctionName) {
  76. return '[Function]';
  77. }
  78. return '[Function ' + (val.name || 'anonymous') + ']';
  79. }
  80. function printSymbol(val) {
  81. return String(val).replace(SYMBOL_REGEXP, 'Symbol($1)');
  82. }
  83. function printError(val) {
  84. return '[' + errorToString.call(val) + ']';
  85. }
  86. /**
  87. * The first port of call for printing an object, handles most of the
  88. * data-types in JS.
  89. */
  90. function printBasicValue(val, printFunctionName, escapeRegex, escapeString) {
  91. if (val === true || val === false) {
  92. return '' + val;
  93. }
  94. if (val === undefined) {
  95. return 'undefined';
  96. }
  97. if (val === null) {
  98. return 'null';
  99. }
  100. const typeOf = typeof val;
  101. if (typeOf === 'number') {
  102. return printNumber(val);
  103. }
  104. if (typeOf === 'bigint') {
  105. return printBigInt(val);
  106. }
  107. if (typeOf === 'string') {
  108. if (escapeString) {
  109. return '"' + val.replace(/"|\\/g, '\\$&') + '"';
  110. }
  111. return '"' + val + '"';
  112. }
  113. if (typeOf === 'function') {
  114. return printFunction(val, printFunctionName);
  115. }
  116. if (typeOf === 'symbol') {
  117. return printSymbol(val);
  118. }
  119. const toStringed = toString.call(val);
  120. if (toStringed === '[object WeakMap]') {
  121. return 'WeakMap {}';
  122. }
  123. if (toStringed === '[object WeakSet]') {
  124. return 'WeakSet {}';
  125. }
  126. if (
  127. toStringed === '[object Function]' ||
  128. toStringed === '[object GeneratorFunction]'
  129. ) {
  130. return printFunction(val, printFunctionName);
  131. }
  132. if (toStringed === '[object Symbol]') {
  133. return printSymbol(val);
  134. }
  135. if (toStringed === '[object Date]') {
  136. return isNaN(+val) ? 'Date { NaN }' : toISOString.call(val);
  137. }
  138. if (toStringed === '[object Error]') {
  139. return printError(val);
  140. }
  141. if (toStringed === '[object RegExp]') {
  142. if (escapeRegex) {
  143. // https://github.com/benjamingr/RegExp.escape/blob/main/polyfill.js
  144. return regExpToString.call(val).replace(/[\\^$*+?.()|[\]{}]/g, '\\$&');
  145. }
  146. return regExpToString.call(val);
  147. }
  148. if (val instanceof Error) {
  149. return printError(val);
  150. }
  151. return null;
  152. }
  153. /**
  154. * Handles more complex objects ( such as objects with circular references.
  155. * maps and sets etc )
  156. */
  157. function printComplexValue(
  158. val,
  159. config,
  160. indentation,
  161. depth,
  162. refs,
  163. hasCalledToJSON
  164. ) {
  165. if (refs.indexOf(val) !== -1) {
  166. return '[Circular]';
  167. }
  168. refs = refs.slice();
  169. refs.push(val);
  170. const hitMaxDepth = ++depth > config.maxDepth;
  171. const min = config.min;
  172. if (
  173. config.callToJSON &&
  174. !hitMaxDepth &&
  175. val.toJSON &&
  176. typeof val.toJSON === 'function' &&
  177. !hasCalledToJSON
  178. ) {
  179. return printer(val.toJSON(), config, indentation, depth, refs, true);
  180. }
  181. const toStringed = toString.call(val);
  182. if (toStringed === '[object Arguments]') {
  183. return hitMaxDepth
  184. ? '[Arguments]'
  185. : (min ? '' : 'Arguments ') +
  186. '[' +
  187. (0, _collections.printListItems)(
  188. val,
  189. config,
  190. indentation,
  191. depth,
  192. refs,
  193. printer
  194. ) +
  195. ']';
  196. }
  197. if (isToStringedArrayType(toStringed)) {
  198. return hitMaxDepth
  199. ? '[' + val.constructor.name + ']'
  200. : (min
  201. ? ''
  202. : !config.printBasicPrototype && val.constructor.name === 'Array'
  203. ? ''
  204. : val.constructor.name + ' ') +
  205. '[' +
  206. (0, _collections.printListItems)(
  207. val,
  208. config,
  209. indentation,
  210. depth,
  211. refs,
  212. printer
  213. ) +
  214. ']';
  215. }
  216. if (toStringed === '[object Map]') {
  217. return hitMaxDepth
  218. ? '[Map]'
  219. : 'Map {' +
  220. (0, _collections.printIteratorEntries)(
  221. val.entries(),
  222. config,
  223. indentation,
  224. depth,
  225. refs,
  226. printer,
  227. ' => '
  228. ) +
  229. '}';
  230. }
  231. if (toStringed === '[object Set]') {
  232. return hitMaxDepth
  233. ? '[Set]'
  234. : 'Set {' +
  235. (0, _collections.printIteratorValues)(
  236. val.values(),
  237. config,
  238. indentation,
  239. depth,
  240. refs,
  241. printer
  242. ) +
  243. '}';
  244. } // Avoid failure to serialize global window object in jsdom test environment.
  245. // For example, not even relevant if window is prop of React element.
  246. return hitMaxDepth || isWindow(val)
  247. ? '[' + getConstructorName(val) + ']'
  248. : (min
  249. ? ''
  250. : !config.printBasicPrototype && getConstructorName(val) === 'Object'
  251. ? ''
  252. : getConstructorName(val) + ' ') +
  253. '{' +
  254. (0, _collections.printObjectProperties)(
  255. val,
  256. config,
  257. indentation,
  258. depth,
  259. refs,
  260. printer
  261. ) +
  262. '}';
  263. }
  264. function isNewPlugin(plugin) {
  265. return plugin.serialize != null;
  266. }
  267. function printPlugin(plugin, val, config, indentation, depth, refs) {
  268. let printed;
  269. try {
  270. printed = isNewPlugin(plugin)
  271. ? plugin.serialize(val, config, indentation, depth, refs, printer)
  272. : plugin.print(
  273. val,
  274. valChild => printer(valChild, config, indentation, depth, refs),
  275. str => {
  276. const indentationNext = indentation + config.indent;
  277. return (
  278. indentationNext +
  279. str.replace(NEWLINE_REGEXP, '\n' + indentationNext)
  280. );
  281. },
  282. {
  283. edgeSpacing: config.spacingOuter,
  284. min: config.min,
  285. spacing: config.spacingInner
  286. },
  287. config.colors
  288. );
  289. } catch (error) {
  290. throw new PrettyFormatPluginError(error.message, error.stack);
  291. }
  292. if (typeof printed !== 'string') {
  293. throw new Error(
  294. `pretty-format: Plugin must return type "string" but instead returned "${typeof printed}".`
  295. );
  296. }
  297. return printed;
  298. }
  299. function findPlugin(plugins, val) {
  300. for (let p = 0; p < plugins.length; p++) {
  301. try {
  302. if (plugins[p].test(val)) {
  303. return plugins[p];
  304. }
  305. } catch (error) {
  306. throw new PrettyFormatPluginError(error.message, error.stack);
  307. }
  308. }
  309. return null;
  310. }
  311. function printer(val, config, indentation, depth, refs, hasCalledToJSON) {
  312. const plugin = findPlugin(config.plugins, val);
  313. if (plugin !== null) {
  314. return printPlugin(plugin, val, config, indentation, depth, refs);
  315. }
  316. const basicResult = printBasicValue(
  317. val,
  318. config.printFunctionName,
  319. config.escapeRegex,
  320. config.escapeString
  321. );
  322. if (basicResult !== null) {
  323. return basicResult;
  324. }
  325. return printComplexValue(
  326. val,
  327. config,
  328. indentation,
  329. depth,
  330. refs,
  331. hasCalledToJSON
  332. );
  333. }
  334. const DEFAULT_THEME = {
  335. comment: 'gray',
  336. content: 'reset',
  337. prop: 'yellow',
  338. tag: 'cyan',
  339. value: 'green'
  340. };
  341. const DEFAULT_THEME_KEYS = Object.keys(DEFAULT_THEME);
  342. const DEFAULT_OPTIONS = {
  343. callToJSON: true,
  344. escapeRegex: false,
  345. escapeString: true,
  346. highlight: false,
  347. indent: 2,
  348. maxDepth: Infinity,
  349. min: false,
  350. plugins: [],
  351. printBasicPrototype: true,
  352. printFunctionName: true,
  353. theme: DEFAULT_THEME
  354. };
  355. exports.DEFAULT_OPTIONS = DEFAULT_OPTIONS;
  356. function validateOptions(options) {
  357. Object.keys(options).forEach(key => {
  358. if (!DEFAULT_OPTIONS.hasOwnProperty(key)) {
  359. throw new Error(`pretty-format: Unknown option "${key}".`);
  360. }
  361. });
  362. if (options.min && options.indent !== undefined && options.indent !== 0) {
  363. throw new Error(
  364. 'pretty-format: Options "min" and "indent" cannot be used together.'
  365. );
  366. }
  367. if (options.theme !== undefined) {
  368. if (options.theme === null) {
  369. throw new Error(`pretty-format: Option "theme" must not be null.`);
  370. }
  371. if (typeof options.theme !== 'object') {
  372. throw new Error(
  373. `pretty-format: Option "theme" must be of type "object" but instead received "${typeof options.theme}".`
  374. );
  375. }
  376. }
  377. }
  378. const getColorsHighlight = options =>
  379. DEFAULT_THEME_KEYS.reduce((colors, key) => {
  380. const value =
  381. options.theme && options.theme[key] !== undefined
  382. ? options.theme[key]
  383. : DEFAULT_THEME[key];
  384. const color = value && _ansiStyles.default[value];
  385. if (
  386. color &&
  387. typeof color.close === 'string' &&
  388. typeof color.open === 'string'
  389. ) {
  390. colors[key] = color;
  391. } else {
  392. throw new Error(
  393. `pretty-format: Option "theme" has a key "${key}" whose value "${value}" is undefined in ansi-styles.`
  394. );
  395. }
  396. return colors;
  397. }, Object.create(null));
  398. const getColorsEmpty = () =>
  399. DEFAULT_THEME_KEYS.reduce((colors, key) => {
  400. colors[key] = {
  401. close: '',
  402. open: ''
  403. };
  404. return colors;
  405. }, Object.create(null));
  406. const getPrintFunctionName = options =>
  407. options && options.printFunctionName !== undefined
  408. ? options.printFunctionName
  409. : DEFAULT_OPTIONS.printFunctionName;
  410. const getEscapeRegex = options =>
  411. options && options.escapeRegex !== undefined
  412. ? options.escapeRegex
  413. : DEFAULT_OPTIONS.escapeRegex;
  414. const getEscapeString = options =>
  415. options && options.escapeString !== undefined
  416. ? options.escapeString
  417. : DEFAULT_OPTIONS.escapeString;
  418. const getConfig = options => {
  419. var _options$printBasicPr;
  420. return {
  421. callToJSON:
  422. options && options.callToJSON !== undefined
  423. ? options.callToJSON
  424. : DEFAULT_OPTIONS.callToJSON,
  425. colors:
  426. options && options.highlight
  427. ? getColorsHighlight(options)
  428. : getColorsEmpty(),
  429. escapeRegex: getEscapeRegex(options),
  430. escapeString: getEscapeString(options),
  431. indent:
  432. options && options.min
  433. ? ''
  434. : createIndent(
  435. options && options.indent !== undefined
  436. ? options.indent
  437. : DEFAULT_OPTIONS.indent
  438. ),
  439. maxDepth:
  440. options && options.maxDepth !== undefined
  441. ? options.maxDepth
  442. : DEFAULT_OPTIONS.maxDepth,
  443. min:
  444. options && options.min !== undefined ? options.min : DEFAULT_OPTIONS.min,
  445. plugins:
  446. options && options.plugins !== undefined
  447. ? options.plugins
  448. : DEFAULT_OPTIONS.plugins,
  449. printBasicPrototype:
  450. (_options$printBasicPr =
  451. options === null || options === void 0
  452. ? void 0
  453. : options.printBasicPrototype) !== null &&
  454. _options$printBasicPr !== void 0
  455. ? _options$printBasicPr
  456. : true,
  457. printFunctionName: getPrintFunctionName(options),
  458. spacingInner: options && options.min ? ' ' : '\n',
  459. spacingOuter: options && options.min ? '' : '\n'
  460. };
  461. };
  462. function createIndent(indent) {
  463. return new Array(indent + 1).join(' ');
  464. }
  465. /**
  466. * Returns a presentation string of your `val` object
  467. * @param val any potential JavaScript object
  468. * @param options Custom settings
  469. */
  470. function format(val, options) {
  471. if (options) {
  472. validateOptions(options);
  473. if (options.plugins) {
  474. const plugin = findPlugin(options.plugins, val);
  475. if (plugin !== null) {
  476. return printPlugin(plugin, val, getConfig(options), '', 0, []);
  477. }
  478. }
  479. }
  480. const basicResult = printBasicValue(
  481. val,
  482. getPrintFunctionName(options),
  483. getEscapeRegex(options),
  484. getEscapeString(options)
  485. );
  486. if (basicResult !== null) {
  487. return basicResult;
  488. }
  489. return printComplexValue(val, getConfig(options), '', 0, []);
  490. }
  491. const plugins = {
  492. AsymmetricMatcher: _AsymmetricMatcher.default,
  493. ConvertAnsi: _ConvertAnsi.default,
  494. DOMCollection: _DOMCollection.default,
  495. DOMElement: _DOMElement.default,
  496. Immutable: _Immutable.default,
  497. ReactElement: _ReactElement.default,
  498. ReactTestComponent: _ReactTestComponent.default
  499. };
  500. exports.plugins = plugins;
  501. var _default = format;
  502. exports.default = _default;