Source: Output/QRStringText.js

  1. /**
  2. * @created 11.07.2022
  3. * @author smiley <smiley@chillerlan.net>
  4. * @copyright 2022 smiley
  5. * @license MIT
  6. */
  7. import QROutputAbstract from './QROutputAbstract.js';
  8. /**
  9. * Converts the matrix data into string types
  10. */
  11. export default class QRStringText extends QROutputAbstract{
  12. /**
  13. * @inheritDoc
  14. */
  15. mimeType = 'text/plain';
  16. /**
  17. * @inheritDoc
  18. */
  19. getDefaultModuleValue($isDark){
  20. return $isDark ? '██' : '░░';
  21. }
  22. /**
  23. * @inheritDoc
  24. */
  25. prepareModuleValue($value){
  26. return $value;
  27. }
  28. /**
  29. * @inheritDoc
  30. */
  31. static moduleValueIsValid($value){
  32. return typeof $value === 'string';
  33. }
  34. /**
  35. * @inheritDoc
  36. */
  37. dump($file){
  38. let $str = [];
  39. for(let $y = 0; $y < this.moduleCount; $y++){
  40. let $row = [];
  41. for(let $x = 0; $x < this.moduleCount; $x++){
  42. $row.push(this.moduleValues[this.matrix.get($x, $y)]);
  43. }
  44. $str.push($row.join(''));
  45. }
  46. let $data = $str.join(this.options.eol);
  47. this.saveToFile($data, $file);
  48. return $data;
  49. }
  50. /**
  51. *
  52. * @param {string} $str
  53. * @param {number|int} $color
  54. * @param {boolean} $background
  55. * @returns {string}
  56. */
  57. static ansi8($str, $color, $background = null){
  58. $color = Math.max(0, Math.min($color, 255));
  59. return `\x1b[${($background === true ? 48 : 38)};5;${$color}m${$str}\x1b[0m`;
  60. }
  61. }