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
208 lines (198 loc) • 6.43 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core')) :
typeof define === 'function' && define.amd ? define(['exports', '@angular/core'], factory) :
(factory((global.ng = global.ng || {}, global.ng.angular2Pretyjson = {}),global.ng.core));
}(this, (function (exports,core) { 'use strict';
/**
* @license angular2-prettyjson
* MIT license
*/
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
var PrettyJsonComponent = (function () {
function PrettyJsonComponent() {
}
PrettyJsonComponent.decorators = [
{ type: core.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: core.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, "&").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, 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: core.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: core.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: core.NgModule, args: [{
declarations: [
PrettyJsonComponent,
PrettyJsonPipe,
SafeJsonPipe
],
exports: [
PrettyJsonComponent,
PrettyJsonPipe,
SafeJsonPipe
]
},] },
];
/** @nocollapse */
PrettyJsonModule.ctorParameters = function () { return []; };
return PrettyJsonModule;
}());
exports.PrettyJsonModule = PrettyJsonModule;
exports.ɵc = SafeJsonPipe;
exports.ɵa = PrettyJsonComponent;
exports.ɵb = PrettyJsonPipe;
Object.defineProperty(exports, '__esModule', { value: true });
})));
//# sourceMappingURL=angular2-prettyjson.umd.js.map