var BNPChrome = angular .module("BNPChrome", []) .factory("parse", function () { const parser = function (input, level, depthOverride) { const depth = depthOverride || 3; if (level > depth) return input; if (!input || typeof input === "number" || typeof input === "boolean") { return input; } if (Array.isArray(input)) { // loop and parse each node for (var i = 0; i < input.length; i++) { input[i] = parser(input[i], level ? level + 1 : 1, depth); } return input; } if (typeof input === "string") { try { input = parser(JSON.parse(input), level ? level + 1 : 1, depth); return input; } catch (e) { // not a stringified node return input; } } else if (typeof input === "object") { Object.keys(input).forEach(function (item) { input[item] = parser(input[item], level ? level + 1 : 1, depth); return item; }); } else { // unless there is a datatype I'm not checking for.... // console.log('shouldnt get here') } return input; }; return parser; }) .directive("prettyPrint", function (parse) { return { restrict: "E", replace: true, transclude: false, scope: { data: "=data" }, link: function (scope, element, attrs) { let data = scope.data; let $el = $("
"); if (data === true) { data = "true"; } else if (data === false) { data = "false"; } else if (data === undefined) { data = "undefined"; } else if (data === null) { data = "null"; } else if (typeof data === "number") { // skip (i.e. do default) } else if (typeof data === "string" && (data[0] === "{" || data[0] === "[")) { $el = $(""); data = JSON.stringify(parse(data, 0), null, 4); // try { // data = JSON.stringify(JSON.parse(data), null, 4); // // $el.text() // } catch (e) { // console.debug("Error parsing", data); // } } else if (typeof data === "string") { // i.e. a string but not a JSON stringified string data = $("