protobufjs 3.6.0 → 3.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/bin/proto2js +16 -4
  2. package/bower.json +1 -1
  3. package/dist/ProtoBuf.js +645 -441
  4. package/dist/ProtoBuf.min.js +96 -96
  5. package/dist/ProtoBuf.min.js.gz +0 -0
  6. package/dist/ProtoBuf.min.map +2 -2
  7. package/dist/ProtoBuf.noparse.js +520 -357
  8. package/dist/ProtoBuf.noparse.min.js +68 -66
  9. package/dist/ProtoBuf.noparse.min.js.gz +0 -0
  10. package/dist/ProtoBuf.noparse.min.map +3 -3
  11. package/docs/ProtoBuf.Builder.Message.html +673 -135
  12. package/docs/ProtoBuf.Builder.Service.html +58 -19
  13. package/docs/ProtoBuf.Builder.html +374 -46
  14. package/docs/ProtoBuf.DotProto.Parser.html +58 -19
  15. package/docs/ProtoBuf.DotProto.Tokenizer.html +104 -19
  16. package/docs/ProtoBuf.DotProto.html +16 -10
  17. package/docs/ProtoBuf.Reflect.Enum.Value.html +207 -33
  18. package/docs/ProtoBuf.Reflect.Enum.html +318 -56
  19. package/docs/ProtoBuf.Reflect.Extension.html +53 -13
  20. package/docs/ProtoBuf.Reflect.Message.ExtensionField.html +450 -68
  21. package/docs/ProtoBuf.Reflect.Message.Field.html +454 -51
  22. package/docs/ProtoBuf.Reflect.Message.OneOf.html +1044 -0
  23. package/docs/ProtoBuf.Reflect.Message.html +378 -64
  24. package/docs/ProtoBuf.Reflect.Namespace.html +333 -51
  25. package/docs/ProtoBuf.Reflect.Service.Method.html +228 -35
  26. package/docs/ProtoBuf.Reflect.Service.RPCMethod.html +262 -41
  27. package/docs/ProtoBuf.Reflect.Service.html +318 -56
  28. package/docs/ProtoBuf.Reflect.T.html +210 -25
  29. package/docs/ProtoBuf.Reflect.html +17 -11
  30. package/docs/ProtoBuf.Util.html +59 -13
  31. package/docs/ProtoBuf.html +235 -67
  32. package/docs/ProtoBuf.js.html +648 -443
  33. package/docs/index.html +4 -2
  34. package/docs/styles/jsdoc-default.css +2 -2
  35. package/examples/protoify/.npmignore +2 -0
  36. package/examples/protoify/README.md +28 -0
  37. package/examples/protoify/index.js +147 -0
  38. package/examples/protoify/json.js +123 -0
  39. package/examples/protoify/json.json +123 -0
  40. package/examples/protoify/json.proto +30 -0
  41. package/examples/protoify/package.json +15 -0
  42. package/examples/protoify/test.js +56 -0
  43. package/examples/websocket/README.md +1 -0
  44. package/examples/websocket/package.json +1 -1
  45. package/externs/ProtoBuf.js +922 -922
  46. package/package.json +3 -3
  47. package/scripts/build.js +58 -58
  48. package/src/ProtoBuf/Builder/Message.js +107 -77
  49. package/src/ProtoBuf/Builder/Service.js +8 -5
  50. package/src/ProtoBuf/Builder.js +71 -37
  51. package/src/ProtoBuf/DotProto/Parser.js +108 -72
  52. package/src/ProtoBuf/DotProto/Tokenizer.js +17 -11
  53. package/src/ProtoBuf/Lang.js +1 -1
  54. package/src/ProtoBuf/Reflect/Enum/Value.js +3 -2
  55. package/src/ProtoBuf/Reflect/Enum.js +9 -5
  56. package/src/ProtoBuf/Reflect/Extension.js +4 -3
  57. package/src/ProtoBuf/Reflect/Message/ExtensionField.js +4 -3
  58. package/src/ProtoBuf/Reflect/Message/Field.js +45 -26
  59. package/src/ProtoBuf/Reflect/Message/OneOf.js +19 -0
  60. package/src/ProtoBuf/Reflect/Message.js +36 -19
  61. package/src/ProtoBuf/Reflect/Namespace.js +19 -15
  62. package/src/ProtoBuf/Reflect/Service/Method.js +9 -5
  63. package/src/ProtoBuf/Reflect/Service/RPCMethod.js +3 -2
  64. package/src/ProtoBuf/Reflect/Service.js +9 -5
  65. package/src/ProtoBuf/Reflect/T.js +20 -6
  66. package/src/ProtoBuf/Reflect.js +9 -0
  67. package/src/ProtoBuf/Util.js +132 -132
  68. package/src/ProtoBuf.js +15 -17
  69. package/src/google/protobuf/descriptor.json +33 -13
  70. package/tests/bench.txt +373 -373
  71. package/tests/complex.json +8 -4
  72. package/tests/custom-options.json +169 -169
  73. package/tests/extend.json +71 -71
  74. package/tests/nodeunit-browser/nodeunit.css +70 -70
  75. package/tests/nodeunit-browser/nodeunit.js +2108 -2108
  76. package/tests/oneof.proto +6 -0
  77. package/tests/options.json +32 -32
  78. package/tests/options.proto +2 -0
  79. package/tests/proto2js/Bar.json +46 -46
  80. package/tests/suite.js +83 -12
  81. package/.idea/.name +0 -1
  82. package/.idea/ProtoBuf.iml +0 -9
  83. package/.idea/dictionaries/Daniel.xml +0 -7
  84. package/.idea/encodings.xml +0 -5
  85. package/.idea/misc.xml +0 -5
  86. package/.idea/modules.xml +0 -9
  87. package/.idea/scopes/scope_settings.xml +0 -5
  88. package/.idea/vcs.xml +0 -7
  89. package/.idea/workspace.xml +0 -551
  90. package/NOTICE +0 -2
  91. package/sandbox/issue146/MyOptions.proto +0 -28
  92. package/sandbox/issue146/Sample.proto +0 -21
  93. package/sandbox/issue146/main.js +0 -3
  94. package/sandbox/issue147/enum.proto +0 -8
  95. package/sandbox/issue147/main.js +0 -3
  96. package/sandbox/issue42/innerextend.proto +0 -18
  97. package/sandbox/issue42/main.js +0 -8
  98. package/sandbox/issue42/outerextend.proto +0 -17
  99. package/v8.log +0 -3828
