├── .babelrc ├── .gitignore ├── .npmignore ├── LICENSE.md ├── README.md ├── docs ├── index.html ├── jsnview.min.css ├── jsnview.min.js └── style.css ├── package.json ├── rollup.config.js └── src ├── index.css ├── index.js └── jsnview.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | [ 4 | "@babel/env" 5 | ] 6 | ], 7 | "plugins": [ 8 | "@babel/plugin-transform-arrow-functions" 9 | ] 10 | } 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.log 2 | npm-debug.log* 3 | 4 | # Dependency directories 5 | node_modules/ 6 | 7 | # project files 8 | examples/ 9 | dist/ 10 | testr/ 11 | build/ 12 | 13 | .cache 14 | .env 15 | 16 | yarn.lock -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | *.log 2 | npm-debug.log* 3 | 4 | .babelrc 5 | rollup.config.js 6 | .whitesource 7 | 8 | coverage 9 | .nyc_output 10 | 11 | node_modules 12 | 13 | package-lock.json 14 | yarn.lock 15 | 16 | src/ 17 | tests/ 18 | examples/ 19 | dist/ 20 | lib/ 21 | helpers/ 22 | util/ 23 | public/ 24 | testr/ 25 | docs/ 26 | 27 | CHANGELOG.md 28 | .cache 29 | .travis.yml 30 | .editorconfig 31 | .eslintignore 32 | .eslintrc 33 | .babelrc 34 | .gitignore 35 | .vscode 36 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | Copyright 2020 Haikel Fazzani 179 | 180 | Licensed under the Apache License, Version 2.0 (the "License"); 181 | you may not use this file except in compliance with the License. 182 | You may obtain a copy of the License at 183 | 184 | http://www.apache.org/licenses/LICENSE-2.0 185 | 186 | Unless required by applicable law or agreed to in writing, software 187 | distributed under the License is distributed on an "AS IS" BASIS, 188 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 189 | See the License for the specific language governing permissions and 190 | limitations under the License. 191 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | json viewer 3 |

