[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/yuilib/3.17.2/arraysort/ -> arraysort.js (source)

   1  /*
   2  YUI 3.17.2 (build 9c3c78e)
   3  Copyright 2014 Yahoo! Inc. All rights reserved.
   4  Licensed under the BSD License.
   5  http://yuilibrary.com/license/
   6  */
   7  
   8  YUI.add('arraysort', function (Y, NAME) {
   9  
  10  /*jshint expr:true, onevar:false */
  11  
  12  /**
  13  Provides comparator functions useful for sorting arrays.
  14  
  15  @module arraysort
  16  **/
  17  
  18  var LANG = Y.Lang,
  19      ISVALUE = LANG.isValue,
  20      ISSTRING = LANG.isString;
  21  
  22  /**
  23  Provides comparator functions useful for sorting arrays.
  24  
  25  @class ArraySort
  26  @static
  27  **/
  28  
  29  var ArraySort = Y.ArraySort = {
  30      // -- Public Methods -------------------------------------------------------
  31  
  32      /**
  33      Comparator function for simple case-insensitive sorting of an array of
  34      strings.
  35  
  36      @method compare
  37      @param a {Object} First sort argument.
  38      @param b {Object} Second sort argument.
  39      @param desc {Boolean} `true` if sort direction is descending, `false` if
  40          sort direction is ascending.
  41      @return {Boolean} -1 when a < b. 0 when a == b. 1 when a > b.
  42      @static
  43      */
  44      compare: function(a, b, desc) {
  45          if(!ISVALUE(a)) {
  46              if(!ISVALUE(b)) {
  47                  return 0;
  48              }
  49              else {
  50                  return 1;
  51              }
  52          }
  53          else if(!ISVALUE(b)) {
  54              return -1;
  55          }
  56  
  57          if(ISSTRING(a)) {
  58              a = a.toLowerCase();
  59          }
  60          if(ISSTRING(b)) {
  61              b = b.toLowerCase();
  62          }
  63          if(a < b) {
  64              return (desc) ? 1 : -1;
  65          }
  66          else if (a > b) {
  67              return (desc) ? -1 : 1;
  68          }
  69          else {
  70              return 0;
  71          }
  72      },
  73  
  74      /**
  75      Performs a natural-order comparison of two strings or numbers (or a string
  76      and a number). This ensures that a value like 'foo2' will be sorted before
  77      'foo10', whereas a standard ASCII sort would sort 'foo10' first.
  78  
  79      @example
  80  
  81          var items = ['item10', 'item2', 'item1', 10, '1', 2];
  82  
  83          items.sort(Y.ArraySort.naturalCompare);
  84          console.log(items); // => ['1', 2, 10, 'item1', 'item2', 'item10']
  85  
  86      @method naturalCompare
  87      @param {Number|String} a First value to compare.
  88      @param {Number|String} b Second value to compare.
  89      @param {Object} [options] Options.
  90          @param {Boolean} [options.caseSensitive=false] If `true`, a
  91              case-sensitive comparison will be performed. By default the
  92              comparison is case-insensitive.
  93          @param {Boolean} [options.descending=false] If `true`, the sort order
  94              will be reversed so that larger values are sorted before smaller
  95              values.
  96      @return {Number} `0` if the two items are equal, a negative number if _a_
  97          should be sorted before _b_, or a positive number if _b_ should be
  98          sorted before _a_.
  99      @static
 100      @since 3.11.0
 101      **/
 102      naturalCompare: function (a, b, options) {
 103          // Coerce `a` and `b` to strings.
 104          a += '';
 105          b += '';
 106  
 107          // Convert `a` and `b` to lowercase unless `options.caseSensitive` is
 108          // truthy.
 109          if (!options || !options.caseSensitive) {
 110              a = a.toLowerCase();
 111              b = b.toLowerCase();
 112          }
 113  
 114          // Split `a` and `b` into alpha parts and numeric parts.
 115          var aParts = ArraySort._splitAlphaNum(a),
 116              bParts = ArraySort._splitAlphaNum(b),
 117              length = Math.min(aParts.length, bParts.length),
 118              result = 0,
 119  
 120              aPart,
 121              bPart,
 122              i;
 123  
 124          // Compare each part of `a` with each part of `b`.
 125          for (i = 0; i < length; i++) {
 126              aPart = aParts[i];
 127              bPart = bParts[i];
 128  
 129              // If the two parts aren't equal, compare them and stop iterating.
 130              if (aPart !== bPart) {
 131                  // First, try comparing them as numbers.
 132                  result = aPart - bPart;
 133  
 134                  // If that didn't work, compare them as strings. This falsiness
 135                  // check works because `result` can't be 0 (we checked for
 136                  // equality above) and NaN is falsy.
 137                  if (!result) {
 138                      result = aPart > bPart ? 1 : -1;
 139                  }
 140  
 141                  // At this point we know enough to be able to sort the two
 142                  // strings, so we don't need to compare any more parts.
 143                  break;
 144              }
 145          }
 146  
 147          // If we get here and `result` is still 0, then sort the shorter string
 148          // before the longer string.
 149          result || (result = a.length - b.length);
 150  
 151          // Return the result, flipping the order if `options.descending` is
 152          // truthy.
 153          return options && options.descending ? -result : result;
 154      },
 155  
 156      // -- Protected Methods ----------------------------------------------------
 157  
 158      /**
 159      Splits a string into an array of alpha character and digit character parts.
 160  
 161      @example
 162  
 163          Y.ArraySort._splitAlphaNum('abc123def456');
 164          // => ['abc', '123', 'def', '456']
 165  
 166      @method _splitAlphaNum
 167      @param {String} string String to split.
 168      @return {String[]} Array of alpha parts and digit parts.
 169      @protected
 170      @static
 171      @since 3.11.0
 172      **/
 173      _splitAlphaNum: function (string) {
 174          /*jshint boss:true */
 175          var parts = [],
 176              regex = /(\d+|\D+)/g,
 177              match;
 178  
 179          while (match = regex.exec(string)) { // assignment
 180              parts.push(match[1]);
 181          }
 182  
 183          return parts;
 184      }
 185  };
 186  
 187  
 188  }, '3.17.2', {"requires": ["yui-base"]});


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1