GithubHelp home page GithubHelp logo

sguiheux / angular2-prettyjson Goto Github PK

View Code? Open in Web Editor NEW

This project forked from matiboy/angular2-prettyjson

0.0 2.0 0.0 78 KB

Angular 2 debug output of objects. Contains a pipe similar to JsonPipe but adds support for spacing and handling of circular structures

TypeScript 100.00%

angular2-prettyjson's Introduction

Angular 2 Pretty Json v2.0.5

A module for Angular 2 debug output of objects. Contains a pipe similar to JsonPipe but adds support for spacing and handling of circular structures.
Also contains a component that outputs any object with syntax highlight.
Warning: just as the JsonPipe, this is an impure pipe and should be used only for debugging purposes.

Install

npm install angular2-prettyjson

ES2015 / UMD

Two versions are available: ES2015 modules and UMD. If you are using a project based on the AngularCLI, everything should work from a simple npm install.
If you are using the Angular Quickstart template (or other SystemJS based compilation), please point to the index.umd.js file e.g. systemjs.config.js:

    map: {
      ...
      // other libraries
      'rxjs':                      'npm:rxjs',
      'angular2-prettyjson': 'npm:angular2-prettyjson'
    },
    packages: {
      ...,
      'angular2-prettyjson': {
        defaultExtension: 'js',
        main: './index.umd.js'
      }

Usage

Import PrettyJsonModule to have access to following component and pipes

import {PrettyJsonModule} from 'angular2-prettyjson';

@NgModule({
    declarations: [
        AppComponent,
    ],
    imports: [
        PrettyJsonModule,
    ],
    providers: [
    ],
    bootstrap: [AppComponent]
})
export class AppModule {
}

Safe Pipe

The SafeJsonPipe aims to override the JsonPipe and so uses the same name "json". It also accepts an optional argument spaces=2 for the JSON stringify spacing.

@Component({
  ....
  template: `
    <pre>
    {{ circularObj | json }}
    {{ circularObj | json:4 }}
    </pre>
  ` // make sure to use a surrounding element with white-space: pre; for best results
  })
  ...

outputs

2 spaces (default):

2 spaces

4 spaces:

4 spaces

Overriding JsonPipe throughout the app

If you want the Safe Pipe to be used throughout the app:

import {PrettyJsonModule, SafeJsonPipe} from 'angular2-prettyjson';
import {JsonPipe} from '@angular/common';

@NgModule({
    declarations: [
        AppComponent,
    ],
    imports: [
        PrettyJsonModule,
    ],
    providers: [
            { provide: JsonPipe, useClass: SafeJsonPipe }
    ],
    bootstrap: [AppComponent]
})
export class AppModule {
}

Pretty (and safe) Pipe

The PrettyJsonPipe stringifies the object and then adds spans around properties, null, arrays etc. You can bind it to the innerHtml of other elements.

@Component({
  ....
  template: `
    <pre [innerHtml]="circularObj | prettyjson:3"></pre>
  `
  })
  ...

A good set of styles to use is

   pre span {white-space: normal;}
   .string { color: green; }
   .number { color: darkorange; }
   .boolean { color: blue; }
   .null { color: magenta; }
   .key { color: red; }

If you wish to use the styles property of the parent component, please prefix each class selector with :host /deep/ e.g.

@Component({
 ....
 template: `
   <pre [innerHtml]="circularObj | prettyjson:3"></pre>
 `,
 styles: [`:host /deep/ .string {color:green} ...`]
 })
 ...

See output under component below.

Component

Creates a pre element into which the Pretty Json pipe'd object is dumped as HTML. Takes care of styling.

Takes an input [obj] that can be data bound to any object.

import {PrettyJsonComponent} from 'angular2-prettyjson';

@Component({
  ....
  entryComponents: [PrettyJsonComponent], // Add to entry components
  template: `
    <prettyjson [obj]="theForm.value"></prettyjson>
  `
  })
  export class MyComponent {
    ngOnInit() {
      this.theForm = this.formBuilder.group({
       ...

outputs

Pretty json with syntax highlight

Changelog

  1. Dependencies to include Angular 4 as well as 2
  2. Removed dependency on RxJS

angular2-prettyjson's People

Contributors

matiboy avatar sguiheux avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.