about summary refs log tree commit diff stats
path: root/js/where/where.js
blob: cef80f36d8a20290c076459aa1c29eaf0dd29ec0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
 * 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) => {
    // null/undefined inputs
    if (!collection || !properties) {
        return [];
    }

    // Cache property paths and their split versions for performance
    const propertyPaths = Object.entries(properties).map(([key, value]) => ({
        path: key,
        parts: key.split('.'),
        value
    }));

    // An optimized nested value getter with memoization
    const getNestedValue = (() => {
        const cache = new WeakMap();
        
        return (obj, parts) => {
            if (!obj) return undefined;
            
            let cached = cache.get(obj);
            if (!cached) {
                cached = new Map();
                cache.set(obj, cached);
            }
            
            const pathKey = parts.join('.');
            if (cached.has(pathKey)) {
                return cached.get(pathKey);
            }

            const value = parts.reduce((current, key) => 
                current && current[key] !== undefined ? current[key] : undefined, 
                obj
            );
            
            cached.set(pathKey, value);
            return value;
        };
    })();

    // An optimized equality comparison
    const isEqual = (value1, value2) => {
        // 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;

        // Array comparison
        if (Array.isArray(value1) && Array.isArray(value2)) {
            return value1.length === value2.length &&
                   value1.every((val, idx) => isEqual(val, value2[idx]));
        }

        // Object comparison
        if (typeof value2 === 'object') {
            return Object.entries(value2).every(([key, val]) => 
                value1 && isEqual(value1[key], val)
            );
        }

        return false;
    };

    // Property matcher
    const matchesProperties = item => 
        propertyPaths.every(({ parts, value }) => 
            isEqual(getNestedValue(item, parts), value)
        );

    // Input validation
    if (!Array.isArray(collection) || typeof properties !== 'object') {
        return [];
    }

    return collection.filter(matchesProperties);
};

// Example usage with nested structures:
/*
const data = [
    { 
        name: 'John',
        age: 30,
        preferences: { 
            theme: 'dark',
            notifications: { email: true, sms: false }
        }
    },
    { 
        name: 'Jane',
        age: 25,
        preferences: { 
            theme: 'light',
            notifications: { email: false, sms: true }
        }
    }
];

// Find users with specific preferences
const darkThemeUsers = where(data, { 
    preferences: { theme: 'dark' } 
});

// Find users with specific notification settings
const emailSubscribers = where(data, { 
    preferences: { notifications: { email: true } } 
});
*/

if (typeof module !== 'undefined' && module.exports) {
    module.exports = where;
}