保誠-保戶業務員媒合平台
HelenHuang
2022-06-09 9bdb95c9e34cef640534e5e5a1e2225a80442000
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// / <reference types="node" />
'use strict';
 
/**
 * Guesses object's type
 * @memberOf module:node-object-hash/objectSorter
 * @inner
 * @private
 * @param {Object} obj Object to guess type
 * @return {string} Object type
 * @example
 * var a = [];
 * _guessObjectType(a) === 'array'; // true
 */
function _guessObjectType(obj) {
  if (obj === null) {
    return 'null';
  }
 
  switch (obj.constructor && obj.constructor.name) {
  case 'Array':
  case 'Int8Array':
  case 'Uint8Array':
  case 'Uint8ClampedArray':
  case 'Int16Array':
  case 'Uint16Array':
  case 'Int32Array':
  case 'Uint32Array':
  case 'Float32Array':
  case 'Float64Array':
  case 'Buffer':
    return 'array';
  case 'Map':
    return 'map';
  case 'Set':
    return 'set';
  case 'Date':
    return 'date';
  case 'String':
    return 'string';
  case 'Number':
    return 'number';
  case 'Boolean':
    return 'boolean';
  case 'Object':
    return 'object';
  default:
    return 'unknown';
  }
}
 
/**
 * Guesses variable type
 * @memberOf module:node-object-hash/objectSorter
 * @inner
 * @private
 * @param {*} obj Variable to guess type
 * @return {string} Variable type
 * @example
 * var a = '';
 * _guessType(a) === 'string'; // true
 */
function _guessType(obj) {
  var type = typeof obj;
 
  return type !== 'object' ? type : _guessObjectType(obj);
}
 
/**
 * Creates object sorter function
 * @memberOf module:node-object-hash/objectSorter
 * @inner
 * @private
 * @param {Object} [options] Sorter options
 * @param {boolean} [options.coerce=true] Performs type coercion
 * @param {boolean} [options.sort=true] Performs array, object, etc. sorting
 * @return {module:node-object-hash/objectSorter~makeObjectSorter~objectToString}
 * Object sorting function
 * @example
 * // with coercion
 * var sorter = makeObjectSorter({coerce: true, sort: false});
 * sorter(1) === "1"; // true
 * // with sort
 * var sorter = makeObjectSorter({coerce: false, sort: true});
 * sorter([2, 3, 1]) === [1, 2, 3]; // true
 */
function makeObjectSorter(options) {
  options = options || {};
  var coerce = typeof options.coerce === 'undefined' ? true : options.coerce;
  var sort = typeof options.sort === 'undefined' ? true : options.sort;
  var stringifier = {};
 
  stringifier.string = function sortString(obj) {
    if (coerce) {
      return obj;
    }
    return '<:s>:' + obj;
  };
 
  stringifier.number = function sortNumber(obj) {
    if (coerce) {
      return obj.toString();
    }
    return '<:n>:' + obj;
  };
 
  stringifier.boolean = function sortBoolean(obj) {
    if (coerce) {
      return obj.valueOf() ? '1' : '0';
    }
    return obj.valueOf() ? '<:b>:true' : '<:b>:false';
  };
 
  stringifier.symbol = function sortSymbol() {
    return '<:smbl>';
  };
 
  stringifier.undefined = function sortUndefined() {
    if (coerce) {
      return '';
    }
    return '<:undf>';
  };
 
  stringifier.null = function sortNull() {
    if (coerce) {
      return '';
    }
    return '<:null>';
  };
 
  stringifier.function = function sortFunction(obj) {
    if (coerce) {
      return obj.name + '=>' + obj.toString();
    }
    return '<:func>:' + obj.name + '=>' + obj.toString();
  };
 
  stringifier.array = function sortArray(obj) {
    var item;
    var itemType;
    var result = [];
 
    for (var i = 0; i < obj.length; i++) {
      item = obj[i];
      itemType = _guessType(item);
      result.push(stringifier[itemType](item));
    }
 
    return sort ? '[' + result.sort().toString() + ']' : '[' + result.toString() + ']';
  };
 
  stringifier.set = function sortSet(obj) {
    return stringifier.array(Array.from(obj));
  };
 
  stringifier.date = function sortDate(obj) {
    var dateStr = obj.toISOString();
 
    if (coerce) {
      return dateStr;
    }
    return '<:date>:' + dateStr;
  };
 
  stringifier.object = function sortObject(obj) {
    var keys = sort ? Object.keys(obj).sort() : Object.keys(obj);
    var objArray = [];
    var key;
    var value;
    var valueType;
    var i;
 
    for (i = 0; i < keys.length; i++) {
      key = keys[i];
      value = obj[key];
      valueType = _guessType(value);
      objArray.push(key + ':' + stringifier[valueType](value));
    }
    return '{' + objArray.toString() + '}';
  };
 
  stringifier.map = function sortMap(obj) {
    var arr = Array.from(obj);
    var key;
    var value;
    var item;
    var i;
 
    for (i = 0; i < arr.length; i++) {
      item = arr[i];
      key = item[0];
      value = item[1];
      item = [stringifier[_guessType(key)](key), stringifier[_guessType(value)](value)];
      arr[i] = item;
    }
 
    return sort ? '[' + arr.sort().join(';') + ']' : '[' + arr.join(';') + ']';
  };
 
  stringifier.unknown = function unknownToString(obj) {
    var constructorName = obj.constructor ? obj.constructor.name : 'unknonw';
    var objectName = typeof obj.toString === 'function' ? obj.toString() : 'unknown';
 
    return '<:' + constructorName + '>:' + objectName;
  };
 
  /**
   * Object sorting function
   * @private
   * @param {Object} obj Object to sort
   * @return {string} Sorted string
   */
  function objectToString(obj) {
    return stringifier[_guessType(obj)](obj);
  }
 
  return objectToString;
}
 
/**
 * Object sorter module.
 * It provides object sorter function constructor.
 * @module node-object-hash/objectSorter
 * @type {module:node-object-hash/objectSorter~makeObjectSorter~objectToString}
 */
module.exports = makeObjectSorter;