all files / node-object-hash/ objectSorter.js

100% Statements 80/80
100% Branches 48/48
100% Functions 16/16
100% Lines 80/80
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171                    294×   294× 42×   252× 42×   210× 42×   168× 42×   126× 42×   84×               1386×   1386×                         113× 91×   22×     785× 497×   288×     68× 56×   12×     42×     42× 28×   14×     42× 28×   14×     42× 28×   14×     84×       84× 462× 462× 462×     84×     42×     42×   42× 28×   14×     84×         84× 630× 630× 630× 630×   84×     42×       42× 126× 126× 126× 126×       126×     42×     42×          
/**
 * Created on 8/22/16.
 */
'use strict';
 
/**
 * Guesses object's type
 * @param {Object} obj Object
 * @returns {string}
 */
function guessObjectType(obj) {
  var hasMapSet = typeof Map !== 'undefined';
 
  if (obj === null) {
    return 'null';
  }
  if (Array.isArray(obj)) {
    return 'array';
  }
  if (hasMapSet && (obj instanceof Map || obj instanceof WeakMap)) {
    return 'map';
  }
  if (hasMapSet && (obj instanceof Set || obj instanceof WeakSet)) {
    return 'set';
  }
  if (obj instanceof Date) {
    return 'date';
  }
  return 'object';
}
 
/**
 * Guesses variable type
 * @param {*} obj
 * @returns {string}
 */
function guessType(obj) {
  var type = typeof obj;
 
  return type !== 'object' ? type : guessObjectType(obj);
}
 
/**
 * Creates object sorter function
 * @param {Object} [options] Sorter options
 * @param {boolean} [options.coerce="true"] Performs type coercion (e.g sorter(1) === ("1"))
 * @param {boolean} [options.sort="true"] Performs array, object, etc. sorting
 * @returns {string} Sorted object string
 */
function objectSorter(options) {
  options = options || {};
  var coerce = typeof options.coerce === 'undefined' ? true : options.coerce,
      sort = typeof options.sort === 'undefined' ? true : options.sort,
      self = {};
 
  self.string = function sortString(obj) {
    if (coerce) {
      return obj;
    }
    return '<:s>:' + obj;
  };
 
  self.number = function sortNumber(obj) {
    if (coerce) {
      return obj.toString();
    }
    return '<:n>:' + obj;
  };
 
  self.boolean = function sortBoolean(obj) {
    if (coerce) {
      return obj ? '1' : '0';
    }
    return obj ? '<:b>:true' : '<:b>:false';
  };
 
  self.symbol = function sortSymbol() {
    return '<:smbl>';
  };
 
  self.undefined = function sortUndefined() {
    if (coerce) {
      return '';
    }
    return '<:undf>';
  };
 
  self.null = function sortNull() {
    if (coerce) {
      return '';
    }
    return '<:null>';
  };
 
  self.function = function sortFunction(obj) {
    if (coerce) {
      return obj.name + '=>' + obj.toString();
    }
    return '<:func>:' + obj.name + '=>' + obj.toString();
  };
 
  self.array = function sortArray(obj) {
    var item,
        itemType,
        result = [];
 
    for (var i = 0; i < obj.length; i++) {
      item = obj[i];
      itemType = guessType(item);
      result.push(self[itemType](item));
    }
 
    return sort ? '[' + result.sort().toString() + ']' : '[' + result.toString() + ']';
  };
 
  self.set = function sortSet(obj) {
    return self.array(Array.from(obj));
  };
 
  self.date = function sortDate(obj) {
    var dateStr = obj.toISOString();
 
    if (coerce) {
      return dateStr;
    }
    return '<:date>:' + dateStr;
  };
 
  self.object = function sortObject(obj) {
    var keys = sort ? Object.keys(obj).sort() : Object.keys(obj),
        objArray = [],
        key, value, valueType,
        i;
 
    for (i = 0; i < keys.length; i++) {
      key = keys[i];
      value = obj[key];
      valueType = guessType(value);
      objArray.push(key + ':' + self[valueType](value));
    }
    return '{' + objArray.toString() + '}';
  };
 
  self.map = function sortMap(obj) {
    var arr = Array.from(obj),
        key, value, item,
        i;
 
    for (i = 0; i < arr.length; i++) {
      item = arr[i];
      key = item[0];
      value = item[1];
      item = [
        self[guessType(key)](key),
        self[guessType(value)](value)
      ];
      arr[i] = item;
    }
 
    return sort ? '[' + arr.sort().join(';') + ']' : '[' + arr.join(';') + ']';
  };
 
  function obj2string(obj) {
    return self[guessType(obj)](obj);
  }
 
  return obj2string;
}
 
module.exports = objectSorter;