Merge branch 'feat/json-size' into chore/all-my-stuffs

# Conflicts:
#	package.json
#	pnpm-lock.yaml
#	src/tools/index.ts
This commit is contained in:
ShareVB committed 2024-11-03 20:56:45 +01:00
commit efd30f9be6
8 files changed
+303 -1

No files matched your search

+1
View File
@@ -130,6 +130,7 @@
"image-in-browser": "^3.1.0",
"js-base64": "^3.7.7",
"json-editor-vue": "^0.16.0",
"json-analyzer": "^1.2.2",
"json5": "^2.2.3",
"jsonpath": "^1.1.1",
"jsonar-mod": "^1.9.0",
+43 -1
View File
@@ -245,6 +245,9 @@ dependencies:
jsbarcode:
specifier: ^3.11.6
version: 3.11.6
json-analyzer:
specifier: ^1.2.2
version: 1.2.2
json-editor-vue:
specifier: ^0.16.0
version: 0.16.0(@lezer/common@1.2.3)(vue@3.3.4)
@@ -5734,6 +5737,11 @@ packages:
resolution: {integrity: sha512-IfEDxwoWIjkeXL1eXcDiow4UbKjhLdq6/EuSVR9GMN7KVH3r9gQ83e73hsz1Nd1T3ijd5xv1wcWRYO+D6kCI2w==}
dev: true
/colors@1.4.0:
resolution: {integrity: sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA==}
engines: {node: '>=0.1.90'}
dev: false
/combined-stream@1.0.8:
resolution: {integrity: sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==}
engines: {node: '>= 0.8'}
@@ -8536,6 +8544,21 @@ packages:
engines: {node: '>=6'}
hasBin: true
/json-analyzer@1.2.2:
resolution: {integrity: sha512-3xWTxyIggxOYIPT9NkucQPxlOBPJY14/ifely3eCtifE5pAxJXl/jUmSDwq+fLVhHIIj9MIJ6Bv7u3ItChG8vQ==}
engines: {node: '>=8.0.0'}
hasBin: true
dependencies:
commander: 2.20.3
lodash.flowright: 3.5.0
lodash.get: 4.4.2
lodash.isobject: 3.0.2
pako: 1.0.11
pretty-bytes: 5.6.0
prettyjson: 1.2.5
utf8-length: 0.0.1
dev: false
/json-editor-vue@0.16.0(@lezer/common@1.2.3)(vue@3.3.4):
resolution: {integrity: sha512-8axbIR1DD6cf/AAXDkEYkPHDbWzEQDP762j2GiX3fFnHmE5+RzkcCFV2J/TGr6Mlw9sdZpQVMPHtt+7qYJ8Xyg==}
requiresBuild: true
@@ -8754,10 +8777,22 @@ packages:
resolution: {integrity: sha512-uHaJFihxmJcEX3kT4I23ABqKKalJ/zDrDg0lsFtc1h+3uw49SIJ5beyhx5ExVRti3AvKoOJngIj7xz3oylPdWQ==}
dev: false
/lodash.flowright@3.5.0:
resolution: {integrity: sha512-YxTYuodkvyINbDInmFcGGvkQwoAuoGUYosqstRTr5eq63GQt7WQ2xFU0wG1UfdbKYPwevd3zWDd6ybEE2g6qvA==}
dev: false
/lodash.get@4.4.2:
resolution: {integrity: sha512-z+Uw/vLuy6gQe8cfaFWD7p0wVv8fJl3mbzXh33RS+0oW2wvUqiRXiQ69gLWSLpgB5/6sU+r6BlQR0MBILadqTQ==}
dev: false
/lodash.isempty@4.4.0:
resolution: {integrity: sha512-oKMuF3xEeqDltrGMfDxAPGIVMSSRv8tbRSODbrs4KGsRRLEhrW8N8Rd4DRgB2+621hY8A8XwwrTVhXWpxFvMzg==}
dev: false
/lodash.isobject@3.0.2:
resolution: {integrity: sha512-3/Qptq2vr7WeJbB4KHUSKlq8Pl7ASXi3UG6CMbBm8WRtXi8+GHm7mKaU3urfpSEzWe2wCIChs6/sdocUsTKJiA==}
dev: false
/lodash.isplainobject@4.0.6:
resolution: {integrity: sha512-oSXzaWypCMHkPC3NvBEaPHf0KsA5mvPrOPgQWDsbg8n7orZ290M0BmC/jgRZ4vcJ6DTAhjrsSYgdsW/F+MFOBA==}
dev: false
@@ -9884,7 +9919,6 @@ packages:
/pretty-bytes@5.6.0:
resolution: {integrity: sha512-FFw039TmrBqFK8ma/7OL3sDz/VytdtJr044/QUJtH0wK9lb9jLq9tJyIxUwtQJHwar2BqtiA4iCWSwo9JLkzFg==}
engines: {node: '>=6'}
dev: true
/pretty-bytes@6.1.0:
resolution: {integrity: sha512-Rk753HI8f4uivXi4ZCIYdhmG1V+WKzvRMg/X+M42a6t7D07RcmopXJMDNk6N++7Bl75URRGsb40ruvg7Hcp2wQ==}
@@ -9907,6 +9941,14 @@ packages:
parse-ms: 4.0.0
dev: false
/prettyjson@1.2.5:
resolution: {integrity: sha512-rksPWtoZb2ZpT5OVgtmy0KHVM+Dca3iVwWY9ifwhcexfjebtgjg3wmrUt9PvJ59XIYBcknQeYHD8IAnVlh9lAw==}
hasBin: true
dependencies:
colors: 1.4.0
minimist: 1.2.8
dev: false
/process-nextick-args@2.0.1:
resolution: {integrity: sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==}
dev: false
+2
View File
@@ -22,6 +22,7 @@ import { tool as jqMemo } from './jq-memo';
import { tool as jsUnobfuscator } from './js-unobfuscator';
import { tool as jsonToPhpArray } from './json-to-php-array';
import { tool as phpArrayToJson } from './php-array-to-json';
import { tool as jsonSizeAnalyzer } from './json-size-analyzer';
import { tool as cssXpathConverter } from './css-xpath-converter';
import { tool as cssSelectorsMemo } from './css-selectors-memo';
@@ -233,6 +234,7 @@ export const toolsByCategory: ToolCategory[] = [
crontabGenerator,
jsonViewer,
jsonMinify,
jsonSizeAnalyzer,
jsonToCsv,
sqlPrettify,
chmodCalculator,
+12
View File
@@ -0,0 +1,12 @@
import { FileAnalytics } from '@vicons/tabler';
import { defineTool } from '../tool';
export const tool = defineTool({
name: 'Json Size Analyzer',
path: '/json-size-analyzer',
description: 'Measure JSON nodes relative weights',
keywords: ['json', 'size', 'analyzer'],
component: () => import('./json-size-analyzer.vue'),
icon: FileAnalytics,
createdAt: new Date('2024-07-14'),
});
+13
View File
@@ -0,0 +1,13 @@
declare module 'json-analyzer' {
export default function analyze({
json,
verbose,
maxDepth,
target,
}: {
json: any,
verbose: boolean,
maxDepth: number,
target: string,
});
}
@@ -0,0 +1,113 @@
import { describe, expect, it } from 'vitest';
import { getJsonUsageTreeNodes } from './json-size-analyzer.service';
describe('json-size-analyzer', () => {
describe('getJsonUsageTreeNodes', () => {
it('return correct tree nodes structures', () => {
expect(getJsonUsageTreeNodes([{ a: [1, 2, 3] }, { b: 'a' }])).to.deep.eq({
children: [
{
children: [
{
children: [
{
children: [],
key: '$.[0].a.[0]',
label: '$.[0].a.[0]: 1 B(26 B gzip)',
},
{
children: [],
key: '$.[0].a.[1]',
label: '$.[0].a.[1]: 1 B(24 B gzip)',
},
{
children: [],
key: '$.[0].a.[2]',
label: '$.[0].a.[2]: 1 B(25 B gzip)',
},
],
key: '$.[0].a',
label: '$.[0].a: 7 B(35 B gzip) ; 28.000% of parent ; biggest child node: \'0\'',
},
],
key: '$.[0]',
label: '$.[0]: 13 B(43 B gzip) ; 52.000% of parent ; biggest child node: \'a\'',
},
{
children: [
{
children: [],
key: '$.[1].b',
label: '$.[1].b: 1 B(25 B gzip)',
},
],
key: '$.[1]',
label: '$.[1]: 9 B(34 B gzip) ; 36.000% of parent ; biggest child node: \'b\'',
},
],
key: '$',
label: '$: 25 B(61 B gzip) ; 100.00% of parent ; biggest child node: \'0\'',
});
expect(getJsonUsageTreeNodes({ a: { b: [1, 2, 3], c: 12 } })).to.deep.eq({
children: [
{
children: [
{
children: [
{
children: [],
key: '$.a.b.[0]',
label: '$.a.b.[0]: 1 B(26 B gzip)',
},
{
children: [],
key: '$.a.b.[1]',
label: '$.a.b.[1]: 1 B(24 B gzip)',
},
{
children: [],
key: '$.a.b.[2]',
label: '$.a.b.[2]: 1 B(25 B gzip)',
},
],
key: '$.a.b',
label: '$.a.b: 7 B(35 B gzip) ; 26.923% of parent ; biggest child node: \'0\'',
},
{
children: [],
key: '$.a.c',
label: '$.a.c: 2 B(24 B gzip)',
},
],
key: '$.a',
label: '$.a: 20 B(50 B gzip) ; 76.923% of parent ; biggest child node: \'b\'',
},
],
key: '$',
label: '$: 26 B(63 B gzip) ; 100.00% of parent ; biggest child node: \'a\'',
});
expect(getJsonUsageTreeNodes({ a: { b: 'azerty', c: 'ueop' } })).to.deep.eq({
children: [
{
children: [
{
children: [],
key: '$.a.b',
label: '$.a.b: 6 B(30 B gzip)',
},
{
children: [],
key: '$.a.c',
label: '$.a.c: 4 B(29 B gzip)',
},
],
key: '$.a',
label: '$.a: 25 B(51 B gzip) ; 80.645% of parent ; biggest child node: \'b\'',
},
],
key: '$',
label: '$: 31 B(61 B gzip) ; 100.00% of parent ; biggest child node: \'a\'',
});
});
});
});
@@ -0,0 +1,51 @@
import jsonAnalyzer from 'json-analyzer';
export interface Meta {
__meta__: {
size?: {
value: number
raw: string
gzip: string
}
number_of_childs?: number
parent_relative_percentage?: string
biggest_node_child: string
}
}
export type AnalysisNode = {
[key: string]: object & Meta
} & Meta;
export type TreeNode = {
key: string
label: string
children: Array<TreeNode>
} & Record<string, unknown>;
function getTreeNodes(obj: AnalysisNode, parentName: string): TreeNode {
const childNodes = Object.entries(obj)
.filter(([key, v]) => key !== '__meta__' && typeof v === 'object')
.map(([k, v]) => ({
key: (Number.isNaN(Number.parseInt(k, 10)) ? `.${k}` : `.[${k}]`),
value: v as AnalysisNode,
}));
const biggest_child_node = obj.__meta__.biggest_node_child ? ` ; biggest child node: '${obj.__meta__.biggest_node_child}'` : '';
const parent_relative_percentage = obj.__meta__.parent_relative_percentage ? ` ; ${obj.__meta__.parent_relative_percentage} of parent` : '';
return {
key: parentName,
label: obj.__meta__
? `${parentName}: ${obj.__meta__.size?.raw}(${obj.__meta__.size?.gzip} gzip)${parent_relative_percentage}${biggest_child_node}`
: parentName,
children: childNodes.map(childNode => getTreeNodes(childNode.value, parentName + childNode.key)),
};
}
export function getJsonUsageTreeNodes(jsonObj: any, maxDepth: number = 100, targetNode: string = ''): TreeNode {
const analysis = jsonAnalyzer({
json: jsonObj,
verbose: true,
maxDepth,
target: targetNode,
});
return getTreeNodes(analysis, '$');
}
@@ -0,0 +1,68 @@
<script setup lang="ts">
import JSON5 from 'json5';
import { getJsonUsageTreeNodes } from './json-size-analyzer.service';
import { useValidation } from '@/composable/validation';
const json = ref('{"a": 1, "b": [1,2,3]}');
const maxDepth = ref(100);
const target = ref('');
const jsonSizes = computed(() => {
const jsonObj = JSON5.parse(json.value);
if (!jsonObj) {
return null;
}
return [getJsonUsageTreeNodes(jsonObj, maxDepth.value - 1, target.value)];
});
const searchInAnalysis = ref('');
const jsonValidation = useValidation({
source: json,
rules: [
{
validator: (v) => {
return JSON5.parse(v);
},
message: 'Provided JSON is not valid.',
},
],
});
</script>
<template>
<div style="max-width: 600px;">
<c-card title="Input" mb-2>
<c-input-text
v-model:value="json"
label="JSON"
multiline
placeholder="Put your JSON data here..."
rows="5"
:validation="jsonValidation"
mb-2
/>
<n-form-item label="Max Depth:" label-placement="left">
<n-input-number v-model:value="maxDepth" :min="0" w-full />
</n-form-item>
<c-input-text
v-model:value="target"
label="Target Node"
placeholder="Where to start the analyze (ie, a[0].b.c)"
mb-2
/>
</c-card>
<c-card v-if="jsonSizes" title="Analysis">
<n-input v-model:value="searchInAnalysis" placeholder="Search in result" />
<n-tree
:show-irrelevant-nodes="false"
:pattern="searchInAnalysis"
:default-expand-all="true"
:data="jsonSizes"
block-line
/>
</c-card>
</div>
</template>