diff options
-rw-r--r-- | js/where/where.js | 17 | ||||
-rw-r--r-- | js/where/where.test.js | 6 |
2 files changed, 8 insertions, 15 deletions
diff --git a/js/where/where.js b/js/where/where.js index 9ff6545..cef80f3 100644 --- a/js/where/where.js +++ b/js/where/where.js @@ -1,11 +1,11 @@ /** - * A functional implementation of a query filter similar to SQL's WHERE clause + * A mostly functional query filter similar to SQL's WHERE * @param {Array} collection - Array of objects to filter * @param {Object} properties - Object containing properties to match against * @returns {Array} - Filtered array of objects */ const where = (collection, properties) => { - // Handle null/undefined inputs + // null/undefined inputs if (!collection || !properties) { return []; } @@ -17,7 +17,7 @@ const where = (collection, properties) => { value })); - // Optimized nested value getter with memoization + // An optimized nested value getter with memoization const getNestedValue = (() => { const cache = new WeakMap(); @@ -45,20 +45,20 @@ const where = (collection, properties) => { }; })(); - // Optimized equality comparison + // An optimized equality comparison const isEqual = (value1, value2) => { - // Handle null/undefined cases first for early return + // Handle null/undefined cases first for faster returns if (value2 === undefined) return true; if (value1 === value2) return true; if (value1 === null || value2 === null) return false; - // Handle array comparison + // Array comparison if (Array.isArray(value1) && Array.isArray(value2)) { return value1.length === value2.length && value1.every((val, idx) => isEqual(val, value2[idx])); } - // Handle object comparison + // Object comparison if (typeof value2 === 'object') { return Object.entries(value2).every(([key, val]) => value1 && isEqual(value1[key], val) @@ -68,7 +68,7 @@ const where = (collection, properties) => { return false; }; - // Optimized property matcher + // Property matcher const matchesProperties = item => propertyPaths.every(({ parts, value }) => isEqual(getNestedValue(item, parts), value) @@ -114,7 +114,6 @@ const emailSubscribers = where(data, { }); */ -// Export for module usage if (typeof module !== 'undefined' && module.exports) { module.exports = where; } diff --git a/js/where/where.test.js b/js/where/where.test.js index 43638d9..e674ca0 100644 --- a/js/where/where.test.js +++ b/js/where/where.test.js @@ -1,6 +1,3 @@ -/** - * Simple test runner for the where function - */ const test = { run(name, fn) { try { @@ -21,7 +18,6 @@ const test = { } }; -// Test Data const testData = [ { name: 'John', @@ -64,7 +60,6 @@ const testData = [ } ]; -// Basic Tests test.run('should handle empty inputs', () => { test.assertEquals(where([], {}), [], 'Empty array and empty object'); test.assertEquals(where(null, {}), [], 'Null collection'); @@ -126,7 +121,6 @@ test.run('should handle null values correctly', () => { test.assertEquals(result[0].name, 'Alice', 'Should find Alice'); }); -// Run in Node.js environment if (typeof module !== 'undefined' && module.exports) { module.exports = { test }; } |