resize.js 3.7 KB

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