simple-squiggle

A restricted subset of Squiggle
Log | Files | Refs | README

resize.js (4097B)


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