diff --git a/example/index.ts b/example/index.ts index 39e9d57..1c710af 100644 --- a/example/index.ts +++ b/example/index.ts @@ -14,6 +14,9 @@ class User extends Base { username: 'virk', email: 'virk@adonisjs.com', isAdmin: true, + profile: { + avatarUrl: 'foo', + }, lastLoginAt: null, } @@ -21,10 +24,15 @@ class User extends Base { public get username () { return this.attributes.username } + + public toJSON () { + return {} + } } const user = new User() user.parent = user +console.log(user) createServer((_req, res) => { res.writeHead(200, { 'content-type': 'text/html' }) diff --git a/package-lock.json b/package-lock.json index 67fb978..b82da95 100644 --- a/package-lock.json +++ b/package-lock.json @@ -385,6 +385,11 @@ "fs-extra": "^9.0.0" } }, + "@poppinss/inspect": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@poppinss/inspect/-/inspect-1.0.1.tgz", + "integrity": "sha512-kLeEaBSGhlleyYvKc7c9s3uE6xv7cwyulE0EgHf4jU/CL96h0yC4mkdw1wvC1l1PYYQozCGy46FwMBAAMOobCA==" + }, "@samverschueren/stream-to-observable": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/@samverschueren/stream-to-observable/-/stream-to-observable-0.3.0.tgz", diff --git a/package.json b/package.json index 8b96c0f..fd884a3 100644 --- a/package.json +++ b/package.json @@ -64,6 +64,7 @@ ] }, "dependencies": { + "@poppinss/inspect": "^1.0.1", "edge-error": "^1.0.4", "edge-lexer": "^3.0.3", "edge-parser": "^5.0.6", diff --git a/src/Edge/globals/PrettyPrint.ts b/src/Edge/globals/PrettyPrint.ts deleted file mode 100644 index df17e3b..0000000 --- a/src/Edge/globals/PrettyPrint.ts +++ /dev/null @@ -1,134 +0,0 @@ -/* - * edge - * - * (c) Harminder Virk - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. -*/ - -/** - * Pretty prints an Object with colors and formatting. Code is copied from - * https://www.npmjs.com/package/pretty-print-json package and then tweaked - * to fit our use cases. - */ - -const inspectSymbol = Symbol.for('edge.inspect') - -export class PrettyPrint { - private styles = { - string: 'color: rgb(173, 219, 103);', - key: 'color: rgb(127, 219, 202);', - boolean: 'color: rgb(247, 140, 108);', - number: 'color: rgb(199, 146, 234);', - null: 'color: rgb(255, 203, 139);', - function: 'color: rgb(255, 203, 139);', - pre: ` - padding: 30px 25px; - background-color: rgb(6, 21, 38); - color: rgb(214, 222, 235); - border-radius: 6px; - font-family: JetBrains Mono, Menlo, Monaco, monospace; - font-size: 14px; - overflow: auto; - white-space: pre; - word-spacing: normal; - word-break: normal; - word-wrap: normal; - tab-size: 4; - line-height: 1.4; - text-align: left; - `, - } - - /** - * Return a boolean telling if the variable name is a - * standard global - */ - private isStandardGlobal (name) { - return ['inspect', 'truncate', 'excerpt', 'safe'].includes(name) - } - - /** - * Encode html - */ - private encodeHTML (value: string) { - return value - .replace(/&/g, '&') - .replace(/\\"/g, '\"') - .replace(//g, '>') - } - - /** - * Build HTML value of the key - */ - private buildValueHtml (value: string) { - let type = 'number' - - if (value.startsWith('"[Function')) { - type = 'function' - } else if (/^"/.test(value)) { - type = 'string' - } else if (['true', 'false'].includes(value)) { - type = 'boolean' - } else if (value === 'null') { - type = 'null' - } - return `${value}` - } - - /** - * Build individual lines inside JSON - */ - private replacer (_: string, p1: string, p2: string, p3: string, p4: string) { - const part = { indent: p1, key: p2, value: p3, end: p4 } - const findNameRegex = /(.*)(): / - const indentHtml = part.indent || '' - const keyName = part.key && part.key.replace(findNameRegex, '$1$2') - const keyHtml = part.key ? `${keyName}: ` : '' - const valueHtml = part.value ? this.buildValueHtml(part.value) : '' - const endHtml = part.end || '' - return indentHtml + keyHtml + valueHtml + endHtml - } - - /** - * JSON replacer that also handles circular references - */ - private getJSONReplacer () { - const seen = new WeakSet() - - return (key, value) => { - if (this.isStandardGlobal(key)) { - return undefined - } - - if (typeof value === 'object' && value !== null) { - if (seen.has(value)) { - return '[Circular]' - } - seen.add(value) - } - - if (typeof (value) === 'function') { - return `[Function (${value.name || 'anonymous'})]` - } - - return value - } - } - - /** - * Pretty print by converting the value to JSON string first - */ - public print (value: any) { - const serialized = typeof (value[inspectSymbol]) === 'function' - ? value[inspectSymbol]() - : value - - const json = JSON.stringify(serialized, this.getJSONReplacer(), 2) - - const jsonLineRegex = /^( *)("[^"]+": )?("[^"]*"|[\w.+-]*)?([{}[\],]*)?$/mg - return `
${this.encodeHTML(json).replace(jsonLineRegex, this.replacer.bind(this))}
` - } -} diff --git a/src/Edge/globals/index.ts b/src/Edge/globals/index.ts index 1f60368..0a20290 100644 --- a/src/Edge/globals/index.ts +++ b/src/Edge/globals/index.ts @@ -8,12 +8,12 @@ */ import truncatise from 'truncatise' -import { PrettyPrint } from './PrettyPrint' +import inspect from '@poppinss/inspect' import { safeValue } from '../../Context' export const GLOBALS = { inspect: (value) => { - return safeValue(new PrettyPrint().print(value)) + return safeValue(inspect.string.html(value)) }, truncate: (value: string, length: number = 20, options?: { strict: boolean, suffix: string }) => { return truncatise(value, {