Improve code formatting and readability, handle edge cases in tests, and enhance lazy path extraction logic

This commit is contained in:
2026-01-29 03:47:37 +00:00
parent 9507059ad9
commit e98cb05b14
2 changed files with 75 additions and 77 deletions

View File

@@ -108,12 +108,7 @@ export default function useJsonFilter() {
* @param {Function} callback - Callback with new paths
* @param {number} chunkSize - Items per chunk
*/
function extractPathsLazy(
data,
startIndex = 100,
callback,
chunkSize = 100
) {
function extractPathsLazy(data, startIndex = 100, callback, chunkSize = 100) {
if (!Array.isArray(data) || startIndex >= data.length) {
return;
}
@@ -131,7 +126,9 @@ export default function useJsonFilter() {
}
});
const addedPaths = Array.from(newPaths).filter(p => !existingPaths.has(p));
const addedPaths = Array.from(newPaths).filter(
p => !existingPaths.has(p)
);
if (addedPaths.length > 0) {
addedPaths.forEach(p => existingPaths.add(p));
@@ -298,7 +295,8 @@ export default function useJsonFilter() {
const value = filterValue.value || '*';
const mode = filterMode.value;
const modeLabel = {
const modeLabel =
{
equals: '=',
contains: '∋',
regex: '~'

View File

@@ -14,7 +14,7 @@ describe('useJsonFilter', () => {
{ id: 2, name: 'Charizard', type: 'Fire', level: 50 },
{ id: 3, name: 'Blastoise', type: 'Water', level: 50 },
{ id: 4, name: 'Venusaur', type: 'Grass', level: 50 },
{ id: 5, name: 'Pikachu', type: 'Electric', level: 30 },
{ id: 5, name: 'Pikachu', type: 'Electric', level: 30 }
];
const nestedData = [
@@ -140,14 +140,18 @@ describe('useJsonFilter', () => {
filter.setFilter('name', 'Pikachu', 'equals');
expect(filter.filteredData.value.length).toBe(2);
expect(filter.filteredData.value.every(item => item.name === 'Pikachu')).toBeTruthy();
expect(
filter.filteredData.value.every(item => item.name === 'Pikachu')
).toBeTruthy();
});
it('should filter by exact number match', () => {
filter.setFilter('level', '50', 'equals');
expect(filter.filteredData.value.length).toBe(3);
expect(filter.filteredData.value.every(item => item.level === 50)).toBeTruthy();
expect(
filter.filteredData.value.every(item => item.level === 50)
).toBeTruthy();
});
it('should be case-insensitive', () => {
@@ -219,9 +223,9 @@ describe('useJsonFilter', () => {
filter.setFilter('name', '(Pikachu|Charizard)', 'regex');
const names = filter.filteredData.value.map(item => item.name);
expect(names.every(name =>
name === 'Pikachu' || name === 'Charizard'
)).toBeTruthy();
expect(
names.every(name => name === 'Pikachu' || name === 'Charizard')
).toBeTruthy();
});
it('should be case-insensitive by default in regex', () => {
@@ -459,7 +463,7 @@ describe('useJsonFilter', () => {
});
describe('Lazy Path Extraction', () => {
it('should extract paths lazily from large datasets', (done) => {
it('should extract paths lazily from large datasets', done => {
const largeData = Array.from({ length: 300 }, (_, i) => ({
id: i,
name: `Item ${i}`,
@@ -470,7 +474,7 @@ describe('useJsonFilter', () => {
filter.initializeFilter(largeData.slice(0, 100));
const initialPathCount = filter.availablePaths.value.length;
filter.extractPathsLazy(largeData, 100, (newPaths) => {
filter.extractPathsLazy(largeData, 100, newPaths => {
expect(filter.availablePaths.value.length).toBeGreaterThanOrEqual(
initialPathCount
);
@@ -536,9 +540,7 @@ describe('useJsonFilter', () => {
});
it('should handle arrays in data without recursing', () => {
const dataWithArrays = [
{ id: 1, tags: ['a', 'b', 'c'], name: 'test' }
];
const dataWithArrays = [{ id: 1, tags: ['a', 'b', 'c'], name: 'test' }];
filter.initializeFilter(dataWithArrays);
const paths = filter.availablePaths.value.map(p => p.path);
@@ -548,9 +550,7 @@ describe('useJsonFilter', () => {
});
it('should handle numeric keys in objects', () => {
const dataWithNumericKeys = [
{ '1': 'numeric', '2': 'keys', name: 'test' }
];
const dataWithNumericKeys = [{ 1: 'numeric', 2: 'keys', name: 'test' }];
filter.initializeFilter(dataWithNumericKeys);
const paths = filter.availablePaths.value.map(p => p.path);
@@ -562,7 +562,7 @@ describe('useJsonFilter', () => {
it('should handle unicode characters in paths', () => {
const unicodeData = [
{ '名前': 'Japanese', 'nome': 'Italian', name: 'English' }
{ 名前: 'Japanese', nome: 'Italian', name: 'English' }
];
filter.initializeFilter(unicodeData);