Skip to content

Commit

Permalink
feat: add inspect global method
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Feb 28, 2020
1 parent 0ca5991 commit 1d6aa75
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 19 deletions.
5 changes: 2 additions & 3 deletions examples/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// @ts-check

const { Edge } = require('..')
const edge = require('..').default
const { join } = require('path')
const http = require('http')

const edge = new Edge()

http.createServer((req, res) => {
edge.mount(join(__dirname, './views'))
res.writeHead(200, { 'content-type': 'text/html' })
res.end(edge.render('user', { title: 'Hello' }))
}).listen(3000)
24 changes: 17 additions & 7 deletions examples/views/user.edge
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
@debugger
@component('alert', title = 'Hey')
@slot('title', props)
@include('partial')
@endslot
@endcomponent
@!component('alert', title = 'Wow')
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
<style>
</style>
</head>
<body>
@set('user', { username: 'virk' })

{{ inspect($state, 1) }}
</body>
</html>
3 changes: 3 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import { Edge } from './src/Edge'
import globals from './src/Edge/globals'

export {
LoaderContract,
Expand All @@ -29,4 +30,6 @@ export {
export { Edge }

const edge = new Edge()
globals(edge)

export default edge
9 changes: 3 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,12 @@
},
"dependencies": {
"@poppinss/utils": "^2.1.2",
"debug": "^4.1.1",
"edge-error": "^1.0.4",
"edge-parser": "^2.1.1",
"he": "^1.2.0",
"import-fresh": "^3.2.1",
"lodash": "^4.17.15",
"macroable": "^4.0.2",
"node-exceptions": "^4.0.1"
"macroable": "^4.0.2"
},
"gitHooks": {
"commit-msg": "node ./node_modules/@adonisjs/mrm-preset/validateCommit/conventional/validate.js"
Expand Down
30 changes: 30 additions & 0 deletions src/Edge/globals/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @module edge
*/

/*
* edge
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import { inspect as utilInspect } from 'util'
import { EdgeContract, ContextContract } from '../../Contracts'

/**
* Inspect value.
*/
function inspect (ctx: ContextContract, valueToInspect: any, depth: number = 1) {
return ctx.safe(`<pre style="background: #000; color: #fff; padding: 20px;">${utilInspect(valueToInspect, {
showHidden: true,
compact: false,
depth: depth,
})}</pre>`)
}

export default function globals (edge: EdgeContract) {
edge.global('inspect', inspect)
}

0 comments on commit 1d6aa75

Please sign in to comment.