@@ -1,2108 +1,2108 @@
1
- /*!
2
- * Nodeunit
3
- * https://github.com/caolan/nodeunit
4
- * Copyright (c) 2010 Caolan McMahon
5
- * MIT Licensed
6
- *
7
- * json2.js
8
- * http://www.JSON.org/json2.js
9
- * Public Domain.
10
- * NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
11
- */
12
- nodeunit = (function(){
13
- /*
14
- http://www.JSON.org/json2.js
15
- 2010-11-17
16
-
17
- Public Domain.
18
-
19
- NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
20
-
21
- See http://www.JSON.org/js.html
22
-
23
-
24
- This code should be minified before deployment.
25
- See http://javascript.crockford.com/jsmin.html
26
-
27
- USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO
28
- NOT CONTROL.
29
-
30
-
31
- This file creates a global JSON object containing two methods: stringify
32
- and parse.
33
-
34
- JSON.stringify(value, replacer, space)
35
- value any JavaScript value, usually an object or array.
36
-
37
- replacer an optional parameter that determines how object
38
- values are stringified for objects. It can be a
39
- function or an array of strings.
40
-
41
- space an optional parameter that specifies the indentation
42
- of nested structures. If it is omitted, the text will
43
- be packed without extra whitespace. If it is a number,
44
- it will specify the number of spaces to indent at each
45
- level. If it is a string (such as '\t' or ' '),
46
- it contains the characters used to indent at each level.
47
-
48
- This method produces a JSON text from a JavaScript value.
49
-
50
- When an object value is found, if the object contains a toJSON
51
- method, its toJSON method will be called and the result will be
52
- stringified. A toJSON method does not serialize: it returns the
53
- value represented by the name/value pair that should be serialized,
54
- or undefined if nothing should be serialized. The toJSON method
55
- will be passed the key associated with the value, and this will be
56
- bound to the value
57
-
58
- For example, this would serialize Dates as ISO strings.
59
-
60
- Date.prototype.toJSON = function (key) {
61
- function f(n) {
62
- // Format integers to have at least two digits.
63
- return n < 10 ? '0' + n : n;
64
- }
65
-
66
- return this.getUTCFullYear() + '-' +
67
- f(this.getUTCMonth() + 1) + '-' +
68
- f(this.getUTCDate()) + 'T' +
69
- f(this.getUTCHours()) + ':' +
70
- f(this.getUTCMinutes()) + ':' +
71
- f(this.getUTCSeconds()) + 'Z';
72
- };
73
-
74
- You can provide an optional replacer method. It will be passed the
75
- key and value of each member, with this bound to the containing
76
- object. The value that is returned from your method will be
77
- serialized. If your method returns undefined, then the member will
78
- be excluded from the serialization.
79
-
80
- If the replacer parameter is an array of strings, then it will be
81
- used to select the members to be serialized. It filters the results
82
- such that only members with keys listed in the replacer array are
83
- stringified.
84
-
85
- Values that do not have JSON representations, such as undefined or
86
- functions, will not be serialized. Such values in objects will be
87
- dropped; in arrays they will be replaced with null. You can use
88
- a replacer function to replace those with JSON values.
89
- JSON.stringify(undefined) returns undefined.
90
-
91
- The optional space parameter produces a stringification of the
92
- value that is filled with line breaks and indentation to make it
93
- easier to read.
94
-
95
- If the space parameter is a non-empty string, then that string will
96
- be used for indentation. If the space parameter is a number, then
97
- the indentation will be that many spaces.
98
-
99
- Example:
100
-
101
- text = JSON.stringify(['e', {pluribus: 'unum'}]);
102
- // text is '["e",{"pluribus":"unum"}]'
103
-
104
-
105
- text = JSON.stringify(['e', {pluribus: 'unum'}], null, '\t');
106
- // text is '[\n\t"e",\n\t{\n\t\t"pluribus": "unum"\n\t}\n]'
107
-
108
- text = JSON.stringify([new Date()], function (key, value) {
109
- return this[key] instanceof Date ?
110
- 'Date(' + this[key] + ')' : value;
111
- });
112
- // text is '["Date(---current time---)"]'
113
-
114
-
115
- JSON.parse(text, reviver)
116
- This method parses a JSON text to produce an object or array.
117
- It can throw a SyntaxError exception.
118
-
119
- The optional reviver parameter is a function that can filter and
120
- transform the results. It receives each of the keys and values,
121
- and its return value is used instead of the original value.
122
- If it returns what it received, then the structure is not modified.
123
- If it returns undefined then the member is deleted.
124
-
125
- Example:
126
-
127
- // Parse the text. Values that look like ISO date strings will
128
- // be converted to Date objects.
129
-
130
- myData = JSON.parse(text, function (key, value) {
131
- var a;
132
- if (typeof value === 'string') {
133
- a =
134
- /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value);
135
- if (a) {
136
- return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4],
137
- +a[5], +a[6]));
138
- }
139
- }
140
- return value;
141
- });
142
-
143
- myData = JSON.parse('["Date(09/09/2001)"]', function (key, value) {
144
- var d;
145
- if (typeof value === 'string' &&
146
- value.slice(0, 5) === 'Date(' &&
147
- value.slice(-1) === ')') {
148
- d = new Date(value.slice(5, -1));
149
- if (d) {
150
- return d;
151
- }
152
- }
153
- return value;
154
- });
155
-
156
-
157
- This is a reference implementation. You are free to copy, modify, or
158
- redistribute.
159
- */
160
-
161
- /*jslint evil: true, strict: false, regexp: false */
162
-
163
- /*members "", "\b", "\t", "\n", "\f", "\r", "\"", JSON, "\\", apply,
164
- call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours,
165
- getUTCMinutes, getUTCMonth, getUTCSeconds, hasOwnProperty, join,
166
- lastIndex, length, parse, prototype, push, replace, slice, stringify,
167
- test, toJSON, toString, valueOf
168
- */
169
-
170
-
171
- // Create a JSON object only if one does not already exist. We create the
172
- // methods in a closure to avoid creating global variables.
173
-
174
- var JSON = {};
175
-
176
- (function () {
177
- "use strict";
178
-
179
- function f(n) {
180
- // Format integers to have at least two digits.
181
- return n < 10 ? '0' + n : n;
182
- }
183
-
184
- if (typeof Date.prototype.toJSON !== 'function') {
185
-
186
- Date.prototype.toJSON = function (key) {
187
-
188
- return isFinite(this.valueOf()) ?
189
- this.getUTCFullYear() + '-' +
190
- f(this.getUTCMonth() + 1) + '-' +
191
- f(this.getUTCDate()) + 'T' +
192
- f(this.getUTCHours()) + ':' +
193
- f(this.getUTCMinutes()) + ':' +
194
- f(this.getUTCSeconds()) + 'Z' : null;
195
- };
196
-
197
- String.prototype.toJSON =
198
- Number.prototype.toJSON =
199
- Boolean.prototype.toJSON = function (key) {
200
- return this.valueOf();
201
- };
202
- }
203
-
204
- var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
205
- escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
206
- gap,
207
- indent,
208
- meta = { // table of character substitutions
209
- '\b': '\\b',
210
- '\t': '\\t',
211
- '\n': '\\n',
212
- '\f': '\\f',
213
- '\r': '\\r',
214
- '"' : '\\"',
215
- '\\': '\\\\'
216
- },
217
- rep;
218
-
219
-
220
- function quote(string) {
221
-
222
- // If the string contains no control characters, no quote characters, and no
223
- // backslash characters, then we can safely slap some quotes around it.
224
- // Otherwise we must also replace the offending characters with safe escape
225
- // sequences.
226
-
227
- escapable.lastIndex = 0;
228
- return escapable.test(string) ?
229
- '"' + string.replace(escapable, function (a) {
230
- var c = meta[a];
231
- return typeof c === 'string' ? c :
232
- '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
233
- }) + '"' :
234
- '"' + string + '"';
235
- }
236
-
237
-
238
- function str(key, holder) {
239
-
240
- // Produce a string from holder[key].
241
-
242
- var i, // The loop counter.
243
- k, // The member key.
244
- v, // The member value.
245
- length,
246
- mind = gap,
247
- partial,
248
- value = holder[key];
249
-
250
- // If the value has a toJSON method, call it to obtain a replacement value.
251
-
252
- if (value && typeof value === 'object' &&
253
- typeof value.toJSON === 'function') {
254
- value = value.toJSON(key);
255
- }
256
-
257
- // If we were called with a replacer function, then call the replacer to
258
- // obtain a replacement value.
259
-
260
- if (typeof rep === 'function') {
261
- value = rep.call(holder, key, value);
262
- }
263
-
264
- // What happens next depends on the value's type.
265
-
266
- switch (typeof value) {
267
- case 'string':
268
- return quote(value);
269
-
270
- case 'number':
271
-
272
- // JSON numbers must be finite. Encode non-finite numbers as null.
273
-
274
- return isFinite(value) ? String(value) : 'null';
275
-
276
- case 'boolean':
277
- case 'null':
278
-
279
- // If the value is a boolean or null, convert it to a string. Note:
280
- // typeof null does not produce 'null'. The case is included here in
281
- // the remote chance that this gets fixed someday.
282
-
283
- return String(value);
284
-
285
- // If the type is 'object', we might be dealing with an object or an array or
286
- // null.
287
-
288
- case 'object':
289
-
290
- // Due to a specification blunder in ECMAScript, typeof null is 'object',
291
- // so watch out for that case.
292
-
293
- if (!value) {
294
- return 'null';
295
- }
296
-
297
- // Make an array to hold the partial results of stringifying this object value.
298
-
299
- gap += indent;
300
- partial = [];
301
-
302
- // Is the value an array?
303
-
304
- if (Object.prototype.toString.apply(value) === '[object Array]') {
305
-
306
- // The value is an array. Stringify every element. Use null as a placeholder
307
- // for non-JSON values.
308
-
309
- length = value.length;
310
- for (i = 0; i < length; i += 1) {
311
- partial[i] = str(i, value) || 'null';
312
- }
313
-
314
- // Join all of the elements together, separated with commas, and wrap them in
315
- // brackets.
316
-
317
- v = partial.length === 0 ? '[]' :
318
- gap ? '[\n' + gap +
319
- partial.join(',\n' + gap) + '\n' +
320
- mind + ']' :
321
- '[' + partial.join(',') + ']';
322
- gap = mind;
323
- return v;
324
- }
325
-
326
- // If the replacer is an array, use it to select the members to be stringified.
327
-
328
- if (rep && typeof rep === 'object') {
329
- length = rep.length;
330
- for (i = 0; i < length; i += 1) {
331
- k = rep[i];
332
- if (typeof k === 'string') {
333
- v = str(k, value);
334
- if (v) {
335
- partial.push(quote(k) + (gap ? ': ' : ':') + v);
336
- }
337
- }
338
- }
339
- } else {
340
-
341
- // Otherwise, iterate through all of the keys in the object.
342
-
343
- for (k in value) {
344
- if (Object.hasOwnProperty.call(value, k)) {
345
- v = str(k, value);
346
- if (v) {
347
- partial.push(quote(k) + (gap ? ': ' : ':') + v);
348
- }
349
- }
350
- }
351
- }
352
-
353
- // Join all of the member texts together, separated with commas,
354
- // and wrap them in braces.
355
-
356
- v = partial.length === 0 ? '{}' :
357
- gap ? '{\n' + gap + partial.join(',\n' + gap) + '\n' +
358
- mind + '}' : '{' + partial.join(',') + '}';
359
- gap = mind;
360
- return v;
361
- }
362
- }
363
-
364
- // If the JSON object does not yet have a stringify method, give it one.
365
-
366
- if (typeof JSON.stringify !== 'function') {
367
- JSON.stringify = function (value, replacer, space) {
368
-
369
- // The stringify method takes a value and an optional replacer, and an optional
370
- // space parameter, and returns a JSON text. The replacer can be a function
371
- // that can replace values, or an array of strings that will select the keys.
372
- // A default replacer method can be provided. Use of the space parameter can
373
- // produce text that is more easily readable.
374
-
375
- var i;
376
- gap = '';
377
- indent = '';
378
-
379
- // If the space parameter is a number, make an indent string containing that
380
- // many spaces.
381
-
382
- if (typeof space === 'number') {
383
- for (i = 0; i < space; i += 1) {
384
- indent += ' ';
385
- }
386
-
387
- // If the space parameter is a string, it will be used as the indent string.
388
-
389
- } else if (typeof space === 'string') {
390
- indent = space;
391
- }
392
-
393
- // If there is a replacer, it must be a function or an array.
394
- // Otherwise, throw an error.
395
-
396
- rep = replacer;
397
- if (replacer && typeof replacer !== 'function' &&
398
- (typeof replacer !== 'object' ||
399
- typeof replacer.length !== 'number')) {
400
- throw new Error('JSON.stringify');
401
- }
402
-
403
- // Make a fake root object containing our value under the key of ''.
404
- // Return the result of stringifying the value.
405
-
406
- return str('', {'': value});
407
- };
408
- }
409
-
410
-
411
- // If the JSON object does not yet have a parse method, give it one.
412
-
413
- if (typeof JSON.parse !== 'function') {
414
- JSON.parse = function (text, reviver) {
415
-
416
- // The parse method takes a text and an optional reviver function, and returns
417
- // a JavaScript value if the text is a valid JSON text.
418
-
419
- var j;
420
-
421
- function walk(holder, key) {
422
-
423
- // The walk method is used to recursively walk the resulting structure so
424
- // that modifications can be made.
425
-
426
- var k, v, value = holder[key];
427
- if (value && typeof value === 'object') {
428
- for (k in value) {
429
- if (Object.hasOwnProperty.call(value, k)) {
430
- v = walk(value, k);
431
- if (v !== undefined) {
432
- value[k] = v;
433
- } else {
434
- delete value[k];
435
- }
436
- }
437
- }
438
- }
439
- return reviver.call(holder, key, value);
440
- }
441
-
442
-
443
- // Parsing happens in four stages. In the first stage, we replace certain
444
- // Unicode characters with escape sequences. JavaScript handles many characters
445
- // incorrectly, either silently deleting them, or treating them as line endings.
446
-
447
- text = String(text);
448
- cx.lastIndex = 0;
449
- if (cx.test(text)) {
450
- text = text.replace(cx, function (a) {
451
- return '\\u' +
452
- ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
453
- });
454
- }
455
-
456
- // In the second stage, we run the text against regular expressions that look
457
- // for non-JSON patterns. We are especially concerned with '()' and 'new'
458
- // because they can cause invocation, and '=' because it can cause mutation.
459
- // But just to be safe, we want to reject all unexpected forms.
460
-
461
- // We split the second stage into 4 regexp operations in order to work around
462
- // crippling inefficiencies in IE's and Safari's regexp engines. First we
463
- // replace the JSON backslash pairs with '@' (a non-JSON character). Second, we
464
- // replace all simple value tokens with ']' characters. Third, we delete all
465
- // open brackets that follow a colon or comma or that begin the text. Finally,
466
- // we look to see that the remaining characters are only whitespace or ']' or
467
- // ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
468
-
469
- if (/^[\],:{}\s]*$/
470
- .test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@')
471
- .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']')
472
- .replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
473
-
474
- // In the third stage we use the eval function to compile the text into a
475
- // JavaScript structure. The '{' operator is subject to a syntactic ambiguity
476
- // in JavaScript: it can begin a block or an object literal. We wrap the text
477
- // in parens to eliminate the ambiguity.
478
-
479
- j = eval('(' + text + ')');
480
-
481
- // In the optional fourth stage, we recursively walk the new structure, passing
482
- // each name/value pair to a reviver function for possible transformation.
483
-
484
- return typeof reviver === 'function' ?
485
- walk({'': j}, '') : j;
486
- }
487
-
488
- // If the text is not JSON parseable, then a SyntaxError is thrown.
489
-
490
- throw new SyntaxError('JSON.parse');
491
- };
492
- }
493
- }());
494
- var assert = this.assert = {};
495
- var types = {};
496
- var core = {};
497
- var nodeunit = {};
498
- var reporter = {};
499
- /*global setTimeout: false, console: false */
500
- (function () {
501
-
502
- var async = {};
503
-
504
- // global on the server, window in the browser
505
- var root = this,
506
- previous_async = root.async;
507
-
508
- if (typeof module !== 'undefined' && module.exports) {
509
- module.exports = async;
510
- }
511
- else {
512
- root.async = async;
513
- }
514
-
515
- async.noConflict = function () {
516
- root.async = previous_async;
517
- return async;
518
- };
519
-
520
- //// cross-browser compatiblity functions ////
521
-
522
- var _forEach = function (arr, iterator) {
523
- if (arr.forEach) {
524
- return arr.forEach(iterator);
525
- }
526
- for (var i = 0; i < arr.length; i += 1) {
527
- iterator(arr[i], i, arr);
528
- }
529
- };
530
-
531
- var _map = function (arr, iterator) {
532
- if (arr.map) {
533
- return arr.map(iterator);
534
- }
535
- var results = [];
536
- _forEach(arr, function (x, i, a) {
537
- results.push(iterator(x, i, a));
538
- });
539
- return results;
540
- };
541
-
542
- var _reduce = function (arr, iterator, memo) {
543
- if (arr.reduce) {
544
- return arr.reduce(iterator, memo);
545
- }
546
- _forEach(arr, function (x, i, a) {
547
- memo = iterator(memo, x, i, a);
548
- });
549
- return memo;
550
- };
551
-
552
- var _keys = function (obj) {
553
- if (Object.keys) {
554
- return Object.keys(obj);
555
- }
556
- var keys = [];
557
- for (var k in obj) {
558
- if (obj.hasOwnProperty(k)) {
559
- keys.push(k);
560
- }
561
- }
562
- return keys;
563
- };
564
-
565
- var _indexOf = function (arr, item) {
566
- if (arr.indexOf) {
567
- return arr.indexOf(item);
568
- }
569
- for (var i = 0; i < arr.length; i += 1) {
570
- if (arr[i] === item) {
571
- return i;
572
- }
573
- }
574
- return -1;
575
- };
576
-
577
- //// exported async module functions ////
578
-
579
- //// nextTick implementation with browser-compatible fallback ////
580
- if (typeof process === 'undefined' || !(process.nextTick)) {
581
- async.nextTick = function (fn) {
582
- setTimeout(fn, 0);
583
- };
584
- }
585
- else {
586
- async.nextTick = process.nextTick;
587
- }
588
-
589
- async.forEach = function (arr, iterator, callback) {
590
- if (!arr.length) {
591
- return callback();
592
- }
593
- var completed = 0;
594
- _forEach(arr, function (x) {
595
- iterator(x, function (err) {
596
- if (err) {
597
- callback(err);
598
- callback = function () {};
599
- }
600
- else {
601
- completed += 1;
602
- if (completed === arr.length) {
603
- callback();
604
- }
605
- }
606
- });
607
- });
608
- };
609
-
610
- async.forEachSeries = function (arr, iterator, callback) {
611
- if (!arr.length) {
612
- return callback();
613
- }
614
- var completed = 0;
615
- var iterate = function () {
616
- iterator(arr[completed], function (err) {
617
- if (err) {
618
- callback(err);
619
- callback = function () {};
620
- }
621
- else {
622
- completed += 1;
623
- if (completed === arr.length) {
624
- callback();
625
- }
626
- else {
627
- iterate();
628
- }
629
- }
630
- });
631
- };
632
- iterate();
633
- };
634
-
635
-
636
- var doParallel = function (fn) {
637
- return function () {
638
- var args = Array.prototype.slice.call(arguments);
639
- return fn.apply(null, [async.forEach].concat(args));
640
- };
641
- };
642
- var doSeries = function (fn) {
643
- return function () {
644
- var args = Array.prototype.slice.call(arguments);
645
- return fn.apply(null, [async.forEachSeries].concat(args));
646
- };
647
- };
648
-
649
-
650
- var _asyncMap = function (eachfn, arr, iterator, callback) {
651
- var results = [];
652
- arr = _map(arr, function (x, i) {
653
- return {index: i, value: x};
654
- });
655
- eachfn(arr, function (x, callback) {
656
- iterator(x.value, function (err, v) {
657
- results[x.index] = v;
658
- callback(err);
659
- });
660
- }, function (err) {
661
- callback(err, results);
662
- });
663
- };
664
- async.map = doParallel(_asyncMap);
665
- async.mapSeries = doSeries(_asyncMap);
666
-
667
-
668
- // reduce only has a series version, as doing reduce in parallel won't
669
- // work in many situations.
670
- async.reduce = function (arr, memo, iterator, callback) {
671
- async.forEachSeries(arr, function (x, callback) {
672
- iterator(memo, x, function (err, v) {
673
- memo = v;
674
- callback(err);
675
- });
676
- }, function (err) {
677
- callback(err, memo);
678
- });
679
- };
680
- // inject alias
681
- async.inject = async.reduce;
682
- // foldl alias
683
- async.foldl = async.reduce;
684
-
685
- async.reduceRight = function (arr, memo, iterator, callback) {
686
- var reversed = _map(arr, function (x) {
687
- return x;
688
- }).reverse();
689
- async.reduce(reversed, memo, iterator, callback);
690
- };
691
- // foldr alias
692
- async.foldr = async.reduceRight;
693
-
694
- var _filter = function (eachfn, arr, iterator, callback) {
695
- var results = [];
696
- arr = _map(arr, function (x, i) {
697
- return {index: i, value: x};
698
- });
699
- eachfn(arr, function (x, callback) {
700
- iterator(x.value, function (v) {
701
- if (v) {
702
- results.push(x);
703
- }
704
- callback();
705
- });
706
- }, function (err) {
707
- callback(_map(results.sort(function (a, b) {
708
- return a.index - b.index;
709
- }), function (x) {
710
- return x.value;
711
- }));
712
- });
713
- };
714
- async.filter = doParallel(_filter);
715
- async.filterSeries = doSeries(_filter);
716
- // select alias
717
- async.select = async.filter;
718
- async.selectSeries = async.filterSeries;
719
-
720
- var _reject = function (eachfn, arr, iterator, callback) {
721
- var results = [];
722
- arr = _map(arr, function (x, i) {
723
- return {index: i, value: x};
724
- });
725
- eachfn(arr, function (x, callback) {
726
- iterator(x.value, function (v) {
727
- if (!v) {
728
- results.push(x);
729
- }
730
- callback();
731
- });
732
- }, function (err) {
733
- callback(_map(results.sort(function (a, b) {
734
- return a.index - b.index;
735
- }), function (x) {
736
- return x.value;
737
- }));
738
- });
739
- };
740
- async.reject = doParallel(_reject);
741
- async.rejectSeries = doSeries(_reject);
742
-
743
- var _detect = function (eachfn, arr, iterator, main_callback) {
744
- eachfn(arr, function (x, callback) {
745
- iterator(x, function (result) {
746
- if (result) {
747
- main_callback(x);
748
- }
749
- else {
750
- callback();
751
- }
752
- });
753
- }, function (err) {
754
- main_callback();
755
- });
756
- };
757
- async.detect = doParallel(_detect);
758
- async.detectSeries = doSeries(_detect);
759
-
760
- async.some = function (arr, iterator, main_callback) {
761
- async.forEach(arr, function (x, callback) {
762
- iterator(x, function (v) {
763
- if (v) {
764
- main_callback(true);
765
- main_callback = function () {};
766
- }
767
- callback();
768
- });
769
- }, function (err) {
770
- main_callback(false);
771
- });
772
- };
773
- // any alias
774
- async.any = async.some;
775
-
776
- async.every = function (arr, iterator, main_callback) {
777
- async.forEach(arr, function (x, callback) {
778
- iterator(x, function (v) {
779
- if (!v) {
780
- main_callback(false);
781
- main_callback = function () {};
782
- }
783
- callback();
784
- });
785
- }, function (err) {
786
- main_callback(true);
787
- });
788
- };
789
- // all alias
790
- async.all = async.every;
791
-
792
- async.sortBy = function (arr, iterator, callback) {
793
- async.map(arr, function (x, callback) {
794
- iterator(x, function (err, criteria) {
795
- if (err) {
796
- callback(err);
797
- }
798
- else {
799
- callback(null, {value: x, criteria: criteria});
800
- }
801
- });
802
- }, function (err, results) {
803
- if (err) {
804
- return callback(err);
805
- }
806
- else {
807
- var fn = function (left, right) {
808
- var a = left.criteria, b = right.criteria;
809
- return a < b ? -1 : a > b ? 1 : 0;
810
- };
811
- callback(null, _map(results.sort(fn), function (x) {
812
- return x.value;
813
- }));
814
- }
815
- });
816
- };
817
-
818
- async.auto = function (tasks, callback) {
819
- callback = callback || function () {};
820
- var keys = _keys(tasks);
821
- if (!keys.length) {
822
- return callback(null);
823
- }
824
-
825
- var completed = [];
826
-
827
- var listeners = [];
828
- var addListener = function (fn) {
829
- listeners.unshift(fn);
830
- };
831
- var removeListener = function (fn) {
832
- for (var i = 0; i < listeners.length; i += 1) {
833
- if (listeners[i] === fn) {
834
- listeners.splice(i, 1);
835
- return;
836
- }
837
- }
838
- };
839
- var taskComplete = function () {
840
- _forEach(listeners, function (fn) {
841
- fn();
842
- });
843
- };
844
-
845
- addListener(function () {
846
- if (completed.length === keys.length) {
847
- callback(null);
848
- }
849
- });
850
-
851
- _forEach(keys, function (k) {
852
- var task = (tasks[k] instanceof Function) ? [tasks[k]]: tasks[k];
853
- var taskCallback = function (err) {
854
- if (err) {
855
- callback(err);
856
- // stop subsequent errors hitting callback multiple times
857
- callback = function () {};
858
- }
859
- else {
860
- completed.push(k);
861
- taskComplete();
862
- }
863
- };
864
- var requires = task.slice(0, Math.abs(task.length - 1)) || [];
865
- var ready = function () {
866
- return _reduce(requires, function (a, x) {
867
- return (a && _indexOf(completed, x) !== -1);
868
- }, true);
869
- };
870
- if (ready()) {
871
- task[task.length - 1](taskCallback);
872
- }
873
- else {
874
- var listener = function () {
875
- if (ready()) {
876
- removeListener(listener);
877
- task[task.length - 1](taskCallback);
878
- }
879
- };
880
- addListener(listener);
881
- }
882
- });
883
- };
884
-
885
- async.waterfall = function (tasks, callback) {
886
- if (!tasks.length) {
887
- return callback();
888
- }
889
- callback = callback || function () {};
890
- var wrapIterator = function (iterator) {
891
- return function (err) {
892
- if (err) {
893
- callback(err);
894
- callback = function () {};
895
- }
896
- else {
897
- var args = Array.prototype.slice.call(arguments, 1);
898
- var next = iterator.next();
899
- if (next) {
900
- args.push(wrapIterator(next));
901
- }
902
- else {
903
- args.push(callback);
904
- }
905
- async.nextTick(function () {
906
- iterator.apply(null, args);
907
- });
908
- }
909
- };
910
- };
911
- wrapIterator(async.iterator(tasks))();
912
- };
913
-
914
- async.parallel = function (tasks, callback) {
915
- callback = callback || function () {};
916
- if (tasks.constructor === Array) {
917
- async.map(tasks, function (fn, callback) {
918
- if (fn) {
919
- fn(function (err) {
920
- var args = Array.prototype.slice.call(arguments, 1);
921
- if (args.length <= 1) {
922
- args = args[0];
923
- }
924
- callback.call(null, err, args || null);
925
- });
926
- }
927
- }, callback);
928
- }
929
- else {
930
- var results = {};
931
- async.forEach(_keys(tasks), function (k, callback) {
932
- tasks[k](function (err) {
933
- var args = Array.prototype.slice.call(arguments, 1);
934
- if (args.length <= 1) {
935
- args = args[0];
936
- }
937
- results[k] = args;
938
- callback(err);
939
- });
940
- }, function (err) {
941
- callback(err, results);
942
- });
943
- }
944
- };
945
-
946
- async.series = function (tasks, callback) {
947
- callback = callback || function () {};
948
- if (tasks.constructor === Array) {
949
- async.mapSeries(tasks, function (fn, callback) {
950
- if (fn) {
951
- fn(function (err) {
952
- var args = Array.prototype.slice.call(arguments, 1);
953
- if (args.length <= 1) {
954
- args = args[0];
955
- }
956
- callback.call(null, err, args || null);
957
- });
958
- }
959
- }, callback);
960
- }
961
- else {
962
- var results = {};
963
- async.forEachSeries(_keys(tasks), function (k, callback) {
964
- tasks[k](function (err) {
965
- var args = Array.prototype.slice.call(arguments, 1);
966
- if (args.length <= 1) {
967
- args = args[0];
968
- }
969
- results[k] = args;
970
- callback(err);
971
- });
972
- }, function (err) {
973
- callback(err, results);
974
- });
975
- }
976
- };
977
-
978
- async.iterator = function (tasks) {
979
- var makeCallback = function (index) {
980
- var fn = function () {
981
- if (tasks.length) {
982
- tasks[index].apply(null, arguments);
983
- }
984
- return fn.next();
985
- };
986
- fn.next = function () {
987
- return (index < tasks.length - 1) ? makeCallback(index + 1): null;
988
- };
989
- return fn;
990
- };
991
- return makeCallback(0);
992
- };
993
-
994
- async.apply = function (fn) {
995
- var args = Array.prototype.slice.call(arguments, 1);
996
- return function () {
997
- return fn.apply(
998
- null, args.concat(Array.prototype.slice.call(arguments))
999
- );
1000
- };
1001
- };
1002
-
1003
- var _concat = function (eachfn, arr, fn, callback) {
1004
- var r = [];
1005
- eachfn(arr, function (x, cb) {
1006
- fn(x, function (err, y) {
1007
- r = r.concat(y || []);
1008
- cb(err);
1009
- });
1010
- }, function (err) {
1011
- callback(err, r);
1012
- });
1013
- };
1014
- async.concat = doParallel(_concat);
1015
- async.concatSeries = doSeries(_concat);
1016
-
1017
- async.whilst = function (test, iterator, callback) {
1018
- if (test()) {
1019
- iterator(function (err) {
1020
- if (err) {
1021
- return callback(err);
1022
- }
1023
- async.whilst(test, iterator, callback);
1024
- });
1025
- }
1026
- else {
1027
- callback();
1028
- }
1029
- };
1030
-
1031
- async.until = function (test, iterator, callback) {
1032
- if (!test()) {
1033
- iterator(function (err) {
1034
- if (err) {
1035
- return callback(err);
1036
- }
1037
- async.until(test, iterator, callback);
1038
- });
1039
- }
1040
- else {
1041
- callback();
1042
- }
1043
- };
1044
-
1045
- async.queue = function (worker, concurrency) {
1046
- var workers = 0;
1047
- var tasks = [];
1048
- var q = {
1049
- concurrency: concurrency,
1050
- push: function (data, callback) {
1051
- tasks.push({data: data, callback: callback});
1052
- async.nextTick(q.process);
1053
- },
1054
- process: function () {
1055
- if (workers < q.concurrency && tasks.length) {
1056
- var task = tasks.splice(0, 1)[0];
1057
- workers += 1;
1058
- worker(task.data, function () {
1059
- workers -= 1;
1060
- if (task.callback) {
1061
- task.callback.apply(task, arguments);
1062
- }
1063
- q.process();
1064
- });
1065
- }
1066
- },
1067
- length: function () {
1068
- return tasks.length;
1069
- }
1070
- };
1071
- return q;
1072
- };
1073
-
1074
- var _console_fn = function (name) {
1075
- return function (fn) {
1076
- var args = Array.prototype.slice.call(arguments, 1);
1077
- fn.apply(null, args.concat([function (err) {
1078
- var args = Array.prototype.slice.call(arguments, 1);
1079
- if (typeof console !== 'undefined') {
1080
- if (err) {
1081
- if (console.error) {
1082
- console.error(err);
1083
- }
1084
- }
1085
- else if (console[name]) {
1086
- _forEach(args, function (x) {
1087
- console[name](x);
1088
- });
1089
- }
1090
- }
1091
- }]));
1092
- };
1093
- };
1094
- async.log = _console_fn('log');
1095
- async.dir = _console_fn('dir');
1096
- /*async.info = _console_fn('info');
1097
- async.warn = _console_fn('warn');
1098
- async.error = _console_fn('error');*/
1099
-
1100
- async.memoize = function (fn, hasher) {
1101
- var memo = {};
1102
- hasher = hasher || function (x) {
1103
- return x;
1104
- };
1105
- return function () {
1106
- var args = Array.prototype.slice.call(arguments);
1107
- var callback = args.pop();
1108
- var key = hasher.apply(null, args);
1109
- if (key in memo) {
1110
- callback.apply(null, memo[key]);
1111
- }
1112
- else {
1113
- fn.apply(null, args.concat([function () {
1114
- memo[key] = arguments;
1115
- callback.apply(null, arguments);
1116
- }]));
1117
- }
1118
- };
1119
- };
1120
-
1121
- }());
1122
- (function(exports){
1123
- /**
1124
- * This file is based on the node.js assert module, but with some small
1125
- * changes for browser-compatibility
1126
- * THIS FILE SHOULD BE BROWSER-COMPATIBLE JS!
1127
- */
1128
-
1129
-
1130
- /**
1131
- * Added for browser compatibility
1132
- */
1133
-
1134
- var _keys = function(obj){
1135
- if(Object.keys) return Object.keys(obj);
1136
- if (typeof obj != 'object' && typeof obj != 'function') {
1137
- throw new TypeError('-');
1138
- }
1139
- var keys = [];
1140
- for(var k in obj){
1141
- if(obj.hasOwnProperty(k)) keys.push(k);
1142
- }
1143
- return keys;
1144
- };
1145
-
1146
-
1147
-
1148
- // http://wiki.commonjs.org/wiki/Unit_Testing/1.0
1149
- //
1150
- // THIS IS NOT TESTED NOR LIKELY TO WORK OUTSIDE V8!
1151
- //
1152
- // Originally from narwhal.js (http://narwhaljs.org)
1153
- // Copyright (c) 2009 Thomas Robinson <280north.com>
1154
- //
1155
- // Permission is hereby granted, free of charge, to any person obtaining a copy
1156
- // of this software and associated documentation files (the 'Software'), to
1157
- // deal in the Software without restriction, including without limitation the
1158
- // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1159
- // sell copies of the Software, and to permit persons to whom the Software is
1160
- // furnished to do so, subject to the following conditions:
1161
- //
1162
- // The above copyright notice and this permission notice shall be included in
1163
- // all copies or substantial portions of the Software.
1164
- //
1165
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1166
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1167
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1168
- // AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
1169
- // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
1170
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1171
-
1172
-
1173
- var pSlice = Array.prototype.slice;
1174
-
1175
- // 1. The assert module provides functions that throw
1176
- // AssertionError's when particular conditions are not met. The
1177
- // assert module must conform to the following interface.
1178
-
1179
- var assert = exports;
1180
-
1181
- // 2. The AssertionError is defined in assert.
1182
- // new assert.AssertionError({message: message, actual: actual, expected: expected})
1183
-
1184
- assert.AssertionError = function AssertionError (options) {
1185
- this.name = "AssertionError";
1186
- this.message = options.message;
1187
- this.actual = options.actual;
1188
- this.expected = options.expected;
1189
- this.operator = options.operator;
1190
- var stackStartFunction = options.stackStartFunction || fail;
1191
-
1192
- if (Error.captureStackTrace) {
1193
- Error.captureStackTrace(this, stackStartFunction);
1194
- }
1195
- };
1196
- // code from util.inherits in node
1197
- assert.AssertionError.super_ = Error;
1198
-
1199
-
1200
- // EDITED FOR BROWSER COMPATIBILITY: replaced Object.create call
1201
- // TODO: test what effect this may have
1202
- var ctor = function () { this.constructor = assert.AssertionError; };
1203
- ctor.prototype = Error.prototype;
1204
- assert.AssertionError.prototype = new ctor();
1205
-
1206
-
1207
- assert.AssertionError.prototype.toString = function() {
1208
- if (this.message) {
1209
- return [this.name+":", this.message].join(' ');
1210
- } else {
1211
- return [ this.name+":"
1212
- , JSON.stringify(this.expected )
1213
- , this.operator
1214
- , JSON.stringify(this.actual)
1215
- ].join(" ");
1216
- }
1217
- };
1218
-
1219
- // assert.AssertionError instanceof Error
1220
-
1221
- assert.AssertionError.__proto__ = Error.prototype;
1222
-
1223
- // At present only the three keys mentioned above are used and
1224
- // understood by the spec. Implementations or sub modules can pass
1225
- // other keys to the AssertionError's constructor - they will be
1226
- // ignored.
1227
-
1228
- // 3. All of the following functions must throw an AssertionError
1229
- // when a corresponding condition is not met, with a message that
1230
- // may be undefined if not provided. All assertion methods provide
1231
- // both the actual and expected values to the assertion error for
1232
- // display purposes.
1233
-
1234
- function fail(actual, expected, message, operator, stackStartFunction) {
1235
- throw new assert.AssertionError({
1236
- message: message,
1237
- actual: actual,
1238
- expected: expected,
1239
- operator: operator,
1240
- stackStartFunction: stackStartFunction
1241
- });
1242
- }
1243
-
1244
- // EXTENSION! allows for well behaved errors defined elsewhere.
1245
- assert.fail = fail;
1246
-
1247
- // 4. Pure assertion tests whether a value is truthy, as determined
1248
- // by !!guard.
1249
- // assert.ok(guard, message_opt);
1250
- // This statement is equivalent to assert.equal(true, guard,
1251
- // message_opt);. To test strictly for the value true, use
1252
- // assert.strictEqual(true, guard, message_opt);.
1253
-
1254
- assert.ok = function ok(value, message) {
1255
- if (!!!value) fail(value, true, message, "==", assert.ok);
1256
- };
1257
-
1258
- // 5. The equality assertion tests shallow, coercive equality with
1259
- // ==.
1260
- // assert.equal(actual, expected, message_opt);
1261
-
1262
- assert.equal = function equal(actual, expected, message) {
1263
- if (actual != expected) fail(actual, expected, message, "==", assert.equal);
1264
- };
1265
-
1266
- // 6. The non-equality assertion tests for whether two objects are not equal
1267
- // with != assert.notEqual(actual, expected, message_opt);
1268
-
1269
- assert.notEqual = function notEqual(actual, expected, message) {
1270
- if (actual == expected) {
1271
- fail(actual, expected, message, "!=", assert.notEqual);
1272
- }
1273
- };
1274
-
1275
- // 7. The equivalence assertion tests a deep equality relation.
1276
- // assert.deepEqual(actual, expected, message_opt);
1277
-
1278
- assert.deepEqual = function deepEqual(actual, expected, message) {
1279
- if (!_deepEqual(actual, expected)) {
1280
- fail(actual, expected, message, "deepEqual", assert.deepEqual);
1281
- }
1282
- };
1283
-
1284
- var Buffer = null;
1285
- if (typeof require !== 'undefined' && typeof process !== 'undefined') {
1286
- try {
1287
- Buffer = require('buffer').Buffer;
1288
- }
1289
- catch (e) {
1290
- // May be a CommonJS environment other than Node.js
1291
- Buffer = null;
1292
- }
1293
- }
1294
-
1295
- function _deepEqual(actual, expected) {
1296
- // 7.1. All identical values are equivalent, as determined by ===.
1297
- if (actual === expected) {
1298
- return true;
1299
- // 7.2. If the expected value is a Date object, the actual value is
1300
- // equivalent if it is also a Date object that refers to the same time.
1301
- } else if (actual instanceof Date && expected instanceof Date) {
1302
- return actual.getTime() === expected.getTime();
1303
-
1304
- // 7.2.1 If the expcted value is a RegExp object, the actual value is
1305
- // equivalent if it is also a RegExp object that refers to the same source and options
1306
- } else if (actual instanceof RegExp && expected instanceof RegExp) {
1307
- return actual.source === expected.source &&
1308
- actual.global === expected.global &&
1309
- actual.ignoreCase === expected.ignoreCase &&
1310
- actual.multiline === expected.multiline;
1311
-
1312
- } else if (Buffer && actual instanceof Buffer && expected instanceof Buffer) {
1313
- return (function() {
1314
- var i, len;
1315
-
1316
- for (i = 0, len = expected.length; i < len; i++) {
1317
- if (actual[i] !== expected[i]) {
1318
- return false;
1319
- }
1320
- }
1321
- return actual.length === expected.length;
1322
- })();
1323
- // 7.3. Other pairs that do not both pass typeof value == "object",
1324
- // equivalence is determined by ==.
1325
- } else if (typeof actual != 'object' && typeof expected != 'object') {
1326
- return actual == expected;
1327
-
1328
- // 7.4. For all other Object pairs, including Array objects, equivalence is
1329
- // determined by having the same number of owned properties (as verified
1330
- // with Object.prototype.hasOwnProperty.call), the same set of keys
1331
- // (although not necessarily the same order), equivalent values for every
1332
- // corresponding key, and an identical "prototype" property. Note: this
1333
- // accounts for both named and indexed properties on Arrays.
1334
- } else {
1335
- return objEquiv(actual, expected);
1336
- }
1337
- }
1338
-
1339
- function isUndefinedOrNull (value) {
1340
- return value === null || value === undefined;
1341
- }
1342
-
1343
- function isArguments (object) {
1344
- return Object.prototype.toString.call(object) == '[object Arguments]';
1345
- }
1346
-
1347
- function objEquiv (a, b) {
1348
- if (isUndefinedOrNull(a) || isUndefinedOrNull(b))
1349
- return false;
1350
- // an identical "prototype" property.
1351
- if (a.prototype !== b.prototype) return false;
1352
- //~~~I've managed to break Object.keys through screwy arguments passing.
1353
- // Converting to array solves the problem.
1354
- if (isArguments(a)) {
1355
- if (!isArguments(b)) {
1356
- return false;
1357
- }
1358
- a = pSlice.call(a);
1359
- b = pSlice.call(b);
1360
- return _deepEqual(a, b);
1361
- }
1362
- try{
1363
- var ka = _keys(a),
1364
- kb = _keys(b),
1365
- key, i;
1366
- } catch (e) {//happens when one is a string literal and the other isn't
1367
- return false;
1368
- }
1369
- // having the same number of owned properties (keys incorporates hasOwnProperty)
1370
- if (ka.length != kb.length)
1371
- return false;
1372
- //the same set of keys (although not necessarily the same order),
1373
- ka.sort();
1374
- kb.sort();
1375
- //~~~cheap key test
1376
- for (i = ka.length - 1; i >= 0; i--) {
1377
- if (ka[i] != kb[i])
1378
- return false;
1379
- }
1380
- //equivalent values for every corresponding key, and
1381
- //~~~possibly expensive deep test
1382
- for (i = ka.length - 1; i >= 0; i--) {
1383
- key = ka[i];
1384
- if (!_deepEqual(a[key], b[key] ))
1385
- return false;
1386
- }
1387
- return true;
1388
- }
1389
-
1390
- // 8. The non-equivalence assertion tests for any deep inequality.
1391
- // assert.notDeepEqual(actual, expected, message_opt);
1392
-
1393
- assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
1394
- if (_deepEqual(actual, expected)) {
1395
- fail(actual, expected, message, "notDeepEqual", assert.notDeepEqual);
1396
- }
1397
- };
1398
-
1399
- // 9. The strict equality assertion tests strict equality, as determined by ===.
1400
- // assert.strictEqual(actual, expected, message_opt);
1401
-
1402
- assert.strictEqual = function strictEqual(actual, expected, message) {
1403
- if (actual !== expected) {
1404
- fail(actual, expected, message, "===", assert.strictEqual);
1405
- }
1406
- };
1407
-
1408
- // 10. The strict non-equality assertion tests for strict inequality, as determined by !==.
1409
- // assert.notStrictEqual(actual, expected, message_opt);
1410
-
1411
- assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
1412
- if (actual === expected) {
1413
- fail(actual, expected, message, "!==", assert.notStrictEqual);
1414
- }
1415
- };
1416
-
1417
- function expectedException(actual, expected) {
1418
- if (!actual || !expected) {
1419
- return false;
1420
- }
1421
-
1422
- if (expected instanceof RegExp) {
1423
- return expected.test(actual.message || actual);
1424
- } else if (actual instanceof expected) {
1425
- return true;
1426
- } else if (expected.call({}, actual) === true) {
1427
- return true;
1428
- }
1429
-
1430
- return false;
1431
- }
1432
-
1433
- function _throws(shouldThrow, block, expected, message) {
1434
- var actual;
1435
-
1436
- if (typeof expected === 'string') {
1437
- message = expected;
1438
- expected = null;
1439
- }
1440
-
1441
- try {
1442
- block();
1443
- } catch (e) {
1444
- actual = e;
1445
- }
1446
-
1447
- message = (expected && expected.name ? ' (' + expected.name + ').' : '.') +
1448
- (message ? ' ' + message : '.');
1449
-
1450
- if (shouldThrow && !actual) {
1451
- fail('Missing expected exception' + message);
1452
- }
1453
-
1454
- if (!shouldThrow && expectedException(actual, expected)) {
1455
- fail('Got unwanted exception' + message);
1456
- }
1457
-
1458
- if ((shouldThrow && actual && expected &&
1459
- !expectedException(actual, expected)) || (!shouldThrow && actual)) {
1460
- throw actual;
1461
- }
1462
- }
1463
-
1464
- // 11. Expected to throw an error:
1465
- // assert.throws(block, Error_opt, message_opt);
1466
-
1467
- assert.throws = function(block, /*optional*/error, /*optional*/message) {
1468
- _throws.apply(this, [true].concat(pSlice.call(arguments)));
1469
- };
1470
-
1471
- // EXTENSION! This is annoying to write outside this module.
1472
- assert.doesNotThrow = function(block, /*optional*/error, /*optional*/message) {
1473
- _throws.apply(this, [false].concat(pSlice.call(arguments)));
1474
- };
1475
-
1476
- assert.ifError = function (err) { if (err) {throw err;}};
1477
- })(assert);
1478
- (function(exports){
1479
- /*!
1480
- * Nodeunit
1481
- * Copyright (c) 2010 Caolan McMahon
1482
- * MIT Licensed
1483
- *
1484
- * THIS FILE SHOULD BE BROWSER-COMPATIBLE JS!
1485
- * Only code on that line will be removed, it's mostly to avoid requiring code
1486
- * that is node specific
1487
- */
1488
-
1489
- /**
1490
- * Module dependencies
1491
- */
1492
-
1493
-
1494
-
1495
- /**
1496
- * Creates assertion objects representing the result of an assert call.
1497
- * Accepts an object or AssertionError as its argument.
1498
- *
1499
- * @param {object} obj
1500
- * @api public
1501
- */
1502
-
1503
- exports.assertion = function (obj) {
1504
- return {
1505
- method: obj.method || '',
1506
- message: obj.message || (obj.error && obj.error.message) || '',
1507
- error: obj.error,
1508
- passed: function () {
1509
- return !this.error;
1510
- },
1511
- failed: function () {
1512
- return Boolean(this.error);
1513
- }
1514
- };
1515
- };
1516
-
1517
- /**
1518
- * Creates an assertion list object representing a group of assertions.
1519
- * Accepts an array of assertion objects.
1520
- *
1521
- * @param {Array} arr
1522
- * @param {Number} duration
1523
- * @api public
1524
- */
1525
-
1526
- exports.assertionList = function (arr, duration) {
1527
- var that = arr || [];
1528
- that.failures = function () {
1529
- var failures = 0;
1530
- for (var i = 0; i < this.length; i += 1) {
1531
- if (this[i].failed()) {
1532
- failures += 1;
1533
- }
1534
- }
1535
- return failures;
1536
- };
1537
- that.passes = function () {
1538
- return that.length - that.failures();
1539
- };
1540
- that.duration = duration || 0;
1541
- return that;
1542
- };
1543
-
1544
- /**
1545
- * Create a wrapper function for assert module methods. Executes a callback
1546
- * after it's complete with an assertion object representing the result.
1547
- *
1548
- * @param {Function} callback
1549
- * @api private
1550
- */
1551
-
1552
- var assertWrapper = function (callback) {
1553
- return function (new_method, assert_method, arity) {
1554
- return function () {
1555
- var message = arguments[arity - 1];
1556
- var a = exports.assertion({method: new_method, message: message});
1557
- try {
1558
- assert[assert_method].apply(null, arguments);
1559
- }
1560
- catch (e) {
1561
- a.error = e;
1562
- }
1563
- callback(a);
1564
- };
1565
- };
1566
- };
1567
-
1568
- /**
1569
- * Creates the 'test' object that gets passed to every test function.
1570
- * Accepts the name of the test function as its first argument, followed by
1571
- * the start time in ms, the options object and a callback function.
1572
- *
1573
- * @param {String} name
1574
- * @param {Number} start
1575
- * @param {Object} options
1576
- * @param {Function} callback
1577
- * @api public
1578
- */
1579
-
1580
- exports.test = function (name, start, options, callback) {
1581
- var expecting;
1582
- var a_list = [];
1583
-
1584
- var wrapAssert = assertWrapper(function (a) {
1585
- a_list.push(a);
1586
- if (options.log) {
1587
- async.nextTick(function () {
1588
- options.log(a);
1589
- });
1590
- }
1591
- });
1592
-
1593
- var test = {
1594
- done: function (err) {
1595
- if (expecting !== undefined && expecting !== a_list.length) {
1596
- var e = new Error(
1597
- 'Expected ' + expecting + ' assertions, ' +
1598
- a_list.length + ' ran'
1599
- );
1600
- var a1 = exports.assertion({method: 'expect', error: e});
1601
- a_list.push(a1);
1602
- if (options.log) {
1603
- async.nextTick(function () {
1604
- options.log(a1);
1605
- });
1606
- }
1607
- }
1608
- if (err) {
1609
- var a2 = exports.assertion({error: err});
1610
- a_list.push(a2);
1611
- if (options.log) {
1612
- async.nextTick(function () {
1613
- options.log(a2);
1614
- });
1615
- }
1616
- }
1617
- var end = new Date().getTime();
1618
- async.nextTick(function () {
1619
- var assertion_list = exports.assertionList(a_list, end - start);
1620
- options.testDone(name, assertion_list);
1621
- callback(null, a_list);
1622
- });
1623
- },
1624
- ok: wrapAssert('ok', 'ok', 2),
1625
- same: wrapAssert('same', 'deepEqual', 3),
1626
- equals: wrapAssert('equals', 'equal', 3),
1627
- expect: function (num) {
1628
- expecting = num;
1629
- },
1630
- _assertion_list: a_list
1631
- };
1632
- // add all functions from the assert module
1633
- for (var k in assert) {
1634
- if (assert.hasOwnProperty(k)) {
1635
- test[k] = wrapAssert(k, k, assert[k].length);
1636
- }
1637
- }
1638
- return test;
1639
- };
1640
-
1641
- /**
1642
- * Ensures an options object has all callbacks, adding empty callback functions
1643
- * if any are missing.
1644
- *
1645
- * @param {Object} opt
1646
- * @return {Object}
1647
- * @api public
1648
- */
1649
-
1650
- exports.options = function (opt) {
1651
- var optionalCallback = function (name) {
1652
- opt[name] = opt[name] || function () {};
1653
- };
1654
-
1655
- optionalCallback('moduleStart');
1656
- optionalCallback('moduleDone');
1657
- optionalCallback('testStart');
1658
- optionalCallback('testDone');
1659
- //optionalCallback('log');
1660
-
1661
- // 'done' callback is not optional.
1662
-
1663
- return opt;
1664
- };
1665
- })(types);
1666
- (function(exports){
1667
- /*!
1668
- * Nodeunit
1669
- * Copyright (c) 2010 Caolan McMahon
1670
- * MIT Licensed
1671
- *
1672
- * THIS FILE SHOULD BE BROWSER-COMPATIBLE JS!
1673
- * Only code on that line will be removed, it's mostly to avoid requiring code
1674
- * that is node specific
1675
- */
1676
-
1677
- /**
1678
- * Module dependencies
1679
- */
1680
-
1681
-
1682
-
1683
- /**
1684
- * Added for browser compatibility
1685
- */
1686
-
1687
- var _keys = function (obj) {
1688
- if (Object.keys) {
1689
- return Object.keys(obj);
1690
- }
1691
- var keys = [];
1692
- for (var k in obj) {
1693
- if (obj.hasOwnProperty(k)) {
1694
- keys.push(k);
1695
- }
1696
- }
1697
- return keys;
1698
- };
1699
-
1700
-
1701
- var _copy = function (obj) {
1702
- var nobj = {};
1703
- var keys = _keys(obj);
1704
- for (var i = 0; i < keys.length; i += 1) {
1705
- nobj[keys[i]] = obj[keys[i]];
1706
- }
1707
- return nobj;
1708
- };
1709
-
1710
-
1711
- /**
1712
- * Runs a test function (fn) from a loaded module. After the test function
1713
- * calls test.done(), the callback is executed with an assertionList as its
1714
- * second argument.
1715
- *
1716
- * @param {String} name
1717
- * @param {Function} fn
1718
- * @param {Object} opt
1719
- * @param {Function} callback
1720
- * @api public
1721
- */
1722
-
1723
- exports.runTest = function (name, fn, opt, callback) {
1724
- var options = types.options(opt);
1725
-
1726
- options.testStart(name);
1727
- var start = new Date().getTime();
1728
- var test = types.test(name, start, options, callback);
1729
-
1730
- try {
1731
- fn(test);
1732
- }
1733
- catch (e) {
1734
- test.done(e);
1735
- }
1736
- };
1737
-
1738
- /**
1739
- * Takes an object containing test functions or other test suites as properties
1740
- * and runs each in series. After all tests have completed, the callback is
1741
- * called with a list of all assertions as the second argument.
1742
- *
1743
- * If a name is passed to this function it is prepended to all test and suite
1744
- * names that run within it.
1745
- *
1746
- * @param {String} name
1747
- * @param {Object} suite
1748
- * @param {Object} opt
1749
- * @param {Function} callback
1750
- * @api public
1751
- */
1752
-
1753
- exports.runSuite = function (name, suite, opt, callback) {
1754
- suite = wrapGroup(suite);
1755
- var keys = _keys(suite);
1756
-
1757
- async.concatSeries(keys, function (k, cb) {
1758
- var prop = suite[k], _name;
1759
-
1760
- _name = name ? [].concat(name, k) : [k];
1761
- _name.toString = function () {
1762
- // fallback for old one
1763
- return this.join(' - ');
1764
- };
1765
-
1766
- if (typeof prop === 'function') {
1767
- var in_name = false,
1768
- in_specific_test = (_name.toString() === opt.testFullSpec) ? true : false;
1769
- for (var i = 0; i < _name.length; i += 1) {
1770
- if (_name[i] === opt.testspec) {
1771
- in_name = true;
1772
- }
1773
- }
1774
-
1775
- if ((!opt.testFullSpec || in_specific_test) && (!opt.testspec || in_name)) {
1776
- if (opt.moduleStart) {
1777
- opt.moduleStart();
1778
- }
1779
- exports.runTest(_name, suite[k], opt, cb);
1780
- }
1781
- else {
1782
- return cb();
1783
- }
1784
- }
1785
- else {
1786
- exports.runSuite(_name, suite[k], opt, cb);
1787
- }
1788
- }, callback);
1789
- };
1790
-
1791
- /**
1792
- * Run each exported test function or test suite from a loaded module.
1793
- *
1794
- * @param {String} name
1795
- * @param {Object} mod
1796
- * @param {Object} opt
1797
- * @param {Function} callback
1798
- * @api public
1799
- */
1800
-
1801
- exports.runModule = function (name, mod, opt, callback) {
1802
- var options = _copy(types.options(opt));
1803
-
1804
- var _run = false;
1805
- var _moduleStart = options.moduleStart;
1806
-
1807
- mod = wrapGroup(mod);
1808
-
1809
- function run_once() {
1810
- if (!_run) {
1811
- _run = true;
1812
- _moduleStart(name);
1813
- }
1814
- }
1815
- options.moduleStart = run_once;
1816
-
1817
- var start = new Date().getTime();
1818
-
1819
- exports.runSuite(null, mod, options, function (err, a_list) {
1820
- var end = new Date().getTime();
1821
- var assertion_list = types.assertionList(a_list, end - start);
1822
- options.moduleDone(name, assertion_list);
1823
- if (nodeunit.complete) {
1824
- nodeunit.complete(name, assertion_list);
1825
- }
1826
- callback(null, a_list);
1827
- });
1828
- };
1829
-
1830
- /**
1831
- * Treats an object literal as a list of modules keyed by name. Runs each
1832
- * module and finished with calling 'done'. You can think of this as a browser
1833
- * safe alternative to runFiles in the nodeunit module.
1834
- *
1835
- * @param {Object} modules
1836
- * @param {Object} opt
1837
- * @api public
1838
- */
1839
-
1840
- // TODO: add proper unit tests for this function
1841
- exports.runModules = function (modules, opt) {
1842
- var all_assertions = [];
1843
- var options = types.options(opt);
1844
- var start = new Date().getTime();
1845
-
1846
- async.concatSeries(_keys(modules), function (k, cb) {
1847
- exports.runModule(k, modules[k], options, cb);
1848
- },
1849
- function (err, all_assertions) {
1850
- var end = new Date().getTime();
1851
- options.done(types.assertionList(all_assertions, end - start));
1852
- });
1853
- };
1854
-
1855
-
1856
- /**
1857
- * Wraps a test function with setUp and tearDown functions.
1858
- * Used by testCase.
1859
- *
1860
- * @param {Function} setUp
1861
- * @param {Function} tearDown
1862
- * @param {Function} fn
1863
- * @api private
1864
- */
1865
-
1866
- var wrapTest = function (setUp, tearDown, fn) {
1867
- return function (test) {
1868
- var context = {};
1869
- if (tearDown) {
1870
- var done = test.done;
1871
- test.done = function (err) {
1872
- try {
1873
- tearDown.call(context, function (err2) {
1874
- if (err && err2) {
1875
- test._assertion_list.push(
1876
- types.assertion({error: err})
1877
- );
1878
- return done(err2);
1879
- }
1880
- done(err || err2);
1881
- });
1882
- }
1883
- catch (e) {
1884
- done(e);
1885
- }
1886
- };
1887
- }
1888
- if (setUp) {
1889
- setUp.call(context, function (err) {
1890
- if (err) {
1891
- return test.done(err);
1892
- }
1893
- fn.call(context, test);
1894
- });
1895
- }
1896
- else {
1897
- fn.call(context, test);
1898
- }
1899
- };
1900
- };
1901
-
1902
-
1903
- /**
1904
- * Returns a serial callback from two functions.
1905
- *
1906
- * @param {Function} funcFirst
1907
- * @param {Function} funcSecond
1908
- * @api private
1909
- */
1910
-
1911
- var getSerialCallback = function (fns) {
1912
- if (!fns.length) {
1913
- return null;
1914
- }
1915
- return function (callback) {
1916
- var that = this;
1917
- var bound_fns = [];
1918
- for (var i = 0, len = fns.length; i < len; i++) {
1919
- (function (j) {
1920
- bound_fns.push(function () {
1921
- return fns[j].apply(that, arguments);
1922
- });
1923
- })(i);
1924
- }
1925
- return async.series(bound_fns, callback);
1926
- };
1927
- };
1928
-
1929
-
1930
- /**
1931
- * Wraps a group of tests with setUp and tearDown functions.
1932
- * Used by testCase.
1933
- *
1934
- * @param {Object} group
1935
- * @param {Array} setUps - parent setUp functions
1936
- * @param {Array} tearDowns - parent tearDown functions
1937
- * @api private
1938
- */
1939
-
1940
- var wrapGroup = function (group, setUps, tearDowns) {
1941
- var tests = {};
1942
-
1943
- var setUps = setUps ? setUps.slice(): [];
1944
- var tearDowns = tearDowns ? tearDowns.slice(): [];
1945
-
1946
- if (group.setUp) {
1947
- setUps.push(group.setUp);
1948
- delete group.setUp;
1949
- }
1950
- if (group.tearDown) {
1951
- tearDowns.unshift(group.tearDown);
1952
- delete group.tearDown;
1953
- }
1954
-
1955
- var keys = _keys(group);
1956
-
1957
- for (var i = 0; i < keys.length; i += 1) {
1958
- var k = keys[i];
1959
- if (typeof group[k] === 'function') {
1960
- tests[k] = wrapTest(
1961
- getSerialCallback(setUps),
1962
- getSerialCallback(tearDowns),
1963
- group[k]
1964
- );
1965
- }
1966
- else if (typeof group[k] === 'object') {
1967
- tests[k] = wrapGroup(group[k], setUps, tearDowns);
1968
- }
1969
- }
1970
- return tests;
1971
- };
1972
-
1973
-
1974
- /**
1975
- * Backwards compatibility for test suites using old testCase API
1976
- */
1977
-
1978
- exports.testCase = function (suite) {
1979
- return suite;
1980
- };
1981
- })(core);
1982
- (function(exports){
1983
- /*!
1984
- * Nodeunit
1985
- * Copyright (c) 2010 Caolan McMahon
1986
- * MIT Licensed
1987
- *
1988
- * THIS FILE SHOULD BE BROWSER-COMPATIBLE JS!
1989
- * Only code on that line will be removed, its mostly to avoid requiring code
1990
- * that is node specific
1991
- */
1992
-
1993
-
1994
- /**
1995
- * NOTE: this test runner is not listed in index.js because it cannot be
1996
- * used with the command-line tool, only inside the browser.
1997
- */
1998
-
1999
-
2000
- /**
2001
- * Reporter info string
2002
- */
2003
-
2004
- exports.info = "Browser-based test reporter";
2005
-
2006
-
2007
- /**
2008
- * Run all tests within each module, reporting the results
2009
- *
2010
- * @param {Array} files
2011
- * @api public
2012
- */
2013
-
2014
- exports.run = function (modules, options) {
2015
- var start = new Date().getTime(), div;
2016
- options = options || {};
2017
- div = options.div || document.body;
2018
-
2019
- function setText(el, txt) {
2020
- if ('innerText' in el) {
2021
- el.innerText = txt;
2022
- }
2023
- else if ('textContent' in el){
2024
- el.textContent = txt;
2025
- }
2026
- }
2027
-
2028
- function getOrCreate(tag, id) {
2029
- var el = document.getElementById(id);
2030
- if (!el) {
2031
- el = document.createElement(tag);
2032
- el.id = id;
2033
- div.appendChild(el);
2034
- }
2035
- return el;
2036
- };
2037
-
2038
- var header = getOrCreate('h1', 'nodeunit-header');
2039
- var banner = getOrCreate('h2', 'nodeunit-banner');
2040
- var userAgent = getOrCreate('h2', 'nodeunit-userAgent');
2041
- var tests = getOrCreate('ol', 'nodeunit-tests');
2042
- var result = getOrCreate('p', 'nodeunit-testresult');
2043
-
2044
- setText(userAgent, navigator.userAgent);
2045
-
2046
- nodeunit.runModules(modules, {
2047
- moduleStart: function (name) {
2048
- /*var mheading = document.createElement('h2');
2049
- mheading.innerText = name;
2050
- results.appendChild(mheading);
2051
- module = document.createElement('ol');
2052
- results.appendChild(module);*/
2053
- },
2054
- testDone: function (name, assertions) {
2055
- var test = document.createElement('li');
2056
- var strong = document.createElement('strong');
2057
- strong.innerHTML = name + ' <b style="color: black;">(' +
2058
- '<b class="fail">' + assertions.failures() + '</b>, ' +
2059
- '<b class="pass">' + assertions.passes() + '</b>, ' +
2060
- assertions.length +
2061
- ')</b>';
2062
- test.className = assertions.failures() ? 'fail': 'pass';
2063
- test.appendChild(strong);
2064
-
2065
- var aList = document.createElement('ol');
2066
- aList.style.display = 'none';
2067
- test.onclick = function () {
2068
- var d = aList.style.display;
2069
- aList.style.display = (d == 'none') ? 'block': 'none';
2070
- };
2071
- for (var i=0; i<assertions.length; i++) {
2072
- var li = document.createElement('li');
2073
- var a = assertions[i];
2074
- if (a.failed()) {
2075
- li.innerHTML = (a.message || a.method || 'no message') +
2076
- '<pre>' + (a.error.stack || a.error) + '</pre>';
2077
- li.className = 'fail';
2078
- }
2079
- else {
2080
- li.innerHTML = a.message || a.method || 'no message';
2081
- li.className = 'pass';
2082
- }
2083
- aList.appendChild(li);
2084
- }
2085
- test.appendChild(aList);
2086
- tests.appendChild(test);
2087
- },
2088
- done: function (assertions) {
2089
- var end = new Date().getTime();
2090
- var duration = end - start;
2091
-
2092
- var failures = assertions.failures();
2093
- banner.className = failures ? 'fail': 'pass';
2094
-
2095
- result.innerHTML = 'Tests completed in ' + duration +
2096
- ' milliseconds.<br/><span class="passed">' +
2097
- assertions.passes() + '</span> assertions of ' +
2098
- '<span class="all">' + assertions.length + '<span> passed, ' +
2099
- assertions.failures() + ' failed.';
2100
- }
2101
- });
2102
- };
2103
- })(reporter);
2104
- nodeunit = core;
2105
- nodeunit.assert = assert;
2106
- nodeunit.reporter = reporter;
2107
- nodeunit.run = reporter.run;
2108
- return nodeunit; })();
1
+ /*!
2
+ * Nodeunit
3
+ * https://github.com/caolan/nodeunit
4
+ * Copyright (c) 2010 Caolan McMahon
5
+ * MIT Licensed
6
+ *
7
+ * json2.js
8
+ * http://www.JSON.org/json2.js
9
+ * Public Domain.
10
+ * NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
11
+ */
12
+ nodeunit = (function(){
13
+ /*
14
+ http://www.JSON.org/json2.js
15
+ 2010-11-17
16
+
17
+ Public Domain.
18
+
19
+ NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
20
+
21
+ See http://www.JSON.org/js.html
22
+
23
+
24
+ This code should be minified before deployment.
25
+ See http://javascript.crockford.com/jsmin.html
26
+
27
+ USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO
28
+ NOT CONTROL.
29
+
30
+
31
+ This file creates a global JSON object containing two methods: stringify
32
+ and parse.
33
+
34
+ JSON.stringify(value, replacer, space)
35
+ value any JavaScript value, usually an object or array.
36
+
37
+ replacer an optional parameter that determines how object
38
+ values are stringified for objects. It can be a
39
+ function or an array of strings.
40
+
41
+ space an optional parameter that specifies the indentation
42
+ of nested structures. If it is omitted, the text will
43
+ be packed without extra whitespace. If it is a number,
44
+ it will specify the number of spaces to indent at each
45
+ level. If it is a string (such as '\t' or '&nbsp;'),
46
+ it contains the characters used to indent at each level.
47
+
48
+ This method produces a JSON text from a JavaScript value.
49
+
50
+ When an object value is found, if the object contains a toJSON
51
+ method, its toJSON method will be called and the result will be
52
+ stringified. A toJSON method does not serialize: it returns the
53
+ value represented by the name/value pair that should be serialized,
54
+ or undefined if nothing should be serialized. The toJSON method
55
+ will be passed the key associated with the value, and this will be
56
+ bound to the value
57
+
58
+ For example, this would serialize Dates as ISO strings.
59
+
60
+ Date.prototype.toJSON = function (key) {
61
+ function f(n) {
62
+ // Format integers to have at least two digits.
63
+ return n < 10 ? '0' + n : n;
64
+ }
65
+
66
+ return this.getUTCFullYear() + '-' +
67
+ f(this.getUTCMonth() + 1) + '-' +
68
+ f(this.getUTCDate()) + 'T' +
69
+ f(this.getUTCHours()) + ':' +
70
+ f(this.getUTCMinutes()) + ':' +
71
+ f(this.getUTCSeconds()) + 'Z';
72
+ };
73
+
74
+ You can provide an optional replacer method. It will be passed the
75
+ key and value of each member, with this bound to the containing
76
+ object. The value that is returned from your method will be
77
+ serialized. If your method returns undefined, then the member will
78
+ be excluded from the serialization.
79
+
80
+ If the replacer parameter is an array of strings, then it will be
81
+ used to select the members to be serialized. It filters the results
82
+ such that only members with keys listed in the replacer array are
83
+ stringified.
84
+
85
+ Values that do not have JSON representations, such as undefined or
86
+ functions, will not be serialized. Such values in objects will be
87
+ dropped; in arrays they will be replaced with null. You can use
88
+ a replacer function to replace those with JSON values.
89
+ JSON.stringify(undefined) returns undefined.
90
+
91
+ The optional space parameter produces a stringification of the
92
+ value that is filled with line breaks and indentation to make it
93
+ easier to read.
94
+
95
+ If the space parameter is a non-empty string, then that string will
96
+ be used for indentation. If the space parameter is a number, then
97
+ the indentation will be that many spaces.
98
+
99
+ Example:
100
+
101
+ text = JSON.stringify(['e', {pluribus: 'unum'}]);
102
+ // text is '["e",{"pluribus":"unum"}]'
103
+
104
+
105
+ text = JSON.stringify(['e', {pluribus: 'unum'}], null, '\t');
106
+ // text is '[\n\t"e",\n\t{\n\t\t"pluribus": "unum"\n\t}\n]'
107
+
108
+ text = JSON.stringify([new Date()], function (key, value) {
109
+ return this[key] instanceof Date ?
110
+ 'Date(' + this[key] + ')' : value;
111
+ });
112
+ // text is '["Date(---current time---)"]'
113
+
114
+
115
+ JSON.parse(text, reviver)
116
+ This method parses a JSON text to produce an object or array.
117
+ It can throw a SyntaxError exception.
118
+
119
+ The optional reviver parameter is a function that can filter and
120
+ transform the results. It receives each of the keys and values,
121
+ and its return value is used instead of the original value.
122
+ If it returns what it received, then the structure is not modified.
123
+ If it returns undefined then the member is deleted.
124
+
125
+ Example:
126
+
127
+ // Parse the text. Values that look like ISO date strings will
128
+ // be converted to Date objects.
129
+
130
+ myData = JSON.parse(text, function (key, value) {
131
+ var a;
132
+ if (typeof value === 'string') {
133
+ a =
134
+ /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value);
135
+ if (a) {
136
+ return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4],
137
+ +a[5], +a[6]));
138
+ }
139
+ }
140
+ return value;
141
+ });
142
+
143
+ myData = JSON.parse('["Date(09/09/2001)"]', function (key, value) {
144
+ var d;
145
+ if (typeof value === 'string' &&
146
+ value.slice(0, 5) === 'Date(' &&
147
+ value.slice(-1) === ')') {
148
+ d = new Date(value.slice(5, -1));
149
+ if (d) {
150
+ return d;
151
+ }
152
+ }
153
+ return value;
154
+ });
155
+
156
+
157
+ This is a reference implementation. You are free to copy, modify, or
158
+ redistribute.
159
+ */
160
+
161
+ /*jslint evil: true, strict: false, regexp: false */
162
+
163
+ /*members "", "\b", "\t", "\n", "\f", "\r", "\"", JSON, "\\", apply,
164
+ call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours,
165
+ getUTCMinutes, getUTCMonth, getUTCSeconds, hasOwnProperty, join,
166
+ lastIndex, length, parse, prototype, push, replace, slice, stringify,
167
+ test, toJSON, toString, valueOf
168
+ */
169
+
170
+
171
+ // Create a JSON object only if one does not already exist. We create the
172
+ // methods in a closure to avoid creating global variables.
173
+
174
+ var JSON = {};
175
+
176
+ (function () {
177
+ "use strict";
178
+
179
+ function f(n) {
180
+ // Format integers to have at least two digits.
181
+ return n < 10 ? '0' + n : n;
182
+ }
183
+
184
+ if (typeof Date.prototype.toJSON !== 'function') {
185
+
186
+ Date.prototype.toJSON = function (key) {
187
+
188
+ return isFinite(this.valueOf()) ?
189
+ this.getUTCFullYear() + '-' +
190
+ f(this.getUTCMonth() + 1) + '-' +
191
+ f(this.getUTCDate()) + 'T' +
192
+ f(this.getUTCHours()) + ':' +
193
+ f(this.getUTCMinutes()) + ':' +
194
+ f(this.getUTCSeconds()) + 'Z' : null;
195
+ };
196
+
197
+ String.prototype.toJSON =
198
+ Number.prototype.toJSON =
199
+ Boolean.prototype.toJSON = function (key) {
200
+ return this.valueOf();
201
+ };
202
+ }
203
+
204
+ var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
205
+ escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
206
+ gap,
207
+ indent,
208
+ meta = { // table of character substitutions
209
+ '\b': '\\b',
210
+ '\t': '\\t',
211
+ '\n': '\\n',
212
+ '\f': '\\f',
213
+ '\r': '\\r',
214
+ '"' : '\\"',
215
+ '\\': '\\\\'
216
+ },
217
+ rep;
218
+
219
+
220
+ function quote(string) {
221
+
222
+ // If the string contains no control characters, no quote characters, and no
223
+ // backslash characters, then we can safely slap some quotes around it.
224
+ // Otherwise we must also replace the offending characters with safe escape
225
+ // sequences.
226
+
227
+ escapable.lastIndex = 0;
228
+ return escapable.test(string) ?
229
+ '"' + string.replace(escapable, function (a) {
230
+ var c = meta[a];
231
+ return typeof c === 'string' ? c :
232
+ '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
233
+ }) + '"' :
234
+ '"' + string + '"';
235
+ }
236
+
237
+
238
+ function str(key, holder) {
239
+
240
+ // Produce a string from holder[key].
241
+
242
+ var i, // The loop counter.
243
+ k, // The member key.
244
+ v, // The member value.
245
+ length,
246
+ mind = gap,
247
+ partial,
248
+ value = holder[key];
249
+
250
+ // If the value has a toJSON method, call it to obtain a replacement value.
251
+
252
+ if (value && typeof value === 'object' &&
253
+ typeof value.toJSON === 'function') {
254
+ value = value.toJSON(key);
255
+ }
256
+
257
+ // If we were called with a replacer function, then call the replacer to
258
+ // obtain a replacement value.
259
+
260
+ if (typeof rep === 'function') {
261
+ value = rep.call(holder, key, value);
262
+ }
263
+
264
+ // What happens next depends on the value's type.
265
+
266
+ switch (typeof value) {
267
+ case 'string':
268
+ return quote(value);
269
+
270
+ case 'number':
271
+
272
+ // JSON numbers must be finite. Encode non-finite numbers as null.
273
+
274
+ return isFinite(value) ? String(value) : 'null';
275
+
276
+ case 'boolean':
277
+ case 'null':
278
+
279
+ // If the value is a boolean or null, convert it to a string. Note:
280
+ // typeof null does not produce 'null'. The case is included here in
281
+ // the remote chance that this gets fixed someday.
282
+
283
+ return String(value);
284
+
285
+ // If the type is 'object', we might be dealing with an object or an array or
286
+ // null.
287
+
288
+ case 'object':
289
+
290
+ // Due to a specification blunder in ECMAScript, typeof null is 'object',
291
+ // so watch out for that case.
292
+
293
+ if (!value) {
294
+ return 'null';
295
+ }
296
+
297
+ // Make an array to hold the partial results of stringifying this object value.
298
+
299
+ gap += indent;
300
+ partial = [];
301
+
302
+ // Is the value an array?
303
+
304
+ if (Object.prototype.toString.apply(value) === '[object Array]') {
305
+
306
+ // The value is an array. Stringify every element. Use null as a placeholder
307
+ // for non-JSON values.
308
+
309
+ length = value.length;
310
+ for (i = 0; i < length; i += 1) {
311
+ partial[i] = str(i, value) || 'null';
312
+ }
313
+
314
+ // Join all of the elements together, separated with commas, and wrap them in
315
+ // brackets.
316
+
317
+ v = partial.length === 0 ? '[]' :
318
+ gap ? '[\n' + gap +
319
+ partial.join(',\n' + gap) + '\n' +
320
+ mind + ']' :
321
+ '[' + partial.join(',') + ']';
322
+ gap = mind;
323
+ return v;
324
+ }
325
+
326
+ // If the replacer is an array, use it to select the members to be stringified.
327
+
328
+ if (rep && typeof rep === 'object') {
329
+ length = rep.length;
330
+ for (i = 0; i < length; i += 1) {
331
+ k = rep[i];
332
+ if (typeof k === 'string') {
333
+ v = str(k, value);
334
+ if (v) {
335
+ partial.push(quote(k) + (gap ? ': ' : ':') + v);
336
+ }
337
+ }
338
+ }
339
+ } else {
340
+
341
+ // Otherwise, iterate through all of the keys in the object.
342
+
343
+ for (k in value) {
344
+ if (Object.hasOwnProperty.call(value, k)) {
345
+ v = str(k, value);
346
+ if (v) {
347
+ partial.push(quote(k) + (gap ? ': ' : ':') + v);
348
+ }
349
+ }
350
+ }
351
+ }
352
+
353
+ // Join all of the member texts together, separated with commas,
354
+ // and wrap them in braces.
355
+
356
+ v = partial.length === 0 ? '{}' :
357
+ gap ? '{\n' + gap + partial.join(',\n' + gap) + '\n' +
358
+ mind + '}' : '{' + partial.join(',') + '}';
359
+ gap = mind;
360
+ return v;
361
+ }
362
+ }
363
+
364
+ // If the JSON object does not yet have a stringify method, give it one.
365
+
366
+ if (typeof JSON.stringify !== 'function') {
367
+ JSON.stringify = function (value, replacer, space) {
368
+
369
+ // The stringify method takes a value and an optional replacer, and an optional
370
+ // space parameter, and returns a JSON text. The replacer can be a function
371
+ // that can replace values, or an array of strings that will select the keys.
372
+ // A default replacer method can be provided. Use of the space parameter can
373
+ // produce text that is more easily readable.
374
+
375
+ var i;
376
+ gap = '';
377
+ indent = '';
378
+
379
+ // If the space parameter is a number, make an indent string containing that
380
+ // many spaces.
381
+
382
+ if (typeof space === 'number') {
383
+ for (i = 0; i < space; i += 1) {
384
+ indent += ' ';
385
+ }
386
+
387
+ // If the space parameter is a string, it will be used as the indent string.
388
+
389
+ } else if (typeof space === 'string') {
390
+ indent = space;
391
+ }
392
+
393
+ // If there is a replacer, it must be a function or an array.
394
+ // Otherwise, throw an error.
395
+
396
+ rep = replacer;
397
+ if (replacer && typeof replacer !== 'function' &&
398
+ (typeof replacer !== 'object' ||
399
+ typeof replacer.length !== 'number')) {
400
+ throw new Error('JSON.stringify');
401
+ }
402
+
403
+ // Make a fake root object containing our value under the key of ''.
404
+ // Return the result of stringifying the value.
405
+
406
+ return str('', {'': value});
407
+ };
408
+ }
409
+
410
+
411
+ // If the JSON object does not yet have a parse method, give it one.
412
+
413
+ if (typeof JSON.parse !== 'function') {
414
+ JSON.parse = function (text, reviver) {
415
+
416
+ // The parse method takes a text and an optional reviver function, and returns
417
+ // a JavaScript value if the text is a valid JSON text.
418
+
419
+ var j;
420
+
421
+ function walk(holder, key) {
422
+
423
+ // The walk method is used to recursively walk the resulting structure so
424
+ // that modifications can be made.
425
+
426
+ var k, v, value = holder[key];
427
+ if (value && typeof value === 'object') {
428
+ for (k in value) {
429
+ if (Object.hasOwnProperty.call(value, k)) {
430
+ v = walk(value, k);
431
+ if (v !== undefined) {
432
+ value[k] = v;
433
+ } else {
434
+ delete value[k];
435
+ }
436
+ }
437
+ }
438
+ }
439
+ return reviver.call(holder, key, value);
440
+ }
441
+
442
+
443
+ // Parsing happens in four stages. In the first stage, we replace certain
444
+ // Unicode characters with escape sequences. JavaScript handles many characters
445
+ // incorrectly, either silently deleting them, or treating them as line endings.
446
+
447
+ text = String(text);
448
+ cx.lastIndex = 0;
449
+ if (cx.test(text)) {
450
+ text = text.replace(cx, function (a) {
451
+ return '\\u' +
452
+ ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
453
+ });
454
+ }
455
+
456
+ // In the second stage, we run the text against regular expressions that look
457
+ // for non-JSON patterns. We are especially concerned with '()' and 'new'
458
+ // because they can cause invocation, and '=' because it can cause mutation.
459
+ // But just to be safe, we want to reject all unexpected forms.
460
+
461
+ // We split the second stage into 4 regexp operations in order to work around
462
+ // crippling inefficiencies in IE's and Safari's regexp engines. First we
463
+ // replace the JSON backslash pairs with '@' (a non-JSON character). Second, we
464
+ // replace all simple value tokens with ']' characters. Third, we delete all
465
+ // open brackets that follow a colon or comma or that begin the text. Finally,
466
+ // we look to see that the remaining characters are only whitespace or ']' or
467
+ // ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
468
+
469
+ if (/^[\],:{}\s]*$/
470
+ .test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@')
471
+ .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']')
472
+ .replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
473
+
474
+ // In the third stage we use the eval function to compile the text into a
475
+ // JavaScript structure. The '{' operator is subject to a syntactic ambiguity
476
+ // in JavaScript: it can begin a block or an object literal. We wrap the text
477
+ // in parens to eliminate the ambiguity.
478
+
479
+ j = eval('(' + text + ')');
480
+
481
+ // In the optional fourth stage, we recursively walk the new structure, passing
482
+ // each name/value pair to a reviver function for possible transformation.
483
+
484
+ return typeof reviver === 'function' ?
485
+ walk({'': j}, '') : j;
486
+ }
487
+
488
+ // If the text is not JSON parseable, then a SyntaxError is thrown.
489
+
490
+ throw new SyntaxError('JSON.parse');
491
+ };
492
+ }
493
+ }());
494
+ var assert = this.assert = {};
495
+ var types = {};
496
+ var core = {};
497
+ var nodeunit = {};
498
+ var reporter = {};
499
+ /*global setTimeout: false, console: false */
500
+ (function () {
501
+
502
+ var async = {};
503
+
504
+ // global on the server, window in the browser
505
+ var root = this,
506
+ previous_async = root.async;
507
+
508
+ if (typeof module !== 'undefined' && module.exports) {
509
+ module.exports = async;
510
+ }
511
+ else {
512
+ root.async = async;
513
+ }
514
+
515
+ async.noConflict = function () {
516
+ root.async = previous_async;
517
+ return async;
518
+ };
519
+
520
+ //// cross-browser compatiblity functions ////
521
+
522
+ var _forEach = function (arr, iterator) {
523
+ if (arr.forEach) {
524
+ return arr.forEach(iterator);
525
+ }
526
+ for (var i = 0; i < arr.length; i += 1) {
527
+ iterator(arr[i], i, arr);
528
+ }
529
+ };
530
+
531
+ var _map = function (arr, iterator) {
532
+ if (arr.map) {
533
+ return arr.map(iterator);
534
+ }
535
+ var results = [];
536
+ _forEach(arr, function (x, i, a) {
537
+ results.push(iterator(x, i, a));
538
+ });
539
+ return results;
540
+ };
541
+
542
+ var _reduce = function (arr, iterator, memo) {
543
+ if (arr.reduce) {
544
+ return arr.reduce(iterator, memo);
545
+ }
546
+ _forEach(arr, function (x, i, a) {
547
+ memo = iterator(memo, x, i, a);
548
+ });
549
+ return memo;
550
+ };
551
+
552
+ var _keys = function (obj) {
553
+ if (Object.keys) {
554
+ return Object.keys(obj);
555
+ }
556
+ var keys = [];
557
+ for (var k in obj) {
558
+ if (obj.hasOwnProperty(k)) {
559
+ keys.push(k);
560
+ }
561
+ }
562
+ return keys;
563
+ };
564
+
565
+ var _indexOf = function (arr, item) {
566
+ if (arr.indexOf) {
567
+ return arr.indexOf(item);
568
+ }
569
+ for (var i = 0; i < arr.length; i += 1) {
570
+ if (arr[i] === item) {
571
+ return i;
572
+ }
573
+ }
574
+ return -1;
575
+ };
576
+
577
+ //// exported async module functions ////
578
+
579
+ //// nextTick implementation with browser-compatible fallback ////
580
+ if (typeof process === 'undefined' || !(process.nextTick)) {
581
+ async.nextTick = function (fn) {
582
+ setTimeout(fn, 0);
583
+ };
584
+ }
585
+ else {
586
+ async.nextTick = process.nextTick;
587
+ }
588
+
589
+ async.forEach = function (arr, iterator, callback) {
590
+ if (!arr.length) {
591
+ return callback();
592
+ }
593
+ var completed = 0;
594
+ _forEach(arr, function (x) {
595
+ iterator(x, function (err) {
596
+ if (err) {
597
+ callback(err);
598
+ callback = function () {};
599
+ }
600
+ else {
601
+ completed += 1;
602
+ if (completed === arr.length) {
603
+ callback();
604
+ }
605
+ }
606
+ });
607
+ });
608
+ };
609
+
610
+ async.forEachSeries = function (arr, iterator, callback) {
611
+ if (!arr.length) {
612
+ return callback();
613
+ }
614
+ var completed = 0;
615
+ var iterate = function () {
616
+ iterator(arr[completed], function (err) {
617
+ if (err) {
618
+ callback(err);
619
+ callback = function () {};
620
+ }
621
+ else {
622
+ completed += 1;
623
+ if (completed === arr.length) {
624
+ callback();
625
+ }
626
+ else {
627
+ iterate();
628
+ }
629
+ }
630
+ });
631
+ };
632
+ iterate();
633
+ };
634
+
635
+
636
+ var doParallel = function (fn) {
637
+ return function () {
638
+ var args = Array.prototype.slice.call(arguments);
639
+ return fn.apply(null, [async.forEach].concat(args));
640
+ };
641
+ };
642
+ var doSeries = function (fn) {
643
+ return function () {
644
+ var args = Array.prototype.slice.call(arguments);
645
+ return fn.apply(null, [async.forEachSeries].concat(args));
646
+ };
647
+ };
648
+
649
+
650
+ var _asyncMap = function (eachfn, arr, iterator, callback) {
651
+ var results = [];
652
+ arr = _map(arr, function (x, i) {
653
+ return {index: i, value: x};
654
+ });
655
+ eachfn(arr, function (x, callback) {
656
+ iterator(x.value, function (err, v) {
657
+ results[x.index] = v;
658
+ callback(err);
659
+ });
660
+ }, function (err) {
661
+ callback(err, results);
662
+ });
663
+ };
664
+ async.map = doParallel(_asyncMap);
665
+ async.mapSeries = doSeries(_asyncMap);
666
+
667
+
668
+ // reduce only has a series version, as doing reduce in parallel won't
669
+ // work in many situations.
670
+ async.reduce = function (arr, memo, iterator, callback) {
671
+ async.forEachSeries(arr, function (x, callback) {
672
+ iterator(memo, x, function (err, v) {
673
+ memo = v;
674
+ callback(err);
675
+ });
676
+ }, function (err) {
677
+ callback(err, memo);
678
+ });
679
+ };
680
+ // inject alias
681
+ async.inject = async.reduce;
682
+ // foldl alias
683
+ async.foldl = async.reduce;
684
+
685
+ async.reduceRight = function (arr, memo, iterator, callback) {
686
+ var reversed = _map(arr, function (x) {
687
+ return x;
688
+ }).reverse();
689
+ async.reduce(reversed, memo, iterator, callback);
690
+ };
691
+ // foldr alias
692
+ async.foldr = async.reduceRight;
693
+
694
+ var _filter = function (eachfn, arr, iterator, callback) {
695
+ var results = [];
696
+ arr = _map(arr, function (x, i) {
697
+ return {index: i, value: x};
698
+ });
699
+ eachfn(arr, function (x, callback) {
700
+ iterator(x.value, function (v) {
701
+ if (v) {
702
+ results.push(x);
703
+ }
704
+ callback();
705
+ });
706
+ }, function (err) {
707
+ callback(_map(results.sort(function (a, b) {
708
+ return a.index - b.index;
709
+ }), function (x) {
710
+ return x.value;
711
+ }));
712
+ });
713
+ };
714
+ async.filter = doParallel(_filter);
715
+ async.filterSeries = doSeries(_filter);
716
+ // select alias
717
+ async.select = async.filter;
718
+ async.selectSeries = async.filterSeries;
719
+
720
+ var _reject = function (eachfn, arr, iterator, callback) {
721
+ var results = [];
722
+ arr = _map(arr, function (x, i) {
723
+ return {index: i, value: x};
724
+ });
725
+ eachfn(arr, function (x, callback) {
726
+ iterator(x.value, function (v) {
727
+ if (!v) {
728
+ results.push(x);
729
+ }
730
+ callback();
731
+ });
732
+ }, function (err) {
733
+ callback(_map(results.sort(function (a, b) {
734
+ return a.index - b.index;
735
+ }), function (x) {
736
+ return x.value;
737
+ }));
738
+ });
739
+ };
740
+ async.reject = doParallel(_reject);
741
+ async.rejectSeries = doSeries(_reject);
742
+
743
+ var _detect = function (eachfn, arr, iterator, main_callback) {
744
+ eachfn(arr, function (x, callback) {
745
+ iterator(x, function (result) {
746
+ if (result) {
747
+ main_callback(x);
748
+ }
749
+ else {
750
+ callback();
751
+ }
752
+ });
753
+ }, function (err) {
754
+ main_callback();
755
+ });
756
+ };
757
+ async.detect = doParallel(_detect);
758
+ async.detectSeries = doSeries(_detect);
759
+
760
+ async.some = function (arr, iterator, main_callback) {
761
+ async.forEach(arr, function (x, callback) {
762
+ iterator(x, function (v) {
763
+ if (v) {
764
+ main_callback(true);
765
+ main_callback = function () {};
766
+ }
767
+ callback();
768
+ });
769
+ }, function (err) {
770
+ main_callback(false);
771
+ });
772
+ };
773
+ // any alias
774
+ async.any = async.some;
775
+
776
+ async.every = function (arr, iterator, main_callback) {
777
+ async.forEach(arr, function (x, callback) {
778
+ iterator(x, function (v) {
779
+ if (!v) {
780
+ main_callback(false);
781
+ main_callback = function () {};
782
+ }
783
+ callback();
784
+ });
785
+ }, function (err) {
786
+ main_callback(true);
787
+ });
788
+ };
789
+ // all alias
790
+ async.all = async.every;
791
+
792
+ async.sortBy = function (arr, iterator, callback) {
793
+ async.map(arr, function (x, callback) {
794
+ iterator(x, function (err, criteria) {
795
+ if (err) {
796
+ callback(err);
797
+ }
798
+ else {
799
+ callback(null, {value: x, criteria: criteria});
800
+ }
801
+ });
802
+ }, function (err, results) {
803
+ if (err) {
804
+ return callback(err);
805
+ }
806
+ else {
807
+ var fn = function (left, right) {
808
+ var a = left.criteria, b = right.criteria;
809
+ return a < b ? -1 : a > b ? 1 : 0;
810
+ };
811
+ callback(null, _map(results.sort(fn), function (x) {
812
+ return x.value;
813
+ }));
814
+ }
815
+ });
816
+ };
817
+
818
+ async.auto = function (tasks, callback) {
819
+ callback = callback || function () {};
820
+ var keys = _keys(tasks);
821
+ if (!keys.length) {
822
+ return callback(null);
823
+ }
824
+
825
+ var completed = [];
826
+
827
+ var listeners = [];
828
+ var addListener = function (fn) {
829
+ listeners.unshift(fn);
830
+ };
831
+ var removeListener = function (fn) {
832
+ for (var i = 0; i < listeners.length; i += 1) {
833
+ if (listeners[i] === fn) {
834
+ listeners.splice(i, 1);
835
+ return;
836
+ }
837
+ }
838
+ };
839
+ var taskComplete = function () {
840
+ _forEach(listeners, function (fn) {
841
+ fn();
842
+ });
843
+ };
844
+
845
+ addListener(function () {
846
+ if (completed.length === keys.length) {
847
+ callback(null);
848
+ }
849
+ });
850
+
851
+ _forEach(keys, function (k) {
852
+ var task = (tasks[k] instanceof Function) ? [tasks[k]]: tasks[k];
853
+ var taskCallback = function (err) {
854
+ if (err) {
855
+ callback(err);
856
+ // stop subsequent errors hitting callback multiple times
857
+ callback = function () {};
858
+ }
859
+ else {
860
+ completed.push(k);
861
+ taskComplete();
862
+ }
863
+ };
864
+ var requires = task.slice(0, Math.abs(task.length - 1)) || [];
865
+ var ready = function () {
866
+ return _reduce(requires, function (a, x) {
867
+ return (a && _indexOf(completed, x) !== -1);
868
+ }, true);
869
+ };
870
+ if (ready()) {
871
+ task[task.length - 1](taskCallback);
872
+ }
873
+ else {
874
+ var listener = function () {
875
+ if (ready()) {
876
+ removeListener(listener);
877
+ task[task.length - 1](taskCallback);
878
+ }
879
+ };
880
+ addListener(listener);
881
+ }
882
+ });
883
+ };
884
+
885
+ async.waterfall = function (tasks, callback) {
886
+ if (!tasks.length) {
887
+ return callback();
888
+ }
889
+ callback = callback || function () {};
890
+ var wrapIterator = function (iterator) {
891
+ return function (err) {
892
+ if (err) {
893
+ callback(err);
894
+ callback = function () {};
895
+ }
896
+ else {
897
+ var args = Array.prototype.slice.call(arguments, 1);
898
+ var next = iterator.next();
899
+ if (next) {
900
+ args.push(wrapIterator(next));
901
+ }
902
+ else {
903
+ args.push(callback);
904
+ }
905
+ async.nextTick(function () {
906
+ iterator.apply(null, args);
907
+ });
908
+ }
909
+ };
910
+ };
911
+ wrapIterator(async.iterator(tasks))();
912
+ };
913
+
914
+ async.parallel = function (tasks, callback) {
915
+ callback = callback || function () {};
916
+ if (tasks.constructor === Array) {
917
+ async.map(tasks, function (fn, callback) {
918
+ if (fn) {
919
+ fn(function (err) {
920
+ var args = Array.prototype.slice.call(arguments, 1);
921
+ if (args.length <= 1) {
922
+ args = args[0];
923
+ }
924
+ callback.call(null, err, args || null);
925
+ });
926
+ }
927
+ }, callback);
928
+ }
929
+ else {
930
+ var results = {};
931
+ async.forEach(_keys(tasks), function (k, callback) {
932
+ tasks[k](function (err) {
933
+ var args = Array.prototype.slice.call(arguments, 1);
934
+ if (args.length <= 1) {
935
+ args = args[0];
936
+ }
937
+ results[k] = args;
938
+ callback(err);
939
+ });
940
+ }, function (err) {
941
+ callback(err, results);
942
+ });
943
+ }
944
+ };
945
+
946
+ async.series = function (tasks, callback) {
947
+ callback = callback || function () {};
948
+ if (tasks.constructor === Array) {
949
+ async.mapSeries(tasks, function (fn, callback) {
950
+ if (fn) {
951
+ fn(function (err) {
952
+ var args = Array.prototype.slice.call(arguments, 1);
953
+ if (args.length <= 1) {
954
+ args = args[0];
955
+ }
956
+ callback.call(null, err, args || null);
957
+ });
958
+ }
959
+ }, callback);
960
+ }
961
+ else {
962
+ var results = {};
963
+ async.forEachSeries(_keys(tasks), function (k, callback) {
964
+ tasks[k](function (err) {
965
+ var args = Array.prototype.slice.call(arguments, 1);
966
+ if (args.length <= 1) {
967
+ args = args[0];
968
+ }
969
+ results[k] = args;
970
+ callback(err);
971
+ });
972
+ }, function (err) {
973
+ callback(err, results);
974
+ });
975
+ }
976
+ };
977
+
978
+ async.iterator = function (tasks) {
979
+ var makeCallback = function (index) {
980
+ var fn = function () {
981
+ if (tasks.length) {
982
+ tasks[index].apply(null, arguments);
983
+ }
984
+ return fn.next();
985
+ };
986
+ fn.next = function () {
987
+ return (index < tasks.length - 1) ? makeCallback(index + 1): null;
988
+ };
989
+ return fn;
990
+ };
991
+ return makeCallback(0);
992
+ };
993
+
994
+ async.apply = function (fn) {
995
+ var args = Array.prototype.slice.call(arguments, 1);
996
+ return function () {
997
+ return fn.apply(
998
+ null, args.concat(Array.prototype.slice.call(arguments))
999
+ );
1000
+ };
1001
+ };
1002
+
1003
+ var _concat = function (eachfn, arr, fn, callback) {
1004
+ var r = [];
1005
+ eachfn(arr, function (x, cb) {
1006
+ fn(x, function (err, y) {
1007
+ r = r.concat(y || []);
1008
+ cb(err);
1009
+ });
1010
+ }, function (err) {
1011
+ callback(err, r);
1012
+ });
1013
+ };
1014
+ async.concat = doParallel(_concat);
1015
+ async.concatSeries = doSeries(_concat);
1016
+
1017
+ async.whilst = function (test, iterator, callback) {
1018
+ if (test()) {
1019
+ iterator(function (err) {
1020
+ if (err) {
1021
+ return callback(err);
1022
+ }
1023
+ async.whilst(test, iterator, callback);
1024
+ });
1025
+ }
1026
+ else {
1027
+ callback();
1028
+ }
1029
+ };
1030
+
1031
+ async.until = function (test, iterator, callback) {
1032
+ if (!test()) {
1033
+ iterator(function (err) {
1034
+ if (err) {
1035
+ return callback(err);
1036
+ }
1037
+ async.until(test, iterator, callback);
1038
+ });
1039
+ }
1040
+ else {
1041
+ callback();
1042
+ }
1043
+ };
1044
+
1045
+ async.queue = function (worker, concurrency) {
1046
+ var workers = 0;
1047
+ var tasks = [];
1048
+ var q = {
1049
+ concurrency: concurrency,
1050
+ push: function (data, callback) {
1051
+ tasks.push({data: data, callback: callback});
1052
+ async.nextTick(q.process);
1053
+ },
1054
+ process: function () {
1055
+ if (workers < q.concurrency && tasks.length) {
1056
+ var task = tasks.splice(0, 1)[0];
1057
+ workers += 1;
1058
+ worker(task.data, function () {
1059
+ workers -= 1;
1060
+ if (task.callback) {
1061
+ task.callback.apply(task, arguments);
1062
+ }
1063
+ q.process();
1064
+ });
1065
+ }
1066
+ },
1067
+ length: function () {
1068
+ return tasks.length;
1069
+ }
1070
+ };
1071
+ return q;
1072
+ };
1073
+
1074
+ var _console_fn = function (name) {
1075
+ return function (fn) {
1076
+ var args = Array.prototype.slice.call(arguments, 1);
1077
+ fn.apply(null, args.concat([function (err) {
1078
+ var args = Array.prototype.slice.call(arguments, 1);
1079
+ if (typeof console !== 'undefined') {
1080
+ if (err) {
1081
+ if (console.error) {
1082
+ console.error(err);
1083
+ }
1084
+ }
1085
+ else if (console[name]) {
1086
+ _forEach(args, function (x) {
1087
+ console[name](x);
1088
+ });
1089
+ }
1090
+ }
1091
+ }]));
1092
+ };
1093
+ };
1094
+ async.log = _console_fn('log');
1095
+ async.dir = _console_fn('dir');
1096
+ /*async.info = _console_fn('info');
1097
+ async.warn = _console_fn('warn');
1098
+ async.error = _console_fn('error');*/
1099
+
1100
+ async.memoize = function (fn, hasher) {
1101
+ var memo = {};
1102
+ hasher = hasher || function (x) {
1103
+ return x;
1104
+ };
1105
+ return function () {
1106
+ var args = Array.prototype.slice.call(arguments);
1107
+ var callback = args.pop();
1108
+ var key = hasher.apply(null, args);
1109
+ if (key in memo) {
1110
+ callback.apply(null, memo[key]);
1111
+ }
1112
+ else {
1113
+ fn.apply(null, args.concat([function () {
1114
+ memo[key] = arguments;
1115
+ callback.apply(null, arguments);
1116
+ }]));
1117
+ }
1118
+ };
1119
+ };
1120
+
1121
+ }());
1122
+ (function(exports){
1123
+ /**
1124
+ * This file is based on the node.js assert module, but with some small
1125
+ * changes for browser-compatibility
1126
+ * THIS FILE SHOULD BE BROWSER-COMPATIBLE JS!
1127
+ */
1128
+
1129
+
1130
+ /**
1131
+ * Added for browser compatibility
1132
+ */
1133
+
1134
+ var _keys = function(obj){
1135
+ if(Object.keys) return Object.keys(obj);
1136
+ if (typeof obj != 'object' && typeof obj != 'function') {
1137
+ throw new TypeError('-');
1138
+ }
1139
+ var keys = [];
1140
+ for(var k in obj){
1141
+ if(obj.hasOwnProperty(k)) keys.push(k);
1142
+ }
1143
+ return keys;
1144
+ };
1145
+
1146
+
1147
+
1148
+ // http://wiki.commonjs.org/wiki/Unit_Testing/1.0
1149
+ //
1150
+ // THIS IS NOT TESTED NOR LIKELY TO WORK OUTSIDE V8!
1151
+ //
1152
+ // Originally from narwhal.js (http://narwhaljs.org)
1153
+ // Copyright (c) 2009 Thomas Robinson <280north.com>
1154
+ //
1155
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
1156
+ // of this software and associated documentation files (the 'Software'), to
1157
+ // deal in the Software without restriction, including without limitation the
1158
+ // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1159
+ // sell copies of the Software, and to permit persons to whom the Software is
1160
+ // furnished to do so, subject to the following conditions:
1161
+ //
1162
+ // The above copyright notice and this permission notice shall be included in
1163
+ // all copies or substantial portions of the Software.
1164
+ //
1165
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1166
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1167
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1168
+ // AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
1169
+ // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
1170
+ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1171
+
1172
+
1173
+ var pSlice = Array.prototype.slice;
1174
+
1175
+ // 1. The assert module provides functions that throw
1176
+ // AssertionError's when particular conditions are not met. The
1177
+ // assert module must conform to the following interface.
1178
+
1179
+ var assert = exports;
1180
+
1181
+ // 2. The AssertionError is defined in assert.
1182
+ // new assert.AssertionError({message: message, actual: actual, expected: expected})
1183
+
1184
+ assert.AssertionError = function AssertionError (options) {
1185
+ this.name = "AssertionError";
1186
+ this.message = options.message;
1187
+ this.actual = options.actual;
1188
+ this.expected = options.expected;
1189
+ this.operator = options.operator;
1190
+ var stackStartFunction = options.stackStartFunction || fail;
1191
+
1192
+ if (Error.captureStackTrace) {
1193
+ Error.captureStackTrace(this, stackStartFunction);
1194
+ }
1195
+ };
1196
+ // code from util.inherits in node
1197
+ assert.AssertionError.super_ = Error;
1198
+
1199
+
1200
+ // EDITED FOR BROWSER COMPATIBILITY: replaced Object.create call
1201
+ // TODO: test what effect this may have
1202
+ var ctor = function () { this.constructor = assert.AssertionError; };
1203
+ ctor.prototype = Error.prototype;
1204
+ assert.AssertionError.prototype = new ctor();
1205
+
1206
+
1207
+ assert.AssertionError.prototype.toString = function() {
1208
+ if (this.message) {
1209
+ return [this.name+":", this.message].join(' ');
1210
+ } else {
1211
+ return [ this.name+":"
1212
+ , JSON.stringify(this.expected )
1213
+ , this.operator
1214
+ , JSON.stringify(this.actual)
1215
+ ].join(" ");
1216
+ }
1217
+ };
1218
+
1219
+ // assert.AssertionError instanceof Error
1220
+
1221
+ assert.AssertionError.__proto__ = Error.prototype;
1222
+
1223
+ // At present only the three keys mentioned above are used and
1224
+ // understood by the spec. Implementations or sub modules can pass
1225
+ // other keys to the AssertionError's constructor - they will be
1226
+ // ignored.
1227
+
1228
+ // 3. All of the following functions must throw an AssertionError
1229
+ // when a corresponding condition is not met, with a message that
1230
+ // may be undefined if not provided. All assertion methods provide
1231
+ // both the actual and expected values to the assertion error for
1232
+ // display purposes.
1233
+
1234
+ function fail(actual, expected, message, operator, stackStartFunction) {
1235
+ throw new assert.AssertionError({
1236
+ message: message,
1237
+ actual: actual,
1238
+ expected: expected,
1239
+ operator: operator,
1240
+ stackStartFunction: stackStartFunction
1241
+ });
1242
+ }
1243
+
1244
+ // EXTENSION! allows for well behaved errors defined elsewhere.
1245
+ assert.fail = fail;
1246
+
1247
+ // 4. Pure assertion tests whether a value is truthy, as determined
1248
+ // by !!guard.
1249
+ // assert.ok(guard, message_opt);
1250
+ // This statement is equivalent to assert.equal(true, guard,
1251
+ // message_opt);. To test strictly for the value true, use
1252
+ // assert.strictEqual(true, guard, message_opt);.
1253
+
1254
+ assert.ok = function ok(value, message) {
1255
+ if (!!!value) fail(value, true, message, "==", assert.ok);
1256
+ };
1257
+
1258
+ // 5. The equality assertion tests shallow, coercive equality with
1259
+ // ==.
1260
+ // assert.equal(actual, expected, message_opt);
1261
+
1262
+ assert.equal = function equal(actual, expected, message) {
1263
+ if (actual != expected) fail(actual, expected, message, "==", assert.equal);
1264
+ };
1265
+
1266
+ // 6. The non-equality assertion tests for whether two objects are not equal
1267
+ // with != assert.notEqual(actual, expected, message_opt);
1268
+
1269
+ assert.notEqual = function notEqual(actual, expected, message) {
1270
+ if (actual == expected) {
1271
+ fail(actual, expected, message, "!=", assert.notEqual);
1272
+ }
1273
+ };
1274
+
1275
+ // 7. The equivalence assertion tests a deep equality relation.
1276
+ // assert.deepEqual(actual, expected, message_opt);
1277
+
1278
+ assert.deepEqual = function deepEqual(actual, expected, message) {
1279
+ if (!_deepEqual(actual, expected)) {
1280
+ fail(actual, expected, message, "deepEqual", assert.deepEqual);
1281
+ }
1282
+ };
1283
+
1284
+ var Buffer = null;
1285
+ if (typeof require !== 'undefined' && typeof process !== 'undefined') {
1286
+ try {
1287
+ Buffer = require('buffer').Buffer;
1288
+ }
1289
+ catch (e) {
1290
+ // May be a CommonJS environment other than Node.js
1291
+ Buffer = null;
1292
+ }
1293
+ }
1294
+
1295
+ function _deepEqual(actual, expected) {
1296
+ // 7.1. All identical values are equivalent, as determined by ===.
1297
+ if (actual === expected) {
1298
+ return true;
1299
+ // 7.2. If the expected value is a Date object, the actual value is
1300
+ // equivalent if it is also a Date object that refers to the same time.
1301
+ } else if (actual instanceof Date && expected instanceof Date) {
1302
+ return actual.getTime() === expected.getTime();
1303
+
1304
+ // 7.2.1 If the expcted value is a RegExp object, the actual value is
1305
+ // equivalent if it is also a RegExp object that refers to the same source and options
1306
+ } else if (actual instanceof RegExp && expected instanceof RegExp) {
1307
+ return actual.source === expected.source &&
1308
+ actual.global === expected.global &&
1309
+ actual.ignoreCase === expected.ignoreCase &&
1310
+ actual.multiline === expected.multiline;
1311
+
1312
+ } else if (Buffer && actual instanceof Buffer && expected instanceof Buffer) {
1313
+ return (function() {
1314
+ var i, len;
1315
+
1316
+ for (i = 0, len = expected.length; i < len; i++) {
1317
+ if (actual[i] !== expected[i]) {
1318
+ return false;
1319
+ }
1320
+ }
1321
+ return actual.length === expected.length;
1322
+ })();
1323
+ // 7.3. Other pairs that do not both pass typeof value == "object",
1324
+ // equivalence is determined by ==.
1325
+ } else if (typeof actual != 'object' && typeof expected != 'object') {
1326
+ return actual == expected;
1327
+
1328
+ // 7.4. For all other Object pairs, including Array objects, equivalence is
1329
+ // determined by having the same number of owned properties (as verified
1330
+ // with Object.prototype.hasOwnProperty.call), the same set of keys
1331
+ // (although not necessarily the same order), equivalent values for every
1332
+ // corresponding key, and an identical "prototype" property. Note: this
1333
+ // accounts for both named and indexed properties on Arrays.
1334
+ } else {
1335
+ return objEquiv(actual, expected);
1336
+ }
1337
+ }
1338
+
1339
+ function isUndefinedOrNull (value) {
1340
+ return value === null || value === undefined;
1341
+ }
1342
+
1343
+ function isArguments (object) {
1344
+ return Object.prototype.toString.call(object) == '[object Arguments]';
1345
+ }
1346
+
1347
+ function objEquiv (a, b) {
1348
+ if (isUndefinedOrNull(a) || isUndefinedOrNull(b))
1349
+ return false;
1350
+ // an identical "prototype" property.
1351
+ if (a.prototype !== b.prototype) return false;
1352
+ //~~~I've managed to break Object.keys through screwy arguments passing.
1353
+ // Converting to array solves the problem.
1354
+ if (isArguments(a)) {
1355
+ if (!isArguments(b)) {
1356
+ return false;
1357
+ }
1358
+ a = pSlice.call(a);
1359
+ b = pSlice.call(b);
1360
+ return _deepEqual(a, b);
1361
+ }
1362
+ try{
1363
+ var ka = _keys(a),
1364
+ kb = _keys(b),
1365
+ key, i;
1366
+ } catch (e) {//happens when one is a string literal and the other isn't
1367
+ return false;
1368
+ }
1369
+ // having the same number of owned properties (keys incorporates hasOwnProperty)
1370
+ if (ka.length != kb.length)
1371
+ return false;
1372
+ //the same set of keys (although not necessarily the same order),
1373
+ ka.sort();
1374
+ kb.sort();
1375
+ //~~~cheap key test
1376
+ for (i = ka.length - 1; i >= 0; i--) {
1377
+ if (ka[i] != kb[i])
1378
+ return false;
1379
+ }
1380
+ //equivalent values for every corresponding key, and
1381
+ //~~~possibly expensive deep test
1382
+ for (i = ka.length - 1; i >= 0; i--) {
1383
+ key = ka[i];
1384
+ if (!_deepEqual(a[key], b[key] ))
1385
+ return false;
1386
+ }
1387
+ return true;
1388
+ }
1389
+
1390
+ // 8. The non-equivalence assertion tests for any deep inequality.
1391
+ // assert.notDeepEqual(actual, expected, message_opt);
1392
+
1393
+ assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
1394
+ if (_deepEqual(actual, expected)) {
1395
+ fail(actual, expected, message, "notDeepEqual", assert.notDeepEqual);
1396
+ }
1397
+ };
1398
+
1399
+ // 9. The strict equality assertion tests strict equality, as determined by ===.
1400
+ // assert.strictEqual(actual, expected, message_opt);
1401
+
1402
+ assert.strictEqual = function strictEqual(actual, expected, message) {
1403
+ if (actual !== expected) {
1404
+ fail(actual, expected, message, "===", assert.strictEqual);
1405
+ }
1406
+ };
1407
+
1408
+ // 10. The strict non-equality assertion tests for strict inequality, as determined by !==.
1409
+ // assert.notStrictEqual(actual, expected, message_opt);
1410
+
1411
+ assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
1412
+ if (actual === expected) {
1413
+ fail(actual, expected, message, "!==", assert.notStrictEqual);
1414
+ }
1415
+ };
1416
+
1417
+ function expectedException(actual, expected) {
1418
+ if (!actual || !expected) {
1419
+ return false;
1420
+ }
1421
+
1422
+ if (expected instanceof RegExp) {
1423
+ return expected.test(actual.message || actual);
1424
+ } else if (actual instanceof expected) {
1425
+ return true;
1426
+ } else if (expected.call({}, actual) === true) {
1427
+ return true;
1428
+ }
1429
+
1430
+ return false;
1431
+ }
1432
+
1433
+ function _throws(shouldThrow, block, expected, message) {
1434
+ var actual;
1435
+
1436
+ if (typeof expected === 'string') {
1437
+ message = expected;
1438
+ expected = null;
1439
+ }
1440
+
1441
+ try {
1442
+ block();
1443
+ } catch (e) {
1444
+ actual = e;
1445
+ }
1446
+
1447
+ message = (expected && expected.name ? ' (' + expected.name + ').' : '.') +
1448
+ (message ? ' ' + message : '.');
1449
+
1450
+ if (shouldThrow && !actual) {
1451
+ fail('Missing expected exception' + message);
1452
+ }
1453
+
1454
+ if (!shouldThrow && expectedException(actual, expected)) {
1455
+ fail('Got unwanted exception' + message);
1456
+ }
1457
+
1458
+ if ((shouldThrow && actual && expected &&
1459
+ !expectedException(actual, expected)) || (!shouldThrow && actual)) {
1460
+ throw actual;
1461
+ }
1462
+ }
1463
+
1464
+ // 11. Expected to throw an error:
1465
+ // assert.throws(block, Error_opt, message_opt);
1466
+
1467
+ assert.throws = function(block, /*optional*/error, /*optional*/message) {
1468
+ _throws.apply(this, [true].concat(pSlice.call(arguments)));
1469
+ };
1470
+
1471
+ // EXTENSION! This is annoying to write outside this module.
1472
+ assert.doesNotThrow = function(block, /*optional*/error, /*optional*/message) {
1473
+ _throws.apply(this, [false].concat(pSlice.call(arguments)));
1474
+ };
1475
+
1476
+ assert.ifError = function (err) { if (err) {throw err;}};
1477
+ })(assert);
1478
+ (function(exports){
1479
+ /*!
1480
+ * Nodeunit
1481
+ * Copyright (c) 2010 Caolan McMahon
1482
+ * MIT Licensed
1483
+ *
1484
+ * THIS FILE SHOULD BE BROWSER-COMPATIBLE JS!
1485
+ * Only code on that line will be removed, it's mostly to avoid requiring code
1486
+ * that is node specific
1487
+ */
1488
+
1489
+ /**
1490
+ * Module dependencies
1491
+ */
1492
+
1493
+
1494
+
1495
+ /**
1496
+ * Creates assertion objects representing the result of an assert call.
1497
+ * Accepts an object or AssertionError as its argument.
1498
+ *
1499
+ * @param {object} obj
1500
+ * @api public
1501
+ */
1502
+
1503
+ exports.assertion = function (obj) {
1504
+ return {
1505
+ method: obj.method || '',
1506
+ message: obj.message || (obj.error && obj.error.message) || '',
1507
+ error: obj.error,
1508
+ passed: function () {
1509
+ return !this.error;
1510
+ },
1511
+ failed: function () {
1512
+ return Boolean(this.error);
1513
+ }
1514
+ };
1515
+ };
1516
+
1517
+ /**
1518
+ * Creates an assertion list object representing a group of assertions.
1519
+ * Accepts an array of assertion objects.
1520
+ *
1521
+ * @param {Array} arr
1522
+ * @param {Number} duration
1523
+ * @api public
1524
+ */
1525
+
1526
+ exports.assertionList = function (arr, duration) {
1527
+ var that = arr || [];
1528
+ that.failures = function () {
1529
+ var failures = 0;
1530
+ for (var i = 0; i < this.length; i += 1) {
1531
+ if (this[i].failed()) {
1532
+ failures += 1;
1533
+ }
1534
+ }
1535
+ return failures;
1536
+ };
1537
+ that.passes = function () {
1538
+ return that.length - that.failures();
1539
+ };
1540
+ that.duration = duration || 0;
1541
+ return that;
1542
+ };
1543
+
1544
+ /**
1545
+ * Create a wrapper function for assert module methods. Executes a callback
1546
+ * after it's complete with an assertion object representing the result.
1547
+ *
1548
+ * @param {Function} callback
1549
+ * @api private
1550
+ */
1551
+
1552
+ var assertWrapper = function (callback) {
1553
+ return function (new_method, assert_method, arity) {
1554
+ return function () {
1555
+ var message = arguments[arity - 1];
1556
+ var a = exports.assertion({method: new_method, message: message});
1557
+ try {
1558
+ assert[assert_method].apply(null, arguments);
1559
+ }
1560
+ catch (e) {
1561
+ a.error = e;
1562
+ }
1563
+ callback(a);
1564
+ };
1565
+ };
1566
+ };
1567
+
1568
+ /**
1569
+ * Creates the 'test' object that gets passed to every test function.
1570
+ * Accepts the name of the test function as its first argument, followed by
1571
+ * the start time in ms, the options object and a callback function.
1572
+ *
1573
+ * @param {String} name
1574
+ * @param {Number} start
1575
+ * @param {Object} options
1576
+ * @param {Function} callback
1577
+ * @api public
1578
+ */
1579
+
1580
+ exports.test = function (name, start, options, callback) {
1581
+ var expecting;
1582
+ var a_list = [];
1583
+
1584
+ var wrapAssert = assertWrapper(function (a) {
1585
+ a_list.push(a);
1586
+ if (options.log) {
1587
+ async.nextTick(function () {
1588
+ options.log(a);
1589
+ });
1590
+ }
1591
+ });
1592
+
1593
+ var test = {
1594
+ done: function (err) {
1595
+ if (expecting !== undefined && expecting !== a_list.length) {
1596
+ var e = new Error(
1597
+ 'Expected ' + expecting + ' assertions, ' +
1598
+ a_list.length + ' ran'
1599
+ );
1600
+ var a1 = exports.assertion({method: 'expect', error: e});
1601
+ a_list.push(a1);
1602
+ if (options.log) {
1603
+ async.nextTick(function () {
1604
+ options.log(a1);
1605
+ });
1606
+ }
1607
+ }
1608
+ if (err) {
1609
+ var a2 = exports.assertion({error: err});
1610
+ a_list.push(a2);
1611
+ if (options.log) {
1612
+ async.nextTick(function () {
1613
+ options.log(a2);
1614
+ });
1615
+ }
1616
+ }
1617
+ var end = new Date().getTime();
1618
+ async.nextTick(function () {
1619
+ var assertion_list = exports.assertionList(a_list, end - start);
1620
+ options.testDone(name, assertion_list);
1621
+ callback(null, a_list);
1622
+ });
1623
+ },
1624
+ ok: wrapAssert('ok', 'ok', 2),
1625
+ same: wrapAssert('same', 'deepEqual', 3),
1626
+ equals: wrapAssert('equals', 'equal', 3),
1627
+ expect: function (num) {
1628
+ expecting = num;
1629
+ },
1630
+ _assertion_list: a_list
1631
+ };
1632
+ // add all functions from the assert module
1633
+ for (var k in assert) {
1634
+ if (assert.hasOwnProperty(k)) {
1635
+ test[k] = wrapAssert(k, k, assert[k].length);
1636
+ }
1637
+ }
1638
+ return test;
1639
+ };
1640
+
1641
+ /**
1642
+ * Ensures an options object has all callbacks, adding empty callback functions
1643
+ * if any are missing.
1644
+ *
1645
+ * @param {Object} opt
1646
+ * @return {Object}
1647
+ * @api public
1648
+ */
1649
+
1650
+ exports.options = function (opt) {
1651
+ var optionalCallback = function (name) {
1652
+ opt[name] = opt[name] || function () {};
1653
+ };
1654
+
1655
+ optionalCallback('moduleStart');
1656
+ optionalCallback('moduleDone');
1657
+ optionalCallback('testStart');
1658
+ optionalCallback('testDone');
1659
+ //optionalCallback('log');
1660
+
1661
+ // 'done' callback is not optional.
1662
+
1663
+ return opt;
1664
+ };
1665
+ })(types);
1666
+ (function(exports){
1667
+ /*!
1668
+ * Nodeunit
1669
+ * Copyright (c) 2010 Caolan McMahon
1670
+ * MIT Licensed
1671
+ *
1672
+ * THIS FILE SHOULD BE BROWSER-COMPATIBLE JS!
1673
+ * Only code on that line will be removed, it's mostly to avoid requiring code
1674
+ * that is node specific
1675
+ */
1676
+
1677
+ /**
1678
+ * Module dependencies
1679
+ */
1680
+
1681
+
1682
+
1683
+ /**
1684
+ * Added for browser compatibility
1685
+ */
1686
+
1687
+ var _keys = function (obj) {
1688
+ if (Object.keys) {
1689
+ return Object.keys(obj);
1690
+ }
1691
+ var keys = [];
1692
+ for (var k in obj) {
1693
+ if (obj.hasOwnProperty(k)) {
1694
+ keys.push(k);
1695
+ }
1696
+ }
1697
+ return keys;
1698
+ };
1699
+
1700
+
1701
+ var _copy = function (obj) {
1702
+ var nobj = {};
1703
+ var keys = _keys(obj);
1704
+ for (var i = 0; i < keys.length; i += 1) {
1705
+ nobj[keys[i]] = obj[keys[i]];
1706
+ }
1707
+ return nobj;
1708
+ };
1709
+
1710
+
1711
+ /**
1712
+ * Runs a test function (fn) from a loaded module. After the test function
1713
+ * calls test.done(), the callback is executed with an assertionList as its
1714
+ * second argument.
1715
+ *
1716
+ * @param {String} name
1717
+ * @param {Function} fn
1718
+ * @param {Object} opt
1719
+ * @param {Function} callback
1720
+ * @api public
1721
+ */
1722
+
1723
+ exports.runTest = function (name, fn, opt, callback) {
1724
+ var options = types.options(opt);
1725
+
1726
+ options.testStart(name);
1727
+ var start = new Date().getTime();
1728
+ var test = types.test(name, start, options, callback);
1729
+
1730
+ try {
1731
+ fn(test);
1732
+ }
1733
+ catch (e) {
1734
+ test.done(e);
1735
+ }
1736
+ };
1737
+
1738
+ /**
1739
+ * Takes an object containing test functions or other test suites as properties
1740
+ * and runs each in series. After all tests have completed, the callback is
1741
+ * called with a list of all assertions as the second argument.
1742
+ *
1743
+ * If a name is passed to this function it is prepended to all test and suite
1744
+ * names that run within it.
1745
+ *
1746
+ * @param {String} name
1747
+ * @param {Object} suite
1748
+ * @param {Object} opt
1749
+ * @param {Function} callback
1750
+ * @api public
1751
+ */
1752
+
1753
+ exports.runSuite = function (name, suite, opt, callback) {
1754
+ suite = wrapGroup(suite);
1755
+ var keys = _keys(suite);
1756
+
1757
+ async.concatSeries(keys, function (k, cb) {
1758
+ var prop = suite[k], _name;
1759
+
1760
+ _name = name ? [].concat(name, k) : [k];
1761
+ _name.toString = function () {
1762
+ // fallback for old one
1763
+ return this.join(' - ');
1764
+ };
1765
+
1766
+ if (typeof prop === 'function') {
1767
+ var in_name = false,
1768
+ in_specific_test = (_name.toString() === opt.testFullSpec) ? true : false;
1769
+ for (var i = 0; i < _name.length; i += 1) {
1770
+ if (_name[i] === opt.testspec) {
1771
+ in_name = true;
1772
+ }
1773
+ }
1774
+
1775
+ if ((!opt.testFullSpec || in_specific_test) && (!opt.testspec || in_name)) {
1776
+ if (opt.moduleStart) {
1777
+ opt.moduleStart();
1778
+ }
1779
+ exports.runTest(_name, suite[k], opt, cb);
1780
+ }
1781
+ else {
1782
+ return cb();
1783
+ }
1784
+ }
1785
+ else {
1786
+ exports.runSuite(_name, suite[k], opt, cb);
1787
+ }
1788
+ }, callback);
1789
+ };
1790
+
1791
+ /**
1792
+ * Run each exported test function or test suite from a loaded module.
1793
+ *
1794
+ * @param {String} name
1795
+ * @param {Object} mod
1796
+ * @param {Object} opt
1797
+ * @param {Function} callback
1798
+ * @api public
1799
+ */
1800
+
1801
+ exports.runModule = function (name, mod, opt, callback) {
1802
+ var options = _copy(types.options(opt));
1803
+
1804
+ var _run = false;
1805
+ var _moduleStart = options.moduleStart;
1806
+
1807
+ mod = wrapGroup(mod);
1808
+
1809
+ function run_once() {
1810
+ if (!_run) {
1811
+ _run = true;
1812
+ _moduleStart(name);
1813
+ }
1814
+ }
1815
+ options.moduleStart = run_once;
1816
+
1817
+ var start = new Date().getTime();
1818
+
1819
+ exports.runSuite(null, mod, options, function (err, a_list) {
1820
+ var end = new Date().getTime();
1821
+ var assertion_list = types.assertionList(a_list, end - start);
1822
+ options.moduleDone(name, assertion_list);
1823
+ if (nodeunit.complete) {
1824
+ nodeunit.complete(name, assertion_list);
1825
+ }
1826
+ callback(null, a_list);
1827
+ });
1828
+ };
1829
+
1830
+ /**
1831
+ * Treats an object literal as a list of modules keyed by name. Runs each
1832
+ * module and finished with calling 'done'. You can think of this as a browser
1833
+ * safe alternative to runFiles in the nodeunit module.
1834
+ *
1835
+ * @param {Object} modules
1836
+ * @param {Object} opt
1837
+ * @api public
1838
+ */
1839
+
1840
+ // TODO: add proper unit tests for this function
1841
+ exports.runModules = function (modules, opt) {
1842
+ var all_assertions = [];
1843
+ var options = types.options(opt);
1844
+ var start = new Date().getTime();
1845
+
1846
+ async.concatSeries(_keys(modules), function (k, cb) {
1847
+ exports.runModule(k, modules[k], options, cb);
1848
+ },
1849
+ function (err, all_assertions) {
1850
+ var end = new Date().getTime();
1851
+ options.done(types.assertionList(all_assertions, end - start));
1852
+ });
1853
+ };
1854
+
1855
+
1856
+ /**
1857
+ * Wraps a test function with setUp and tearDown functions.
1858
+ * Used by testCase.
1859
+ *
1860
+ * @param {Function} setUp
1861
+ * @param {Function} tearDown
1862
+ * @param {Function} fn
1863
+ * @api private
1864
+ */
1865
+
1866
+ var wrapTest = function (setUp, tearDown, fn) {
1867
+ return function (test) {
1868
+ var context = {};
1869
+ if (tearDown) {
1870
+ var done = test.done;
1871
+ test.done = function (err) {
1872
+ try {
1873
+ tearDown.call(context, function (err2) {
1874
+ if (err && err2) {
1875
+ test._assertion_list.push(
1876
+ types.assertion({error: err})
1877
+ );
1878
+ return done(err2);
1879
+ }
1880
+ done(err || err2);
1881
+ });
1882
+ }
1883
+ catch (e) {
1884
+ done(e);
1885
+ }
1886
+ };
1887
+ }
1888
+ if (setUp) {
1889
+ setUp.call(context, function (err) {
1890
+ if (err) {
1891
+ return test.done(err);
1892
+ }
1893
+ fn.call(context, test);
1894
+ });
1895
+ }
1896
+ else {
1897
+ fn.call(context, test);
1898
+ }
1899
+ };
1900
+ };
1901
+
1902
+
1903
+ /**
1904
+ * Returns a serial callback from two functions.
1905
+ *
1906
+ * @param {Function} funcFirst
1907
+ * @param {Function} funcSecond
1908
+ * @api private
1909
+ */
1910
+
1911
+ var getSerialCallback = function (fns) {
1912
+ if (!fns.length) {
1913
+ return null;
1914
+ }
1915
+ return function (callback) {
1916
+ var that = this;
1917
+ var bound_fns = [];
1918
+ for (var i = 0, len = fns.length; i < len; i++) {
1919
+ (function (j) {
1920
+ bound_fns.push(function () {
1921
+ return fns[j].apply(that, arguments);
1922
+ });
1923
+ })(i);
1924
+ }
1925
+ return async.series(bound_fns, callback);
1926
+ };
1927
+ };
1928
+
1929
+
1930
+ /**
1931
+ * Wraps a group of tests with setUp and tearDown functions.
1932
+ * Used by testCase.
1933
+ *
1934
+ * @param {Object} group
1935
+ * @param {Array} setUps - parent setUp functions
1936
+ * @param {Array} tearDowns - parent tearDown functions
1937
+ * @api private
1938
+ */
1939
+
1940
+ var wrapGroup = function (group, setUps, tearDowns) {
1941
+ var tests = {};
1942
+
1943
+ var setUps = setUps ? setUps.slice(): [];
1944
+ var tearDowns = tearDowns ? tearDowns.slice(): [];
1945
+
1946
+ if (group.setUp) {
1947
+ setUps.push(group.setUp);
1948
+ delete group.setUp;
1949
+ }
1950
+ if (group.tearDown) {
1951
+ tearDowns.unshift(group.tearDown);
1952
+ delete group.tearDown;
1953
+ }
1954
+
1955
+ var keys = _keys(group);
1956
+
1957
+ for (var i = 0; i < keys.length; i += 1) {
1958
+ var k = keys[i];
1959
+ if (typeof group[k] === 'function') {
1960
+ tests[k] = wrapTest(
1961
+ getSerialCallback(setUps),
1962
+ getSerialCallback(tearDowns),
1963
+ group[k]
1964
+ );
1965
+ }
1966
+ else if (typeof group[k] === 'object') {
1967
+ tests[k] = wrapGroup(group[k], setUps, tearDowns);
1968
+ }
1969
+ }
1970
+ return tests;
1971
+ };
1972
+
1973
+
1974
+ /**
1975
+ * Backwards compatibility for test suites using old testCase API
1976
+ */
1977
+
1978
+ exports.testCase = function (suite) {
1979
+ return suite;
1980
+ };
1981
+ })(core);
1982
+ (function(exports){
1983
+ /*!
1984
+ * Nodeunit
1985
+ * Copyright (c) 2010 Caolan McMahon
1986
+ * MIT Licensed
1987
+ *
1988
+ * THIS FILE SHOULD BE BROWSER-COMPATIBLE JS!
1989
+ * Only code on that line will be removed, its mostly to avoid requiring code
1990
+ * that is node specific
1991
+ */
1992
+
1993
+
1994
+ /**
1995
+ * NOTE: this test runner is not listed in index.js because it cannot be
1996
+ * used with the command-line tool, only inside the browser.
1997
+ */
1998
+
1999
+
2000
+ /**
2001
+ * Reporter info string
2002
+ */
2003
+
2004
+ exports.info = "Browser-based test reporter";
2005
+
2006
+
2007
+ /**
2008
+ * Run all tests within each module, reporting the results
2009
+ *
2010
+ * @param {Array} files
2011
+ * @api public
2012
+ */
2013
+
2014
+ exports.run = function (modules, options) {
2015
+ var start = new Date().getTime(), div;
2016
+ options = options || {};
2017
+ div = options.div || document.body;
2018
+
2019
+ function setText(el, txt) {
2020
+ if ('innerText' in el) {
2021
+ el.innerText = txt;
2022
+ }
2023
+ else if ('textContent' in el){
2024
+ el.textContent = txt;
2025
+ }
2026
+ }
2027
+
2028
+ function getOrCreate(tag, id) {
2029
+ var el = document.getElementById(id);
2030
+ if (!el) {
2031
+ el = document.createElement(tag);
2032
+ el.id = id;
2033
+ div.appendChild(el);
2034
+ }
2035
+ return el;
2036
+ };
2037
+
2038
+ var header = getOrCreate('h1', 'nodeunit-header');
2039
+ var banner = getOrCreate('h2', 'nodeunit-banner');
2040
+ var userAgent = getOrCreate('h2', 'nodeunit-userAgent');
2041
+ var tests = getOrCreate('ol', 'nodeunit-tests');
2042
+ var result = getOrCreate('p', 'nodeunit-testresult');
2043
+
2044
+ setText(userAgent, navigator.userAgent);
2045
+
2046
+ nodeunit.runModules(modules, {
2047
+ moduleStart: function (name) {
2048
+ /*var mheading = document.createElement('h2');
2049
+ mheading.innerText = name;
2050
+ results.appendChild(mheading);
2051
+ module = document.createElement('ol');
2052
+ results.appendChild(module);*/
2053
+ },
2054
+ testDone: function (name, assertions) {
2055
+ var test = document.createElement('li');
2056
+ var strong = document.createElement('strong');
2057
+ strong.innerHTML = name + ' <b style="color: black;">(' +
2058
+ '<b class="fail">' + assertions.failures() + '</b>, ' +
2059
+ '<b class="pass">' + assertions.passes() + '</b>, ' +
2060
+ assertions.length +
2061
+ ')</b>';
2062
+ test.className = assertions.failures() ? 'fail': 'pass';
2063
+ test.appendChild(strong);
2064
+
2065
+ var aList = document.createElement('ol');
2066
+ aList.style.display = 'none';
2067
+ test.onclick = function () {
2068
+ var d = aList.style.display;
2069
+ aList.style.display = (d == 'none') ? 'block': 'none';
2070
+ };
2071
+ for (var i=0; i<assertions.length; i++) {
2072
+ var li = document.createElement('li');
2073
+ var a = assertions[i];
2074
+ if (a.failed()) {
2075
+ li.innerHTML = (a.message || a.method || 'no message') +
2076
+ '<pre>' + (a.error.stack || a.error) + '</pre>';
2077
+ li.className = 'fail';
2078
+ }
2079
+ else {
2080
+ li.innerHTML = a.message || a.method || 'no message';
2081
+ li.className = 'pass';
2082
+ }
2083
+ aList.appendChild(li);
2084
+ }
2085
+ test.appendChild(aList);
2086
+ tests.appendChild(test);
2087
+ },
2088
+ done: function (assertions) {
2089
+ var end = new Date().getTime();
2090
+ var duration = end - start;
2091
+
2092
+ var failures = assertions.failures();
2093
+ banner.className = failures ? 'fail': 'pass';
2094
+
2095
+ result.innerHTML = 'Tests completed in ' + duration +
2096
+ ' milliseconds.<br/><span class="passed">' +
2097
+ assertions.passes() + '</span> assertions of ' +
2098
+ '<span class="all">' + assertions.length + '<span> passed, ' +
2099
+ assertions.failures() + ' failed.';
2100
+ }
2101
+ });
2102
+ };
2103
+ })(reporter);
2104
+ nodeunit = core;
2105
+ nodeunit.assert = assert;
2106
+ nodeunit.reporter = reporter;
2107
+ nodeunit.run = reporter.run;
2108
+ return nodeunit; })();