resize.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createResize = void 0;
  6. var _is = require("../../utils/is.js");
  7. var _DimensionError = require("../../error/DimensionError.js");
  8. var _ArgumentsError = require("../../error/ArgumentsError.js");
  9. var _number = require("../../utils/number.js");
  10. var _string = require("../../utils/string.js");
  11. var _object = require("../../utils/object.js");
  12. var _array = require("../../utils/array.js");
  13. var _factory = require("../../utils/factory.js");
  14. var name = 'resize';
  15. var dependencies = ['config', 'matrix'];
  16. var createResize = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
  17. var config = _ref.config,
  18. matrix = _ref.matrix;
  19. /**
  20. * Resize a matrix
  21. *
  22. * Syntax:
  23. *
  24. * math.resize(x, size)
  25. * math.resize(x, size, defaultValue)
  26. *
  27. * Examples:
  28. *
  29. * math.resize([1, 2, 3, 4, 5], [3]) // returns Array [1, 2, 3]
  30. * math.resize([1, 2, 3], [5], 0) // returns Array [1, 2, 3, 0, 0]
  31. * math.resize(2, [2, 3], 0) // returns Matrix [[2, 0, 0], [0, 0, 0]]
  32. * math.resize("hello", [8], "!") // returns string 'hello!!!'
  33. *
  34. * See also:
  35. *
  36. * size, squeeze, subset, reshape
  37. *
  38. * @param {Array | Matrix | *} x Matrix to be resized
  39. * @param {Array | Matrix} size One dimensional array with numbers
  40. * @param {number | string} [defaultValue=0] Zero by default, except in
  41. * case of a string, in that case
  42. * defaultValue = ' '
  43. * @return {* | Array | Matrix} A resized clone of matrix `x`
  44. */
  45. // TODO: rework resize to a typed-function
  46. return function resize(x, size, defaultValue) {
  47. if (arguments.length !== 2 && arguments.length !== 3) {
  48. throw new _ArgumentsError.ArgumentsError('resize', arguments.length, 2, 3);
  49. }
  50. if ((0, _is.isMatrix)(size)) {
  51. size = size.valueOf(); // get Array
  52. }
  53. if ((0, _is.isBigNumber)(size[0])) {
  54. // convert bignumbers to numbers
  55. size = size.map(function (value) {
  56. return !(0, _is.isBigNumber)(value) ? value : value.toNumber();
  57. });
  58. }
  59. // check x is a Matrix
  60. if ((0, _is.isMatrix)(x)) {
  61. // use optimized matrix implementation, return copy
  62. return x.resize(size, defaultValue, true);
  63. }
  64. if (typeof x === 'string') {
  65. // resize string
  66. return _resizeString(x, size, defaultValue);
  67. }
  68. // check result should be a matrix
  69. var asMatrix = Array.isArray(x) ? false : config.matrix !== 'Array';
  70. if (size.length === 0) {
  71. // output a scalar
  72. while (Array.isArray(x)) {
  73. x = x[0];
  74. }
  75. return (0, _object.clone)(x);
  76. } else {
  77. // output an array/matrix
  78. if (!Array.isArray(x)) {
  79. x = [x];
  80. }
  81. x = (0, _object.clone)(x);
  82. var res = (0, _array.resize)(x, size, defaultValue);
  83. return asMatrix ? matrix(res) : res;
  84. }
  85. };
  86. /**
  87. * Resize a string
  88. * @param {string} str
  89. * @param {number[]} size
  90. * @param {string} [defaultChar=' ']
  91. * @private
  92. */
  93. function _resizeString(str, size, defaultChar) {
  94. if (defaultChar !== undefined) {
  95. if (typeof defaultChar !== 'string' || defaultChar.length !== 1) {
  96. throw new TypeError('Single character expected as defaultValue');
  97. }
  98. } else {
  99. defaultChar = ' ';
  100. }
  101. if (size.length !== 1) {
  102. throw new _DimensionError.DimensionError(size.length, 1);
  103. }
  104. var len = size[0];
  105. if (typeof len !== 'number' || !(0, _number.isInteger)(len)) {
  106. throw new TypeError('Invalid size, must contain positive integers ' + '(size: ' + (0, _string.format)(size) + ')');
  107. }
  108. if (str.length > len) {
  109. return str.substring(0, len);
  110. } else if (str.length < len) {
  111. var res = str;
  112. for (var i = 0, ii = len - str.length; i < ii; i++) {
  113. res += defaultChar;
  114. }
  115. return res;
  116. } else {
  117. return str;
  118. }
  119. }
  120. });
  121. exports.createResize = createResize;