4 | 5 |
6 | 7 |
8 | json viewer 9 | json viewer 10 | json viewer 11 | json viewer 12 | json viewer 13 |
14 | 15 | ### Demos 16 | [PLayground](https://wutility.github.io/json-viewer) 17 | [Demo Codepen](https://codepen.io/haikelfazzani-the-bold/pen/bGWKEMP) 18 | 19 | ```bash 20 | $ npm i jsnview 21 | # or 22 | $ yarn add jsnview 23 | ``` 24 | 25 | ## Usage 26 | ```js 27 | import jsnview from 'jsnview'; 28 | import 'jsnview/build/index.css'; 29 | ``` 30 | 31 | Or include it via jsDelivr CDN (UMD): 32 | ```html 33 | 34 | 35 | 36 | ``` 37 | 38 | ### Methods && Examples 39 | - **jsnview(data: Object, options: Object): [HTMLElement]** 40 | ```js 41 | // Default options 42 | const options = { 43 | showLen: false, 44 | showType: false, 45 | showBrackets: true, 46 | showFoldmarker: false, 47 | colors: { boolean: '#ff2929', null: '#ff2929', string: '#690', number: '#905', float: '#002f99' } 48 | } 49 | 50 | let data = { name: 'Mike', age: 22 }; 51 | const treeView = jsnview(data, options); // returns HTMLElement 52 | document.body.appendChild(treeView); 53 | ``` 54 | 55 | ### Notes 56 | - All pull requests are welcome, feel free. 57 | 58 | ### Author 59 | - [Haikel Fazzani](https://github.com/haikelfazzani) 60 | 61 | ## License 62 | Apache License 2.0 63 | -------------------------------------------------------------------------------- /docs/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | JsnView - Ultra Fast and lightweight Json Viewer 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 |
21 | {JsnView} Ultra Fast and lightweight Json Viewer ~3KB 22 |
23 | 24 |
25 | json viewer 26 | json viewer 27 | json viewer 28 | json viewer 29 |
30 | 31 |
$ npm i jsnview
32 | 33 |
34 |
35 | 36 | 38 | Urls for testing: 39 | https://jsonplaceholder.typicode.com/todos/1 40 | https://api.jsonbin.io/b/5fb2bd58dedba573f22230b7 41 | https://api.jsonbin.io/b/5fb2be58dedba573f222310a 42 |
43 | 44 |
45 |
46 | 47 | 51 |
52 | 53 |
54 | 55 | 59 |
60 | 61 |
62 | 63 | 67 |
68 | 69 |
70 | 71 | 75 |
76 |
77 | 78 | 79 |
80 |
81 | 82 |
result here
83 | 84 | 106 | 107 | 108 | -------------------------------------------------------------------------------- /docs/jsnview.min.css: -------------------------------------------------------------------------------- 1 | *{box-sizing:border-box;font-size:16px;font-family:Open Sans,Arial,Helvetica,sans-serif;letter-spacing:.6px}.jsv ul{padding:0 0 0 30px;margin-left:5px;border-left:1px dotted #888}.jsv li,.jsv ul{list-style:none}.jsv{margin:0;padding:0;border:0}.fold{cursor:pointer}.close:after,.close:before,.open:before{font-family:arial;font-size:22px;margin-right:7px;cursor:pointer}.open:before{content:attr(data-before);display:inline-block}.close:before{content:attr(data-close)}.close:after{content:attr(data-after);font-size:30px;margin-left:7px}.len,.type{margin-left:5px}.close:after,.close:before,.len,.open:before,.type{color:#888} -------------------------------------------------------------------------------- /docs/jsnview.min.js: -------------------------------------------------------------------------------- 1 | /*! jsnview - v2.0.4 | Copyright 2022 - Haikel Fazzani */ 2 | !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).jsnview=e()}(this,(function(){"use strict";function t(t){return Number(t)===t&&t%1!=0?"float":{}.toString.call(t).match(/\s([a-zA-Z]+)/)[1].toLowerCase()}function e(t){return document.createElement(t)}let n={showLen:!1,showType:!1,showBrackets:!0,showFoldmarker:!0,colors:{boolean:"#ff2929",null:"#ff2929",string:"#690",number:"#905",float:"#002f99"}};function o(s,l=null,a=!1){for(const c in s){const r=e("li"),i=s[c],p=Array.isArray(i);if((d=i)&&"object"==typeof d&&"Object"===d.constructor.name||p){let t=!1;const s=e("span");if(s.classList.add("fold","open"),s.setAttribute("data-before","▾"),s.setAttribute("data-close","▸"),n.showFoldmarker&&s.setAttribute("data-after","↔"),s.textContent=n.showBrackets?(a?"":`"${c}": `)+(p?"[":"{"):"",r.appendChild(s),n.showLen){const t=e("span"),n=p?i.length:Object.keys(i).length;t.classList.add("len"),t.textContent=`{${n}}`,r.appendChild(t)}const d=e("ul");if(r.appendChild(d),p){const t=e("ul");for(let e=0;e{t=!t,s.parentElement.querySelector("ul").style.display=t?"none":"block",s.classList.add(t?"close":"open"),s.classList.remove(t?"open":"close")};const f=e("span");f.classList.add("fold"),f.textContent=n.showBrackets?p?"]":"}":"",r.appendChild(f),l.appendChild(r)}else{const o=e("span"),s=e("span"),d=t(i);o.style.color=n.colors[d],o.textContent=isNaN(i)?`"${i}"`:i,r.textContent+=a?"":`"${c}": `,r.appendChild(o),n.showType&&(s.classList.add("type"),s.textContent=`(${d})`,r.appendChild(s)),l.appendChild(r)}}var d;return l}return function(t,s){n={...n,...s};const l=e("ul");return l.classList.add("jsv"),o({data:t},l,!0),l}})); 3 | -------------------------------------------------------------------------------- /docs/style.css: -------------------------------------------------------------------------------- 1 | * { 2 | box-sizing: border-box; 3 | } 4 | 5 | body { 6 | height: 100vh; 7 | width: 100vw; 8 | display: grid; 9 | grid-template-columns: 1fr 1fr; 10 | gap: 20px; 11 | background-color: #f1f1f1; 12 | color: #444; 13 | font-family: "Open Sans", Consolas, Georgia, Times, 'Times New Roman', serif; 14 | margin: 0; 15 | padding: 20px 30px; 16 | } 17 | 18 | body>* { 19 | height: 100%; 20 | background-color: #fff; 21 | color: #444; 22 | padding: 25px; 23 | border-radius: 7px; 24 | border: 1px solid #ddd; 25 | overflow: auto; 26 | } 27 | 28 | button,h1, 29 | h3, 30 | a { 31 | width: 100%; 32 | text-transform: uppercase; 33 | } 34 | 35 | a { 36 | color: #2d3748; 37 | text-decoration: none; 38 | } 39 | 40 | .mb-10 { 41 | margin-bottom: 10px !important; 42 | } 43 | 44 | .mr-10 { 45 | margin-right: 10px; 46 | } 47 | 48 | .mb-0 {margin-bottom: 0;} 49 | .m-10>* {margin:10px;} 50 | 51 | select, 52 | input, 53 | button { 54 | padding: 10px 15px; 55 | } 56 | 57 | input, 58 | select { 59 | margin-top: 10px; 60 | } 61 | 62 | button { 63 | background-color: #2d3748; 64 | color: #fff; 65 | cursor: pointer; 66 | outline:none; 67 | } 68 | 69 | button:hover {background-color:#202733;} 70 | 71 | small {font-size: 12px;} 72 | 73 | .w-100 {width: 100%;} 74 | 75 | .d-flex { 76 | display: flex; 77 | } 78 | 79 | .d-flex.flex-grow>* { 80 | flex: 1 1 33%; 81 | } 82 | 83 | .d-flex-col { 84 | display: flex; 85 | flex-direction: column; 86 | } 87 | 88 | .justify-center { 89 | justify-content: center; 90 | } 91 | 92 | .justify-between {justify-content: space-between;} 93 | 94 | .text-center {text-align: center;} 95 | 96 | pre { 97 | background-color: #2d3748; 98 | color: #ffffff; 99 | padding: 10px; 100 | border-radius: 7px; 101 | text-align: center; 102 | font-size: 16px; 103 | max-width: 420px; 104 | margin: 10px auto 20px; 105 | } 106 | 107 | 108 | .flipInX { 109 | -webkit-backface-visibility: visible !important; 110 | backface-visibility: visible !important; 111 | animation: flipInX 2s; 112 | -webkit-animation: flipInX 2s; 113 | } 114 | 115 | 116 | @-webkit-keyframes flipInX { 117 | 0% { -webkit-transform: perspective(400px) rotateX(90deg); opacity: 0; } 118 | 40% { -webkit-transform: perspective(400px) rotateX(-10deg); } 119 | 70% { -webkit-transform: perspective(400px) rotateX(10deg); } 120 | 100% { -webkit-transform: perspective(400px) rotateX(0deg); opacity: 1; } 121 | } 122 | 123 | @keyframes flipInX { 124 | 0% { transform: perspective(400px) rotateX(90deg); opacity: 0; } 125 | 40% { transform: perspective(400px) rotateX(-10deg); } 126 | 70% { transform: perspective(400px) rotateX(10deg); } 127 | 100% { transform: perspective(400px) rotateX(0deg); opacity: 1; } 128 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jsnview", 3 | "version": "2.0.4", 4 | "description": "Ultra Fast and lightweight Json Viewer ~3KB with 0 Dependencies", 5 | "main": "build/index.js", 6 | "module": "build/index.esm.js", 7 | "scripts": { 8 | "copy-css": "cp build/index.css docs/jsnview.min.css", 9 | "copy-js": "cp build/index.js docs/jsnview.min.js", 10 | "watch": "rollup -c -w && yarn copy-js && yarn copy-css", 11 | "build": "rollup -c && yarn copy-js && yarn copy-css" 12 | }, 13 | "devDependencies": { 14 | "rollup": "^2.70.1", 15 | "rollup-plugin-inject-process-env": "^1.3.1", 16 | "rollup-plugin-postcss": "^3.1.8", 17 | "rollup-plugin-terser": "^7.0.2" 18 | }, 19 | "repository": { 20 | "type": "git", 21 | "url": "https://github.com/haikelfazzani/json-view" 22 | }, 23 | "bugs": { 24 | "url": "https://github.com/haikelfazzani/json-view/issues" 25 | }, 26 | "homepage": "https://github.com/haikelfazzani/json-view#readme", 27 | "keywords": [ 28 | "json", 29 | "view", 30 | "json viewer", 31 | "format", 32 | "fast", 33 | "lightweight", 34 | "umd", 35 | "esm" 36 | ], 37 | "author": "Haikel Fazzani", 38 | "license": "MIT" 39 | } -------------------------------------------------------------------------------- /rollup.config.js: -------------------------------------------------------------------------------- 1 | import { terser } from "rollup-plugin-terser"; 2 | import postcss from "rollup-plugin-postcss"; 3 | 4 | const pkg = require('./package.json') 5 | const banner = `/*! jsnview - v${pkg.version} | Copyright 2022 - Haikel Fazzani */\n`; 6 | 7 | const output = [ 8 | { 9 | file: 'build/index.esm.js', 10 | format: 'esm', 11 | sourcemap: true, 12 | 13 | }, 14 | { 15 | name: 'jsnview', 16 | file: 'build/index.js', 17 | format: 'umd', 18 | sourcemap: false, 19 | banner 20 | } 21 | ]; 22 | 23 | export default { 24 | input: "src/index.js", 25 | output, 26 | plugins: [ 27 | postcss({ 28 | babelrc: false, 29 | modules: false, 30 | plugins: [], 31 | extract: true, 32 | minimize: true, 33 | sourceMap: false, 34 | babelHelpers: 'runtime' 35 | }), 36 | terser() 37 | ] 38 | }; 39 | -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | * { 2 | box-sizing: border-box; 3 | font-size: 16px; 4 | font-family: "Open Sans", Arial, Helvetica, sans-serif; 5 | letter-spacing: 0.6px; 6 | } 7 | 8 | .jsv ul { 9 | list-style: none; 10 | padding: 0 0 0 30px; 11 | margin-left: 5px; 12 | border-left: 1px dotted #888888; 13 | } 14 | 15 | .jsv li { list-style: none; } 16 | 17 | 18 | .jsv { margin: 0; padding: 0; border: 0; } 19 | 20 | .fold { cursor: pointer; } 21 | 22 | .open:before, 23 | .close::after , .close:before{ 24 | font-family: arial; 25 | font-size: 22px; 26 | margin-right: 7px; 27 | cursor: pointer; 28 | } 29 | 30 | .open:before { content: attr(data-before); display: inline-block; } 31 | 32 | .close:before { content: attr(data-close); } 33 | 34 | /* foldmarker */ 35 | .close::after { content: attr(data-after); font-size: 30px; margin-left: 7px; } 36 | 37 | .len,.type { margin-left: 5px; } 38 | 39 | .type, 40 | .len, 41 | .open:before, 42 | .close::after, 43 | .close:before { 44 | color: #888888; 45 | } -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import jsnview from './jsnview'; 2 | import './index.css'; 3 | export default jsnview; -------------------------------------------------------------------------------- /src/jsnview.js: -------------------------------------------------------------------------------- 1 | function isObj(o) { 2 | return o && typeof o === 'object' && o.constructor.name === 'Object' 3 | } 4 | 5 | function getType(value) { 6 | let isFloat = Number(value) === value && value % 1 !== 0; 7 | return isFloat ? 'float' : ({}).toString.call(value).match(/\s([a-zA-Z]+)/)[1].toLowerCase(); 8 | } 9 | 10 | function createEl(el) { 11 | return document.createElement(el); 12 | } 13 | 14 | let config = { 15 | showLen: false, 16 | showType: false, 17 | showBrackets: true, 18 | showFoldmarker: true, 19 | colors: { boolean: '#ff2929', null: '#ff2929', string: '#690', number: '#905', float: '#002f99' } 20 | } 21 | 22 | export default function jsnview(data, options) { 23 | config = { ...config, ...options }; 24 | const root = createEl('ul'); 25 | root.classList.add('jsv'); 26 | createTree({ data }, root, true); 27 | return root 28 | } 29 | 30 | function createTree(data, parentEl = null, isArray = false) { 31 | for (const key in data) { 32 | const li = createEl('li'), 33 | value = data[key], 34 | isArr = Array.isArray(value); 35 | 36 | if (isObj(value) || isArr) { 37 | let isOpen = false; 38 | const startDel = createEl('span'); 39 | startDel.classList.add('fold', 'open'); 40 | startDel.setAttribute('data-before', "▾"); 41 | startDel.setAttribute('data-close', "▸"); 42 | if(config.showFoldmarker) startDel.setAttribute('data-after', "↔"); 43 | 44 | startDel.textContent = config.showBrackets ? (isArray ? "" : `"${key}": `) + (isArr ? "[" : "{") : ''; 45 | li.appendChild(startDel); 46 | 47 | if (config.showLen) { 48 | const spanLen = createEl('span'); 49 | const len = isArr ? value.length : Object.keys(value).length; 50 | spanLen.classList.add('len'); 51 | spanLen.textContent = `{${len}}`; 52 | li.appendChild(spanLen) 53 | } 54 | 55 | const ul = createEl('ul'); 56 | li.appendChild(ul); 57 | 58 | if (isArr) { 59 | const np = createEl('ul'); 60 | for (let i = 0; i < value.length; i++) { 61 | createTree(value[i], np, true); 62 | } 63 | } 64 | 65 | createTree(value, ul, isArr); 66 | 67 | startDel.onclick = () => { 68 | isOpen = !isOpen; 69 | startDel.parentElement.querySelector('ul').style.display = isOpen ? 'none' : 'block'; 70 | startDel.classList.add(isOpen ? 'close' : 'open'); 71 | startDel.classList.remove(isOpen ? 'open' : 'close'); 72 | } 73 | 74 | const endDel = createEl('span'); 75 | endDel.classList.add('fold'); 76 | endDel.textContent = !config.showBrackets ? '' : isArr ? ']' : '}'; 77 | li.appendChild(endDel) 78 | parentEl.appendChild(li); 79 | } 80 | else { 81 | const spanVal = createEl('span'), 82 | spanType = createEl('span'), 83 | valType = getType(value); 84 | 85 | spanVal.style.color = config.colors[valType]; 86 | spanVal.textContent = isNaN(value) ? `"${value}"` : value; 87 | 88 | li.textContent += isArray ? "" : `"${key}": `; 89 | li.appendChild(spanVal) 90 | 91 | if (config.showType) { 92 | spanType.classList.add('type'); 93 | spanType.textContent = `(${valType})`; 94 | li.appendChild(spanType) 95 | } 96 | 97 | parentEl.appendChild(li) 98 | } 99 | } 100 | 101 | return parentEl 102 | } 103 | --------------------------------------------------------------------------------