1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | function _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 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | function _guessType(obj) {
|
47 | var type = typeof obj;
|
48 |
|
49 | return type !== 'object' ? type : _guessObjectType(obj);
|
50 | }
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | function 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 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 | function objectToString(obj) {
|
190 | return self[_guessType(obj)](obj);
|
191 | }
|
192 |
|
193 | return objectToString;
|
194 | }
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 | module.exports = makeObjectSorter;
|