UNPKG

4.71 kBJavaScriptView Raw
1'use strict';
2
3/**
4 * Guesses object's type
5 * @memberOf module:node-object-hash/objectSorter
6 * @inner
7 * @private
8 * @param {Object} obj Object to guess type
9 * @returns {string} Object type
10 * @example
11 * var a = [];
12 * _guessObjectType(a) === 'array'; // true
13 */
14function _guessObjectType(obj) {
15 var hasMapSet = typeof Map !== 'undefined';
16
17 if (obj === null) {
18 return 'null';
19 }
20 if (Array.isArray(obj)) {
21 return 'array';
22 }
23 if (hasMapSet && (obj instanceof Map || obj instanceof WeakMap)) {
24 return 'map';
25 }
26 if (hasMapSet && (obj instanceof Set || obj instanceof WeakSet)) {
27 return 'set';
28 }
29 if (obj instanceof Date) {
30 return 'date';
31 }
32 return 'object';
33}
34
35/**
36 * Guesses variable type
37 * @memberOf module:node-object-hash/objectSorter
38 * @inner
39 * @private
40 * @param {*} obj Variable to guess type
41 * @returns {string} Variable type
42 * @example
43 * var a = '';
44 * _guessType(a) === 'string'; // true
45 */
46function _guessType(obj) {
47 var type = typeof obj;
48
49 return type !== 'object' ? type : _guessObjectType(obj);
50}
51
52/**
53 * Creates object sorter function
54 * @memberOf module:node-object-hash/objectSorter
55 * @inner
56 * @private
57 * @param {Object} [options] Sorter options
58 * @param {boolean} [options.coerce=true] Performs type coercion
59 * @param {boolean} [options.sort=true] Performs array, object, etc. sorting
60 * @returns {module:node-object-hash/objectSorter~makeObjectSorter~objectToString}
61 * Object sorting function
62 * @example
63 * // with coercion
64 * var sorter = makeObjectSorter({coerce: true, sort: false});
65 * sorter(1) === "1"; // true
66 * // with sort
67 * var sorter = makeObjectSorter({coerce: false, sort: true});
68 * sorter([2, 3, 1]) === [1, 2, 3]; // true
69 */
70function makeObjectSorter(options) {
71 options = options || {};
72 var coerce = typeof options.coerce === 'undefined' ? true : options.coerce,
73 sort = typeof options.sort === 'undefined' ? true : options.sort,
74 self = {};
75
76 self.string = function sortString(obj) {
77 if (coerce) {
78 return obj;
79 }
80 return '<:s>:' + obj;
81 };
82
83 self.number = function sortNumber(obj) {
84 if (coerce) {
85 return obj.toString();
86 }
87 return '<:n>:' + obj;
88 };
89
90 self.boolean = function sortBoolean(obj) {
91 if (coerce) {
92 return obj ? '1' : '0';
93 }
94 return obj ? '<:b>:true' : '<:b>:false';
95 };
96
97 self.symbol = function sortSymbol() {
98 return '<:smbl>';
99 };
100
101 self.undefined = function sortUndefined() {
102 if (coerce) {
103 return '';
104 }
105 return '<:undf>';
106 };
107
108 self.null = function sortNull() {
109 if (coerce) {
110 return '';
111 }
112 return '<:null>';
113 };
114
115 self.function = function sortFunction(obj) {
116 if (coerce) {
117 return obj.name + '=>' + obj.toString();
118 }
119 return '<:func>:' + obj.name + '=>' + obj.toString();
120 };
121
122 self.array = function sortArray(obj) {
123 var item,
124 itemType,
125 result = [];
126
127 for (var i = 0; i < obj.length; i++) {
128 item = obj[i];
129 itemType = _guessType(item);
130 result.push(self[itemType](item));
131 }
132
133 return sort ? '[' + result.sort().toString() + ']' : '[' + result.toString() + ']';
134 };
135
136 self.set = function sortSet(obj) {
137 return self.array(Array.from(obj));
138 };
139
140 self.date = function sortDate(obj) {
141 var dateStr = obj.toISOString();
142
143 if (coerce) {
144 return dateStr;
145 }
146 return '<:date>:' + dateStr;
147 };
148
149 self.object = function sortObject(obj) {
150 var keys = sort ? Object.keys(obj).sort() : Object.keys(obj),
151 objArray = [],
152 key, value, valueType,
153 i;
154
155 for (i = 0; i < keys.length; i++) {
156 key = keys[i];
157 value = obj[key];
158 valueType = _guessType(value);
159 objArray.push(key + ':' + self[valueType](value));
160 }
161 return '{' + objArray.toString() + '}';
162 };
163
164 self.map = function sortMap(obj) {
165 var arr = Array.from(obj),
166 key, value, item,
167 i;
168
169 for (i = 0; i < arr.length; i++) {
170 item = arr[i];
171 key = item[0];
172 value = item[1];
173 item = [
174 self[_guessType(key)](key),
175 self[_guessType(value)](value)
176 ];
177 arr[i] = item;
178 }
179
180 return sort ? '[' + arr.sort().join(';') + ']' : '[' + arr.join(';') + ']';
181 };
182
183 /**
184 * Object sorting function
185 * @private
186 * @param {Object} obj Object to sort
187 * @returns {string} Sorted string
188 */
189 function objectToString(obj) {
190 return self[_guessType(obj)](obj);
191 }
192
193 return objectToString;
194}
195
196/**
197 * Object sorter module.
198 * It provides object sorter function constructor.
199 * @module node-object-hash/objectSorter
200 * @type {module:node-object-hash/objectSorter~makeObjectSorter~objectToString}
201 */
202module.exports = makeObjectSorter;