UNPKG

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

226 lines (215 loc) 6.57 kB
/** * @license angular2-prettyjson * MIT license */ import { Component, Input, NgModule, Pipe } from '@angular/core'; /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ var PrettyJsonComponent = (function () { function PrettyJsonComponent() { } PrettyJsonComponent.decorators = [ { type: Component, args: [{ selector: "prettyjson", styles: [ "pre {outline: 1px solid #ccc; padding: 5px; margin: 5px; }\n :host >>> span {white-space: normal;}\n :host >>> .string { color: green; }\n :host >>> .number { color: darkorange; }\n :host >>> .boolean { color: blue; }\n :host >>> .null { color: magenta; }\n :host >>> .key { color: red; }" ], template: "\n <pre [innerHtml]=\"obj | prettyjson\">\n </pre>\n ", },] }, ]; /** @nocollapse */ PrettyJsonComponent.ctorParameters = function () { return []; }; PrettyJsonComponent.propDecorators = { "obj": [{ type: Input },], }; return PrettyJsonComponent; }()); /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ /** * @return {?} */ function serializer() { var /** @type {?} */ stack = []; var /** @type {?} */ keys = []; var /** @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) { var /** @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 */ var PrettyJsonPipe = (function () { function PrettyJsonPipe() { } /** * @param {?} obj * @param {?=} spaces * @return {?} */ PrettyJsonPipe.prototype.transform = /** * @param {?} obj * @param {?=} spaces * @return {?} */ function (obj, spaces) { if (spaces === void 0) { spaces = 2; } return this._syntaxHighlight(obj, serializer(), spaces); }; /** * @param {?} json * @param {?} serializer * @param {?} spacing * @return {?} */ PrettyJsonPipe.prototype._syntaxHighlight = /** * @param {?} json * @param {?} serializer * @param {?} spacing * @return {?} */ function (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, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;"); return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) { var /** @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 = function () { return []; }; return PrettyJsonPipe; }()); /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ var SafeJsonPipe = (function () { function SafeJsonPipe() { } /** * @param {?} obj * @param {?=} spaces * @return {?} */ SafeJsonPipe.prototype.transform = /** * @param {?} obj * @param {?=} spaces * @return {?} */ function (obj, spaces) { if (spaces === void 0) { spaces = 2; } return JSON.stringify(obj, serializer(), spaces); }; SafeJsonPipe.decorators = [ { type: Pipe, args: [{ name: "json", pure: false },] }, ]; /** @nocollapse */ SafeJsonPipe.ctorParameters = function () { return []; }; return SafeJsonPipe; }()); /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ var PrettyJsonModule = (function () { function PrettyJsonModule() { } PrettyJsonModule.decorators = [ { type: NgModule, args: [{ declarations: [ PrettyJsonComponent, PrettyJsonPipe, SafeJsonPipe ], exports: [ PrettyJsonComponent, PrettyJsonPipe, SafeJsonPipe ] },] }, ]; /** @nocollapse */ PrettyJsonModule.ctorParameters = function () { return []; }; return PrettyJsonModule; }()); /** * @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