From e9d99ba6ac59e07c7ec0b19c9de116454d957961 Mon Sep 17 00:00:00 2001 From: Mohsen Azimi Date: Tue, 16 Dec 2014 13:34:16 -0800 Subject: [PATCH] Add a function for getting type that covers 'null' case --- dist/json-formatter.css | 2 +- dist/json-formatter.js | 21 ++++++++------------- dist/json-formatter.min.css | 2 +- dist/json-formatter.min.js | 4 ++-- src/json-formatter.js | 19 +++++++------------ 5 files changed, 19 insertions(+), 29 deletions(-) diff --git a/dist/json-formatter.css b/dist/json-formatter.css index 630beb3..abf1432 100644 --- a/dist/json-formatter.css +++ b/dist/json-formatter.css @@ -1,7 +1,7 @@ /*! * jsonformatter * - * Version: 0.2.5 - 2014-12-16T18:51:42.214Z + * Version: 0.2.6 - 2014-12-16T21:33:23.610Z * License: MIT */ diff --git a/dist/json-formatter.js b/dist/json-formatter.js index 99d3e87..0095df3 100644 --- a/dist/json-formatter.js +++ b/dist/json-formatter.js @@ -1,7 +1,7 @@ /*! * jsonformatter * - * Version: 0.2.5 - 2014-12-16T18:51:45.410Z + * Version: 0.2.6 - 2014-12-16T21:33:46.343Z * License: MIT */ @@ -31,6 +31,11 @@ angular.module('jsonFormatter', ['RecursionHelper']) } } + function getType(object) { + if (object === null) { return 'null'; } + return typeof object; + } + function link(scope, element, attributes) { scope.isArray = function () { return Array.isArray(scope.json); @@ -45,22 +50,12 @@ angular.module('jsonFormatter', ['RecursionHelper']) return Object.keys(scope.json); } }; - scope.type = typeof scope.json; + scope.type = getType(scope.json); scope.hasKey = typeof scope.key !== 'undefined'; scope.getConstructorName = function(){ return getObjectName(scope.json); }; - // Set custom type for null - if (scope.json === null){ - scope.type = 'null'; - } - - // Set custom type for null - if (scope.json === undefined){ - scope.type = 'undefined'; - } - if (scope.type === 'string'){ // Add custom type for date @@ -99,7 +94,7 @@ angular.module('jsonFormatter', ['RecursionHelper']) }; scope.parseValue = function (value){ - scope.type = typeof scope.json; + scope.type = getType(scope.json); if (scope.type === 'null') { return 'null'; } diff --git a/dist/json-formatter.min.css b/dist/json-formatter.min.css index 351d102..0475137 100644 --- a/dist/json-formatter.min.css +++ b/dist/json-formatter.min.css @@ -1,6 +1,6 @@ /*! * jsonformatter * - * Version: 0.2.5 - 2014-12-16T18:51:42.214Z + * Version: 0.2.6 - 2014-12-16T21:33:23.610Z * License: MIT */.json-formatter-row{font-family:monospace}.json-formatter-row,.json-formatter-row a,.json-formatter-row a:hover{color:#000;text-decoration:none}.json-formatter-row .json-formatter-row{margin-left:1em}.json-formatter-row .children.empty{opacity:.5;margin-left:1em}.json-formatter-row .children.empty.object:after{content:"No properties"}.json-formatter-row .children.empty.array:after{content:"[]"}.json-formatter-row .string{color:green;white-space:pre;word-wrap:break-word}.json-formatter-row .number{color:#00f}.json-formatter-row .boolean{color:red}.json-formatter-row .null{color:#855a00}.json-formatter-row .undefined{color:#ca0b69}.json-formatter-row .function{color:#ff20ed}.json-formatter-row .date{background-color:rgba(0,0,0,.05)}.json-formatter-row .url{text-decoration:underline;color:#00f;cursor:pointer}.json-formatter-row .bracket{color:#00f}.json-formatter-row .key{color:#00008b;cursor:pointer}.json-formatter-row .constructor-name{cursor:pointer}.json-formatter-row .toggler{font-size:.8em;line-height:1.2em;vertical-align:middle;opacity:.6;cursor:pointer}.json-formatter-row .toggler:after{display:inline-block;transition:transform 100ms ease-in;content:"►"}.json-formatter-row .toggler.open:after{transform:rotate(90deg)}.json-formatter-dark.json-formatter-row{font-family:monospace}.json-formatter-dark.json-formatter-row,.json-formatter-dark.json-formatter-row a,.json-formatter-dark.json-formatter-row a:hover{color:#fff;text-decoration:none}.json-formatter-dark.json-formatter-row .json-formatter-row{margin-left:1em}.json-formatter-dark.json-formatter-row .children.empty{opacity:.5;margin-left:1em}.json-formatter-dark.json-formatter-row .children.empty.object:after{content:"No properties"}.json-formatter-dark.json-formatter-row .children.empty.array:after{content:"[]"}.json-formatter-dark.json-formatter-row .string{color:#31f031;white-space:pre;word-wrap:break-word}.json-formatter-dark.json-formatter-row .number{color:#66c2ff}.json-formatter-dark.json-formatter-row .boolean{color:#ec4242}.json-formatter-dark.json-formatter-row .null{color:#eec97d}.json-formatter-dark.json-formatter-row .undefined{color:#ef8fbe}.json-formatter-dark.json-formatter-row .function{color:#fd48cb}.json-formatter-dark.json-formatter-row .date{background-color:rgba(255,255,255,.05)}.json-formatter-dark.json-formatter-row .url{text-decoration:underline;color:#027bff;cursor:pointer}.json-formatter-dark.json-formatter-row .bracket{color:#9494ff}.json-formatter-dark.json-formatter-row .key{color:#23a0db;cursor:pointer}.json-formatter-dark.json-formatter-row .constructor-name{cursor:pointer}.json-formatter-dark.json-formatter-row .toggler{font-size:.8em;line-height:1.2em;vertical-align:middle;opacity:.6;cursor:pointer}.json-formatter-dark.json-formatter-row .toggler:after{display:inline-block;transition:transform 100ms ease-in;content:"►"}.json-formatter-dark.json-formatter-row .toggler.open:after{transform:rotate(90deg)} \ No newline at end of file diff --git a/dist/json-formatter.min.js b/dist/json-formatter.min.js index d0406cd..c206a03 100644 --- a/dist/json-formatter.min.js +++ b/dist/json-formatter.min.js @@ -1,7 +1,7 @@ /*! * jsonformatter * - * Version: 0.2.5 - 2014-12-16T18:51:45.410Z + * Version: 0.2.6 - 2014-12-16T21:33:46.343Z * License: MIT */ -"use strict";angular.module("jsonFormatter",["RecursionHelper"]).directive("jsonFormatter",["RecursionHelper",function(n){function e(n){return n.replace('"','"')}function t(n){if(void 0===n)return"";if(null===n)return"Object";var e=/function (.{1,})\(/,t=e.exec(n.constructor.toString());return t&&t.length>1?t[1]:""}function s(n){n.isArray=function(){return Array.isArray(n.json)},n.isObject=function(){return n.json&&"object"==typeof n.json},n.getKeys=function(){return n.isObject()?Object.keys(n.json):void 0},n.type=typeof n.json,n.hasKey="undefined"!=typeof n.key,n.getConstructorName=function(){return t(n.json)},null===n.json&&(n.type="null"),void 0===n.json&&(n.type="undefined"),"string"===n.type&&("Invalid Date"!==new Date(n.json).toString()&&(n.isDate=!0),0===n.json.indexOf("http")&&(n.isUrl=!0)),n.isEmptyObject=function(){return n.getKeys()&&!n.getKeys().length&&n.isOpen&&!n.isArray()},n.isOpen=!!n.open,n.toggleOpen=function(){n.isOpen=!n.isOpen},n.childrenOpen=function(){return n.open>1?n.open-1:0},n.openLink=function(e){e&&(window.location.href=n.json)},n.parseValue=function(t){return n.type=typeof n.json,"null"===n.type?"null":"undefined"===n.type?"undefined":("string"===n.type&&(t='"'+e(t)+'"'),"function"===n.type?n.json.toString().replace(/\n/g,"").replace(/\{.+?\}/,"")+"{ ... }":t)}}return{templateUrl:"json-formatter.html",restrict:"E",replace:!0,scope:{json:"=",key:"=",open:"="},compile:function(e){return n.compile(e,s)}}}]),angular.module("RecursionHelper",[]).factory("RecursionHelper",["$compile",function(n){return{compile:function(e,t){angular.isFunction(t)&&(t={post:t});var s,r=e.contents().remove();return{pre:t&&t.pre?t.pre:null,post:function(e,o){s||(s=n(r)),s(e,function(n){o.append(n)}),t&&t.post&&t.post.apply(null,arguments)}}}}}]),angular.module("jsonFormatter").run(["$templateCache",function(n){n.put("json-formatter.html",'
{{key}}: {{getConstructorName(json)}} [{{json.length}}] {{parseValue(json)}}
')}]); \ No newline at end of file +"use strict";angular.module("jsonFormatter",["RecursionHelper"]).directive("jsonFormatter",["RecursionHelper",function(n){function e(n){return n.replace('"','"')}function t(n){if(void 0===n)return"";if(null===n)return"Object";var e=/function (.{1,})\(/,t=e.exec(n.constructor.toString());return t&&t.length>1?t[1]:""}function s(n){return null===n?"null":typeof n}function r(n){n.isArray=function(){return Array.isArray(n.json)},n.isObject=function(){return n.json&&"object"==typeof n.json},n.getKeys=function(){return n.isObject()?Object.keys(n.json):void 0},n.type=s(n.json),n.hasKey="undefined"!=typeof n.key,n.getConstructorName=function(){return t(n.json)},"string"===n.type&&("Invalid Date"!==new Date(n.json).toString()&&(n.isDate=!0),0===n.json.indexOf("http")&&(n.isUrl=!0)),n.isEmptyObject=function(){return n.getKeys()&&!n.getKeys().length&&n.isOpen&&!n.isArray()},n.isOpen=!!n.open,n.toggleOpen=function(){n.isOpen=!n.isOpen},n.childrenOpen=function(){return n.open>1?n.open-1:0},n.openLink=function(e){e&&(window.location.href=n.json)},n.parseValue=function(t){return n.type=s(n.json),"null"===n.type?"null":"undefined"===n.type?"undefined":("string"===n.type&&(t='"'+e(t)+'"'),"function"===n.type?n.json.toString().replace(/\n/g,"").replace(/\{.+?\}/,"")+"{ ... }":t)}}return{templateUrl:"json-formatter.html",restrict:"E",replace:!0,scope:{json:"=",key:"=",open:"="},compile:function(e){return n.compile(e,r)}}}]),angular.module("RecursionHelper",[]).factory("RecursionHelper",["$compile",function(n){return{compile:function(e,t){angular.isFunction(t)&&(t={post:t});var s,r=e.contents().remove();return{pre:t&&t.pre?t.pre:null,post:function(e,o){s||(s=n(r)),s(e,function(n){o.append(n)}),t&&t.post&&t.post.apply(null,arguments)}}}}}]),angular.module("jsonFormatter").run(["$templateCache",function(n){n.put("json-formatter.html",'
{{key}}: {{getConstructorName(json)}} [{{json.length}}] {{parseValue(json)}}
')}]); \ No newline at end of file diff --git a/src/json-formatter.js b/src/json-formatter.js index 3a4a127..b486cdf 100644 --- a/src/json-formatter.js +++ b/src/json-formatter.js @@ -23,6 +23,11 @@ angular.module('jsonFormatter', ['RecursionHelper']) } } + function getType(object) { + if (object === null) { return 'null'; } + return typeof object; + } + function link(scope, element, attributes) { scope.isArray = function () { return Array.isArray(scope.json); @@ -37,22 +42,12 @@ angular.module('jsonFormatter', ['RecursionHelper']) return Object.keys(scope.json); } }; - scope.type = typeof scope.json; + scope.type = getType(scope.json); scope.hasKey = typeof scope.key !== 'undefined'; scope.getConstructorName = function(){ return getObjectName(scope.json); }; - // Set custom type for null - if (scope.json === null){ - scope.type = 'null'; - } - - // Set custom type for null - if (scope.json === undefined){ - scope.type = 'undefined'; - } - if (scope.type === 'string'){ // Add custom type for date @@ -91,7 +86,7 @@ angular.module('jsonFormatter', ['RecursionHelper']) }; scope.parseValue = function (value){ - scope.type = typeof scope.json; + scope.type = getType(scope.json); if (scope.type === 'null') { return 'null'; }