✨ Improve code formatting and readability, handle edge cases in tests, and enhance lazy path extraction logic
This commit is contained in:
@@ -108,12 +108,7 @@ export default function useJsonFilter() {
|
|||||||
* @param {Function} callback - Callback with new paths
|
* @param {Function} callback - Callback with new paths
|
||||||
* @param {number} chunkSize - Items per chunk
|
* @param {number} chunkSize - Items per chunk
|
||||||
*/
|
*/
|
||||||
function extractPathsLazy(
|
function extractPathsLazy(data, startIndex = 100, callback, chunkSize = 100) {
|
||||||
data,
|
|
||||||
startIndex = 100,
|
|
||||||
callback,
|
|
||||||
chunkSize = 100
|
|
||||||
) {
|
|
||||||
if (!Array.isArray(data) || startIndex >= data.length) {
|
if (!Array.isArray(data) || startIndex >= data.length) {
|
||||||
return;
|
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) {
|
if (addedPaths.length > 0) {
|
||||||
addedPaths.forEach(p => existingPaths.add(p));
|
addedPaths.forEach(p => existingPaths.add(p));
|
||||||
@@ -298,7 +295,8 @@ export default function useJsonFilter() {
|
|||||||
const value = filterValue.value || '*';
|
const value = filterValue.value || '*';
|
||||||
const mode = filterMode.value;
|
const mode = filterMode.value;
|
||||||
|
|
||||||
const modeLabel = {
|
const modeLabel =
|
||||||
|
{
|
||||||
equals: '=',
|
equals: '=',
|
||||||
contains: '∋',
|
contains: '∋',
|
||||||
regex: '~'
|
regex: '~'
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ describe('useJsonFilter', () => {
|
|||||||
{ id: 2, name: 'Charizard', type: 'Fire', level: 50 },
|
{ id: 2, name: 'Charizard', type: 'Fire', level: 50 },
|
||||||
{ id: 3, name: 'Blastoise', type: 'Water', level: 50 },
|
{ id: 3, name: 'Blastoise', type: 'Water', level: 50 },
|
||||||
{ id: 4, name: 'Venusaur', type: 'Grass', 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 = [
|
const nestedData = [
|
||||||
@@ -140,14 +140,18 @@ describe('useJsonFilter', () => {
|
|||||||
filter.setFilter('name', 'Pikachu', 'equals');
|
filter.setFilter('name', 'Pikachu', 'equals');
|
||||||
|
|
||||||
expect(filter.filteredData.value.length).toBe(2);
|
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', () => {
|
it('should filter by exact number match', () => {
|
||||||
filter.setFilter('level', '50', 'equals');
|
filter.setFilter('level', '50', 'equals');
|
||||||
|
|
||||||
expect(filter.filteredData.value.length).toBe(3);
|
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', () => {
|
it('should be case-insensitive', () => {
|
||||||
@@ -219,9 +223,9 @@ describe('useJsonFilter', () => {
|
|||||||
filter.setFilter('name', '(Pikachu|Charizard)', 'regex');
|
filter.setFilter('name', '(Pikachu|Charizard)', 'regex');
|
||||||
|
|
||||||
const names = filter.filteredData.value.map(item => item.name);
|
const names = filter.filteredData.value.map(item => item.name);
|
||||||
expect(names.every(name =>
|
expect(
|
||||||
name === 'Pikachu' || name === 'Charizard'
|
names.every(name => name === 'Pikachu' || name === 'Charizard')
|
||||||
)).toBeTruthy();
|
).toBeTruthy();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should be case-insensitive by default in regex', () => {
|
it('should be case-insensitive by default in regex', () => {
|
||||||
@@ -459,7 +463,7 @@ describe('useJsonFilter', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('Lazy Path Extraction', () => {
|
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) => ({
|
const largeData = Array.from({ length: 300 }, (_, i) => ({
|
||||||
id: i,
|
id: i,
|
||||||
name: `Item ${i}`,
|
name: `Item ${i}`,
|
||||||
@@ -470,7 +474,7 @@ describe('useJsonFilter', () => {
|
|||||||
filter.initializeFilter(largeData.slice(0, 100));
|
filter.initializeFilter(largeData.slice(0, 100));
|
||||||
const initialPathCount = filter.availablePaths.value.length;
|
const initialPathCount = filter.availablePaths.value.length;
|
||||||
|
|
||||||
filter.extractPathsLazy(largeData, 100, (newPaths) => {
|
filter.extractPathsLazy(largeData, 100, newPaths => {
|
||||||
expect(filter.availablePaths.value.length).toBeGreaterThanOrEqual(
|
expect(filter.availablePaths.value.length).toBeGreaterThanOrEqual(
|
||||||
initialPathCount
|
initialPathCount
|
||||||
);
|
);
|
||||||
@@ -536,9 +540,7 @@ describe('useJsonFilter', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should handle arrays in data without recursing', () => {
|
it('should handle arrays in data without recursing', () => {
|
||||||
const dataWithArrays = [
|
const dataWithArrays = [{ id: 1, tags: ['a', 'b', 'c'], name: 'test' }];
|
||||||
{ id: 1, tags: ['a', 'b', 'c'], name: 'test' }
|
|
||||||
];
|
|
||||||
|
|
||||||
filter.initializeFilter(dataWithArrays);
|
filter.initializeFilter(dataWithArrays);
|
||||||
const paths = filter.availablePaths.value.map(p => p.path);
|
const paths = filter.availablePaths.value.map(p => p.path);
|
||||||
@@ -548,9 +550,7 @@ describe('useJsonFilter', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should handle numeric keys in objects', () => {
|
it('should handle numeric keys in objects', () => {
|
||||||
const dataWithNumericKeys = [
|
const dataWithNumericKeys = [{ 1: 'numeric', 2: 'keys', name: 'test' }];
|
||||||
{ '1': 'numeric', '2': 'keys', name: 'test' }
|
|
||||||
];
|
|
||||||
|
|
||||||
filter.initializeFilter(dataWithNumericKeys);
|
filter.initializeFilter(dataWithNumericKeys);
|
||||||
const paths = filter.availablePaths.value.map(p => p.path);
|
const paths = filter.availablePaths.value.map(p => p.path);
|
||||||
@@ -562,7 +562,7 @@ describe('useJsonFilter', () => {
|
|||||||
|
|
||||||
it('should handle unicode characters in paths', () => {
|
it('should handle unicode characters in paths', () => {
|
||||||
const unicodeData = [
|
const unicodeData = [
|
||||||
{ '名前': 'Japanese', 'nome': 'Italian', name: 'English' }
|
{ 名前: 'Japanese', nome: 'Italian', name: 'English' }
|
||||||
];
|
];
|
||||||
|
|
||||||
filter.initializeFilter(unicodeData);
|
filter.initializeFilter(unicodeData);
|
||||||
|
|||||||
Reference in New Issue
Block a user