Skip to content

Commit d3dd6b4

Browse files
committed
release: v1.0.0
1 parent f6a2ae8 commit d3dd6b4

File tree

2 files changed

+88
-13
lines changed

2 files changed

+88
-13
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "chobitsu",
3-
"version": "0.4.0",
3+
"version": "1.0.0",
44
"description": "Chrome devtools protocol JavaScript implementation",
55
"main": "dist/chobitsu.js",
66
"files": [

src/lib/stringifyObj.ts

Lines changed: 87 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
const objects = new Map()
2222
const objectIds = new Map()
2323
const selfs = new Map()
24+
const entries = new Map()
2425
let id = 1
2526

2627
function getOrCreateObjId(obj: any, self: any) {
@@ -180,7 +181,7 @@ export function getProperties(params: any) {
180181

181182
properties.push(property)
182183
}
183-
if (proto) {
184+
if (proto && !ownProperties && !noPrototype(obj)) {
184185
properties.push({
185186
name: '__proto__',
186187
configurable: true,
@@ -193,7 +194,31 @@ export function getProperties(params: any) {
193194
})
194195
}
195196

197+
if (accessorPropertiesOnly) {
198+
return {
199+
result: properties,
200+
}
201+
}
202+
203+
const internalProperties = []
204+
if (proto && !noPrototype(obj)) {
205+
internalProperties.push({
206+
name: '[[Prototype]]',
207+
value: wrap(proto, {
208+
self,
209+
}),
210+
})
211+
}
212+
if (isMap(obj) || isSet(obj)) {
213+
const internalEntries = createInternalEntries(obj)
214+
internalProperties.push({
215+
name: '[[Entries]]',
216+
value: wrap(internalEntries),
217+
})
218+
}
219+
196220
return {
221+
internalProperties,
197222
result: properties,
198223
}
199224
}
@@ -312,39 +337,89 @@ function getDescription(obj: any, self: any = obj) {
312337
description = toStr(obj)
313338
} else if (subtype === 'error') {
314339
description = obj.stack
340+
} else if (subtype === 'internal#entry') {
341+
if (obj.name) {
342+
description = `{"${toStr(obj.name)}" => "${toStr(obj.value)}"}`
343+
} else {
344+
description = `"${toStr(obj.value)}"`
345+
}
315346
} else {
316347
description = getType(obj, false)
317348
}
318349

319350
return description
320351
}
321352

322-
function basic(value: any) {
353+
function basic(value: any): any {
323354
const type = typeof value
324-
const ret: any = { type }
355+
let subtype = 'object'
325356

326-
if (isNull(value)) {
327-
ret.subtype = 'null'
357+
if (value instanceof InternalEntry) {
358+
subtype = 'internal#entry'
359+
} else if (isNull(value)) {
360+
subtype = 'null'
328361
} else if (isArr(value)) {
329-
ret.subtype = 'array'
362+
subtype = 'array'
330363
} else if (isRegExp(value)) {
331-
ret.subtype = 'regexp'
364+
subtype = 'regexp'
332365
} else if (isErr(value)) {
333-
ret.subtype = 'error'
366+
subtype = 'error'
334367
} else if (isMap(value)) {
335-
ret.subtype = 'map'
368+
subtype = 'map'
336369
} else if (isSet(value)) {
337-
ret.subtype = 'set'
370+
subtype = 'set'
338371
} else {
339372
try {
340373
// Accessing nodeType may throw exception
341374
if (isEl(value)) {
342-
ret.subtype = 'node'
375+
subtype = 'node'
343376
}
344377
} catch (e) {
345378
/* tslint:disable-next-line */
346379
}
347380
}
348381

349-
return ret
382+
return {
383+
type,
384+
subtype,
385+
}
386+
}
387+
388+
class InternalEntry {
389+
name: any
390+
value: any
391+
constructor(value: any, name?: any) {
392+
if (name) {
393+
this.name = name
394+
}
395+
this.value = value
396+
}
397+
}
398+
399+
function noPrototype(obj: any) {
400+
if (obj instanceof InternalEntry) {
401+
return true
402+
}
403+
404+
if (obj[0] && obj[0] instanceof InternalEntry) {
405+
return true
406+
}
407+
408+
return false
409+
}
410+
411+
function createInternalEntries(obj: any) {
412+
const entryId = entries.get(obj)
413+
const internalEntries: InternalEntry[] = entryId ? getObj(entryId) : []
414+
const objEntries = obj.entries()
415+
let entry = objEntries.next().value
416+
while (entry) {
417+
if (isMap(obj)) {
418+
internalEntries.push(new InternalEntry(entry[1], entry[0]))
419+
} else {
420+
internalEntries.push(new InternalEntry(entry[1]))
421+
}
422+
entry = objEntries.next().value
423+
}
424+
return internalEntries
350425
}

0 commit comments

Comments
 (0)