diff --git a/src/tools/file-splitter/file-splitter.service.test.ts b/src/tools/file-splitter/file-splitter.service.test.ts new file mode 100644 index 00000000..c7cf4c93 --- /dev/null +++ b/src/tools/file-splitter/file-splitter.service.test.ts @@ -0,0 +1,94 @@ +import { describe, expect, it } from 'vitest'; +import { splitContent } from './file-splitter.service'; + +describe('splitContent - lines mode', () => { + const content = 'a\nb\nc\nd\ne'; + + it('splits by maxSize', () => { + const result = splitContent(content, 'lines', 6, 'maxSize'); + expect(result).toEqual(['a\nb\nc', 'd\ne']); + }); + + it('splits by fixedCount', () => { + const result = splitContent(content, 'lines', 2, 'fixedCount'); + expect(result).toEqual(['a\nb', 'c\nd', 'e']); + }); + + it('splits by chunkCount', () => { + const result = splitContent(content, 'lines', 2, 'chunkCount'); + expect(result).toEqual(['a\nb\nc', 'd\ne']); + }); +}); + +describe('splitContent - nodes mode with JSON array', () => { + const jsonArray = JSON.stringify([1, 2, 3, 4, 5]); + + it('splits by maxSize', () => { + const result = splitContent(jsonArray, 'nodes', 6, 'maxSize'); + expect(result).toEqual(['[1,2]', '[3,4]', '[5]']); + }); + + it('splits by fixedCount', () => { + const result = splitContent(jsonArray, 'nodes', 3, 'fixedCount'); + expect(result).toEqual(['[1,2,3]', '[4,5]']); + }); + + it('splits by chunkCount', () => { + const result = splitContent(jsonArray, 'nodes', 4, 'chunkCount'); + expect(result).toEqual(['[1,2]', '[3,4]', '[5]']); + }); +}); + +describe('splitContent - nodes mode with JSON object', () => { + const jsonObject = JSON.stringify({ a: 1, b: 2, c: 3, d: 4 }); + + it('splits by maxSize', () => { + const result = splitContent(jsonObject, 'nodes', 14, 'maxSize'); + expect(result).toEqual([ + '{"a":1}', + '{"b":2}', + '{"c":3}', + '{"d":4}', + ]); + }); + + it('splits by fixedCount', () => { + const result = splitContent(jsonObject, 'nodes', 3, 'fixedCount'); + expect(result).toEqual(['{"a":1,"b":2,"c":3}', '{"d":4}']); + }); + + it('splits by chunkCount', () => { + const result = splitContent(jsonObject, 'nodes', 2, 'chunkCount'); + expect(result).toEqual(['{"a":1,"b":2}', '{"c":3,"d":4}']); + }); +}); + +describe('splitContent - nodes mode with XML', () => { + const xml = '1234'; + + it('splits by maxSize', () => { + const result = splitContent(xml, 'nodes', 30, 'maxSize'); + expect(result).toEqual([ + '12', + '34', + ]); + }); + + it('splits by fixedCount', () => { + const result = splitContent(xml, 'nodes', 3, 'fixedCount'); + expect(result).toEqual([ + '123', + '4', + ]); + }); + + it('splits by chunkCount', () => { + const result = splitContent(xml, 'nodes', 4, 'chunkCount'); + expect(result).toEqual([ + '1', + '2', + '3', + '4', + ]); + }); +}); diff --git a/src/tools/file-splitter/file-splitter.service.ts b/src/tools/file-splitter/file-splitter.service.ts new file mode 100644 index 00000000..2ad27b36 --- /dev/null +++ b/src/tools/file-splitter/file-splitter.service.ts @@ -0,0 +1,167 @@ +function chunkByFixedCount(arr: T[], count: number): T[][] { + const result: T[][] = []; + for (let i = 0; i < arr.length; i += count) { + result.push(arr.slice(i, i + count)); + } + return result; +} + +function chunkByChunkCount(arr: T[], chunks: number): T[][] { + const result: T[][] = []; + const chunkSize = Math.ceil(arr.length / chunks); + for (let i = 0; i < arr.length; i += chunkSize) { + result.push(arr.slice(i, i + chunkSize)); + } + return result; +} + +export function splitContent( + content: string, + mode: 'lines' | 'nodes', + count: number, + strategy: 'maxSize' | 'fixedCount' | 'chunkCount' = 'maxSize', +): string[] { + const getChunks = (arr: T[]): T[][] => { + if (strategy === 'fixedCount') { + return chunkByFixedCount(arr, count); + } + if (strategy === 'chunkCount') { + return chunkByChunkCount(arr, count); + } + return []; // handled separately for maxSize + }; + + if (mode === 'lines') { + const lines = content.split(/\r?\n/); + + if (strategy === 'maxSize') { + const result: string[] = []; + let chunk: string[] = []; + let size = 0; + + for (const line of lines) { + const lineSize = line.length + 1; + if (size + lineSize > count && chunk.length > 0) { + result.push(chunk.join('\n')); + chunk = []; + size = 0; + } + chunk.push(line); + size += lineSize; + } + + if (chunk.length > 0) { + result.push(chunk.join('\n')); + } + return result; + } + + return getChunks(lines).map(group => group.join('\n')); + } + + try { + const parsed = JSON.parse(content); + + if (Array.isArray(parsed)) { + if (strategy === 'maxSize') { + const result: string[] = []; + let chunk: any[] = []; + let size = 0; + + for (const item of parsed) { + const itemStr = JSON.stringify(item); + const itemSize = itemStr.length + 2; + if (size + itemSize > count && chunk.length > 0) { + result.push(JSON.stringify(chunk)); + chunk = []; + size = 0; + } + chunk.push(item); + size += itemSize; + } + + if (chunk.length > 0) { + result.push(JSON.stringify(chunk)); + } + return result; + } + + return getChunks(parsed).map(chunk => JSON.stringify(chunk)); + } + + if (typeof parsed === 'object') { + const entries = Object.entries(parsed); + + if (strategy === 'maxSize') { + const result: string[] = []; + let chunk: [string, any][] = []; + let size = 0; + + for (const [key, value] of entries) { + const entryStr = JSON.stringify({ [key]: value }); + const entrySize = entryStr.length + 2; + if (size + entrySize > count && chunk.length > 0) { + result.push(JSON.stringify(Object.fromEntries(chunk))); + chunk = []; + size = 0; + } + chunk.push([key, value]); + size += entrySize; + } + + if (chunk.length > 0) { + result.push(JSON.stringify(Object.fromEntries(chunk))); + } + return result; + } + + return getChunks(entries).map(chunk => + JSON.stringify(Object.fromEntries(chunk)), + ); + } + } + catch { + const parser = new DOMParser(); + const xmlDoc = parser.parseFromString(content, 'application/xml'); + const root = xmlDoc.documentElement; + const children = Array.from(root.children); + + if (strategy === 'maxSize') { + const result: string[] = []; + let chunk: Element[] = []; + + for (const child of children) { + const tempDoc = document.implementation.createDocument('', root.tagName, null); + chunk.forEach(c => tempDoc.documentElement.appendChild(c.cloneNode(true))); + tempDoc.documentElement.appendChild(child.cloneNode(true)); + const serialized = new XMLSerializer().serializeToString(tempDoc); + const chunkSize = serialized.length; + + if (chunkSize > count && chunk.length > 0) { + const frag = document.implementation.createDocument('', root.tagName, null); + chunk.forEach(c => frag.documentElement.appendChild(c.cloneNode(true))); + result.push(new XMLSerializer().serializeToString(frag)); + chunk = []; + } + + chunk.push(child); + } + + if (chunk.length > 0) { + const frag = document.implementation.createDocument('', root.tagName, null); + chunk.forEach(c => frag.documentElement.appendChild(c.cloneNode(true))); + result.push(new XMLSerializer().serializeToString(frag)); + } + + return result; + } + + return getChunks(children).map((group) => { + const frag = document.implementation.createDocument('', root.tagName, null); + group.forEach(child => frag.documentElement.appendChild(child.cloneNode(true))); + return new XMLSerializer().serializeToString(frag); + }); + } + + throw new Error('Unsupported format or invalid content'); +} diff --git a/src/tools/file-splitter/file-splitter.vue b/src/tools/file-splitter/file-splitter.vue new file mode 100644 index 00000000..a52f7f02 --- /dev/null +++ b/src/tools/file-splitter/file-splitter.vue @@ -0,0 +1,151 @@ + + + diff --git a/src/tools/file-splitter/index.ts b/src/tools/file-splitter/index.ts new file mode 100644 index 00000000..7656c0ed --- /dev/null +++ b/src/tools/file-splitter/index.ts @@ -0,0 +1,13 @@ +import { Cut } from '@vicons/tabler'; +import { defineTool } from '../tool'; + +export const tool = defineTool({ + name: 'File Splitter', + path: '/file-splitter', + description: 'Split JSON, XML or text file either by group of nodes or by group of lines', + keywords: ['file', 'json', 'xml', 'text', 'splitter'], + component: () => import('./file-splitter.vue'), + icon: Cut, + createdAt: new Date('2025-11-11'), + category: 'Development', +});