angular2-prettyjson
Version:
Angular2 json utils. Includes a pipe to replace Angular's built in json pipe which implements spacing, avoids circular references. Also includes a component that will pretty print json with syntax highlight
205 lines (194 loc) • 5.48 kB
JavaScript
/**
* @license angular2-prettyjson
* MIT license
*/
import { Component, Input, NgModule, Pipe } from '@angular/core';
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
class PrettyJsonComponent {
}
PrettyJsonComponent.decorators = [
{ type: Component, args: [{
selector: "prettyjson",
styles: [
`pre {outline: 1px solid #ccc; padding: 5px; margin: 5px; }
:host >>> span {white-space: normal;}
:host >>> .string { color: green; }
:host >>> .number { color: darkorange; }
:host >>> .boolean { color: blue; }
:host >>> .null { color: magenta; }
:host >>> .key { color: red; }`
],
template: `
<pre [innerHtml]="obj | prettyjson">
</pre>
`,
},] },
];
/** @nocollapse */
PrettyJsonComponent.ctorParameters = () => [];
PrettyJsonComponent.propDecorators = {
"obj": [{ type: Input },],
};
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
/**
* @return {?}
*/
function serializer() {
const /** @type {?} */ stack = [];
const /** @type {?} */ keys = [];
const /** @type {?} */ cycleReplacer = function (key, value) {
if (stack[0] === value) {
return "[Circular ~]";
}
return "[Circular ~." + keys.slice(0, stack.indexOf(value)).join(".") + "]";
};
return function (key, value) {
if (stack.length > 0) {
const /** @type {?} */ thisPos = stack.indexOf(this);
~thisPos ? stack.splice(thisPos + 1) : stack.push(this);
~thisPos ? keys.splice(thisPos, Infinity, key) : keys.push(key);
if (~stack.indexOf(value)) {
value = cycleReplacer.call(this, key, value);
}
}
else {
stack.push(value);
}
return value;
};
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
class PrettyJsonPipe {
/**
* @param {?} obj
* @param {?=} spaces
* @return {?}
*/
transform(obj, spaces = 2) {
return this._syntaxHighlight(obj, serializer(), spaces);
}
/**
* @param {?} json
* @param {?} serializer
* @param {?} spacing
* @return {?}
*/
_syntaxHighlight(json, serializer$$1, spacing) {
if (json === undefined) {
return '<span class="undefined"></span>';
}
// Credits to the accepted answer here
// http://stackoverflow.com/questions/4810841/how-can-i-pretty-print-json-using-javascript
if (typeof json !== "string") {
json = JSON.stringify(json, serializer$$1, spacing);
}
json = json.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, (match) => {
let /** @type {?} */ cls = "number";
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = "key";
}
else {
cls = "string";
}
}
else if (/true|false/.test(match)) {
cls = "boolean";
}
else if (/null/.test(match)) {
cls = "null";
}
return `<span class="${cls}">${match}</span>`;
});
}
}
PrettyJsonPipe.decorators = [
{ type: Pipe, args: [{
name: "prettyjson",
pure: false
},] },
];
/** @nocollapse */
PrettyJsonPipe.ctorParameters = () => [];
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
class SafeJsonPipe {
/**
* @param {?} obj
* @param {?=} spaces
* @return {?}
*/
transform(obj, spaces = 2) {
return JSON.stringify(obj, serializer(), spaces);
}
}
SafeJsonPipe.decorators = [
{ type: Pipe, args: [{
name: "json",
pure: false
},] },
];
/** @nocollapse */
SafeJsonPipe.ctorParameters = () => [];
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
class PrettyJsonModule {
}
PrettyJsonModule.decorators = [
{ type: NgModule, args: [{
declarations: [
PrettyJsonComponent,
PrettyJsonPipe,
SafeJsonPipe
],
exports: [
PrettyJsonComponent,
PrettyJsonPipe,
SafeJsonPipe
]
},] },
];
/** @nocollapse */
PrettyJsonModule.ctorParameters = () => [];
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
// Public classes.
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
/**
* Angular library starter
* Build an Angular library compatible with AoT compilation & Tree shaking
* Copyright Roberto Simonetti
* MIT license
* https://github.com/robisim74/angular-library-starter
*/
/**
* Entry point for all public APIs of the package.
*/
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
/**
* Generated bundle index. Do not edit.
*/
export { PrettyJsonModule, SafeJsonPipe as ɵc, PrettyJsonComponent as ɵa, PrettyJsonPipe as ɵb };
//# sourceMappingURL=angular2-prettyjson.js.map