source: trip-planner-front/node_modules/karma/common/stringify.js@ eed0bf8

Last change on this file since eed0bf8 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.5 KB
RevLine 
[6a3a178]1var serialize = null
2try {
3 serialize = require('dom-serialize')
4} catch (e) {
5 // Ignore failure on IE8
6}
7
8var instanceOf = require('./util').instanceOf
9
10function isNode (obj) {
11 return (obj.tagName || obj.nodeName) && obj.nodeType
12}
13
14function stringify (obj, depth) {
15 if (depth === 0) {
16 return '...'
17 }
18
19 if (obj === null) {
20 return 'null'
21 }
22
23 switch (typeof obj) {
24 case 'symbol':
25 return obj.toString()
26 case 'string':
27 return "'" + obj + "'"
28 case 'undefined':
29 return 'undefined'
30 case 'function':
31 try {
32 // function abc(a, b, c) { /* code goes here */ }
33 // -> function abc(a, b, c) { ... }
34 return obj.toString().replace(/\{[\s\S]*\}/, '{ ... }')
35 } catch (err) {
36 if (err instanceof TypeError) {
37 // Support older browsers
38 return 'function ' + (obj.name || '') + '() { ... }'
39 } else {
40 throw err
41 }
42 }
43 case 'boolean':
44 return obj ? 'true' : 'false'
45 case 'object':
46 var strs = []
47 if (instanceOf(obj, 'Array')) {
48 strs.push('[')
49 for (var i = 0, ii = obj.length; i < ii; i++) {
50 if (i) {
51 strs.push(', ')
52 }
53 strs.push(stringify(obj[i], depth - 1))
54 }
55 strs.push(']')
56 } else if (instanceOf(obj, 'Date')) {
57 return obj.toString()
58 } else if (instanceOf(obj, 'Text')) {
59 return obj.nodeValue
60 } else if (instanceOf(obj, 'Comment')) {
61 return '<!--' + obj.nodeValue + '-->'
62 } else if (obj.outerHTML) {
63 return obj.outerHTML
64 } else if (isNode(obj)) {
65 if (serialize) {
66 return serialize(obj)
67 } else {
68 return 'Skipping stringify, no support for dom-serialize'
69 }
70 } else if (instanceOf(obj, 'Error')) {
71 return obj.toString() + '\n' + obj.stack
72 } else {
73 var constructor = 'Object'
74 if (obj.constructor && typeof obj.constructor === 'function') {
75 constructor = obj.constructor.name
76 }
77
78 strs.push(constructor)
79 strs.push('{')
80 var first = true
81 for (var key in obj) {
82 if (Object.prototype.hasOwnProperty.call(obj, key)) {
83 if (first) {
84 first = false
85 } else {
86 strs.push(', ')
87 }
88
89 strs.push(key + ': ' + stringify(obj[key], depth - 1))
90 }
91 }
92 strs.push('}')
93 }
94 return strs.join('')
95 default:
96 return obj
97 }
98}
99
100module.exports = stringify
Note: See TracBrowser for help on using the repository browser.