simple-squiggle

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

sort.js (3559B)


      1 "use strict";
      2 
      3 Object.defineProperty(exports, "__esModule", {
      4   value: true
      5 });
      6 exports.createSort = void 0;
      7 
      8 var _array = require("../../utils/array.js");
      9 
     10 var _factory = require("../../utils/factory.js");
     11 
     12 var name = 'sort';
     13 var dependencies = ['typed', 'matrix', 'compare', 'compareNatural'];
     14 var createSort = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
     15   var typed = _ref.typed,
     16       matrix = _ref.matrix,
     17       compare = _ref.compare,
     18       compareNatural = _ref.compareNatural;
     19   var compareAsc = compare;
     20 
     21   var compareDesc = function compareDesc(a, b) {
     22     return -compare(a, b);
     23   };
     24   /**
     25    * Sort the items in a matrix.
     26    *
     27    * Syntax:
     28    *
     29    *    math.sort(x)
     30    *    math.sort(x, compare)
     31    *
     32    * Examples:
     33    *
     34    *    math.sort([5, 10, 1]) // returns [1, 5, 10]
     35    *    math.sort(['C', 'B', 'A', 'D'], math.compareNatural)
     36    *    // returns ['A', 'B', 'C', 'D']
     37    *
     38    *    function sortByLength (a, b) {
     39    *      return a.length - b.length
     40    *    }
     41    *    math.sort(['Langdon', 'Tom', 'Sara'], sortByLength)
     42    *    // returns ['Tom', 'Sara', 'Langdon']
     43    *
     44    * See also:
     45    *
     46    *    filter, forEach, map, compare, compareNatural
     47    *
     48    * @param {Matrix | Array} x    A one dimensional matrix or array to sort
     49    * @param {Function | 'asc' | 'desc' | 'natural'} [compare='asc']
     50    *        An optional _comparator function or name. The function is called as
     51    *        `compare(a, b)`, and must return 1 when a > b, -1 when a < b,
     52    *        and 0 when a == b.
     53    * @return {Matrix | Array} Returns the sorted matrix.
     54    */
     55 
     56 
     57   return typed(name, {
     58     Array: function Array(x) {
     59       _arrayIsVector(x);
     60 
     61       return x.sort(compareAsc);
     62     },
     63     Matrix: function Matrix(x) {
     64       _matrixIsVector(x);
     65 
     66       return matrix(x.toArray().sort(compareAsc), x.storage());
     67     },
     68     'Array, function': function ArrayFunction(x, _comparator) {
     69       _arrayIsVector(x);
     70 
     71       return x.sort(_comparator);
     72     },
     73     'Matrix, function': function MatrixFunction(x, _comparator) {
     74       _matrixIsVector(x);
     75 
     76       return matrix(x.toArray().sort(_comparator), x.storage());
     77     },
     78     'Array, string': function ArrayString(x, order) {
     79       _arrayIsVector(x);
     80 
     81       return x.sort(_comparator(order));
     82     },
     83     'Matrix, string': function MatrixString(x, order) {
     84       _matrixIsVector(x);
     85 
     86       return matrix(x.toArray().sort(_comparator(order)), x.storage());
     87     }
     88   });
     89   /**
     90    * Get the comparator for given order ('asc', 'desc', 'natural')
     91    * @param {'asc' | 'desc' | 'natural'} order
     92    * @return {Function} Returns a _comparator function
     93    */
     94 
     95   function _comparator(order) {
     96     if (order === 'asc') {
     97       return compareAsc;
     98     } else if (order === 'desc') {
     99       return compareDesc;
    100     } else if (order === 'natural') {
    101       return compareNatural;
    102     } else {
    103       throw new Error('String "asc", "desc", or "natural" expected');
    104     }
    105   }
    106   /**
    107    * Validate whether an array is one dimensional
    108    * Throws an error when this is not the case
    109    * @param {Array} array
    110    * @private
    111    */
    112 
    113 
    114   function _arrayIsVector(array) {
    115     if ((0, _array.arraySize)(array).length !== 1) {
    116       throw new Error('One dimensional array expected');
    117     }
    118   }
    119   /**
    120    * Validate whether a matrix is one dimensional
    121    * Throws an error when this is not the case
    122    * @param {Matrix} matrix
    123    * @private
    124    */
    125 
    126 
    127   function _matrixIsVector(matrix) {
    128     if (matrix.size().length !== 1) {
    129       throw new Error('One dimensional matrix expected');
    130     }
    131   }
    132 });
    133 exports.createSort = createSort;