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