about summary refs log blame commit diff stats
path: root/js/where/where.test.js
blob: e674ca04c1dd3b337c500387681b8321c6948e22 (plain) (tree)



















                                                                                            









































                                                      




























































                                                                                           




                                                      
const test = {
    run(name, fn) {
        try {
            fn();
            console.log(`✅ ${name}`);
        } catch (error) {
            console.error(`❌ ${name}`);
            console.error(error);
        }
    },

    assertEquals(actual, expected, message = '') {
        const actualStr = JSON.stringify(actual);
        const expectedStr = JSON.stringify(expected);
        if (actualStr !== expectedStr) {
            throw new Error(`${message}\nExpected: ${expectedStr}\nReceived: ${actualStr}`);
        }
    }
};

const testData = [
    {
        name: 'John',
        age: 30,
        hobbies: ['reading', 'music'],
        address: {
            city: 'New York',
            country: 'USA'
        },
        preferences: {
            theme: 'dark',
            notifications: { email: true, sms: false }
        }
    },
    {
        name: 'Jane',
        age: 25,
        hobbies: ['sports'],
        address: {
            city: 'London',
            country: 'UK'
        },
        preferences: {
            theme: 'light',
            notifications: { email: false, sms: true }
        }
    },
    {
        name: 'Bob',
        age: 30,
        hobbies: ['reading', 'music'],
        address: {
            city: 'New York',
            country: 'USA'
        },
        preferences: {
            theme: 'dark',
            notifications: { email: true, sms: true }
        }
    }
];

test.run('should handle empty inputs', () => {
    test.assertEquals(where([], {}), [], 'Empty array and empty object');
    test.assertEquals(where(null, {}), [], 'Null collection');
    test.assertEquals(where(undefined, {}), [], 'Undefined collection');
    test.assertEquals(where(testData, null), [], 'Null properties');
});

test.run('should match simple properties', () => {
    const result = where(testData, { age: 30 });
    test.assertEquals(result.length, 2, 'Should find two 30-year-olds');
    test.assertEquals(
        result.map(p => p.name),
        ['John', 'Bob'],
        'Should find John and Bob'
    );
});

test.run('should match nested objects', () => {
    const result = where(testData, {
        address: { city: 'New York', country: 'USA' }
    });
    test.assertEquals(result.length, 2, 'Should find two New Yorkers');
});

test.run('should match arrays exactly', () => {
    const result = where(testData, {
        hobbies: ['reading', 'music']
    });
    test.assertEquals(result.length, 2, 'Should find two people with exact hobbies match');
});

test.run('should match deeply nested properties', () => {
    const result = where(testData, {
        preferences: { notifications: { email: true } }
    });
    test.assertEquals(result.length, 2, 'Should find two people with email notifications');
});

test.run('should handle multiple criteria', () => {
    const result = where(testData, {
        age: 30,
        'preferences.theme': 'dark',
        'address.city': 'New York'
    });
    test.assertEquals(result.length, 2, 'Should find people matching all criteria');
});

test.run('should return empty array for non-matching criteria', () => {
    const result = where(testData, {
        age: 99
    });
    test.assertEquals(result, [], 'Should return empty array for non-matching criteria');
});

test.run('should handle null values correctly', () => {
    const dataWithNull = [...testData, { name: 'Alice', age: null }];
    const result = where(dataWithNull, { age: null });
    test.assertEquals(result.length, 1, 'Should find one person with null age');
    test.assertEquals(result[0].name, 'Alice', 'Should find Alice');
});

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

process.stdout.setEncoding('utf8'); 
8"> </head><body bgcolor="#f0f0f8"> <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading"> <tr bgcolor="#7799ee"> <td valign=bottom>&nbsp;<br> <font color="#ffffff" face="helvetica, arial">&nbsp;<br><big><big><strong><a href="ranger.html"><font color="#ffffff">ranger</font></a>.actions</strong></big></big></font></td ><td align=right valign=bottom ><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:/home/hut/work/ranger/ranger/actions.py">/home/hut/work/ranger/ranger/actions.py</a></font></td></tr></table> <p><tt>#&nbsp;Copyright&nbsp;(c)&nbsp;2009,&nbsp;2010&nbsp;hut&nbsp;&lt;hut@lavabit.com&gt;<br> #<br> #&nbsp;Permission&nbsp;to&nbsp;use,&nbsp;copy,&nbsp;modify,&nbsp;and/or&nbsp;distribute&nbsp;this&nbsp;software&nbsp;for&nbsp;any<br> #&nbsp;purpose&nbsp;with&nbsp;or&nbsp;without&nbsp;fee&nbsp;is&nbsp;hereby&nbsp;granted,&nbsp;provided&nbsp;that&nbsp;the&nbsp;above<br> #&nbsp;copyright&nbsp;notice&nbsp;and&nbsp;this&nbsp;permission&nbsp;notice&nbsp;appear&nbsp;in&nbsp;all&nbsp;copies.<br> #<br> #&nbsp;THE&nbsp;SOFTWARE&nbsp;IS&nbsp;PROVIDED&nbsp;"AS&nbsp;IS"&nbsp;AND&nbsp;THE&nbsp;AUTHOR&nbsp;DISCLAIMS&nbsp;ALL&nbsp;WARRANTIES<br> #&nbsp;WITH&nbsp;REGARD&nbsp;TO&nbsp;THIS&nbsp;SOFTWARE&nbsp;INCLUDING&nbsp;ALL&nbsp;IMPLIED&nbsp;WARRANTIES&nbsp;OF<br> #&nbsp;MERCHANTABILITY&nbsp;AND&nbsp;FITNESS.&nbsp;IN&nbsp;NO&nbsp;EVENT&nbsp;SHALL&nbsp;THE&nbsp;AUTHOR&nbsp;BE&nbsp;LIABLE&nbsp;FOR<br> #&nbsp;ANY&nbsp;SPECIAL,&nbsp;DIRECT,&nbsp;INDIRECT,&nbsp;OR&nbsp;CONSEQUENTIAL&nbsp;DAMAGES&nbsp;OR&nbsp;ANY&nbsp;DAMAGES<br> #&nbsp;WHATSOEVER&nbsp;RESULTING&nbsp;FROM&nbsp;LOSS&nbsp;OF&nbsp;USE,&nbsp;DATA&nbsp;OR&nbsp;PROFITS,&nbsp;WHETHER&nbsp;IN&nbsp;AN<br> #&nbsp;ACTION&nbsp;OF&nbsp;CONTRACT,&nbsp;NEGLIGENCE&nbsp;OR&nbsp;OTHER&nbsp;TORTIOUS&nbsp;ACTION,&nbsp;ARISING&nbsp;OUT&nbsp;OF<br> #&nbsp;OR&nbsp;IN&nbsp;CONNECTION&nbsp;WITH&nbsp;THE&nbsp;USE&nbsp;OR&nbsp;PERFORMANCE&nbsp;OF&nbsp;THIS&nbsp;SOFTWARE.</tt></p> <p> <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> <tr bgcolor="#aa55cc"> <td colspan=3 valign=bottom>&nbsp;<br> <font color="#ffffff" face="helvetica, arial"><big><strong>Modules</strong></big></font></td></tr> <tr><td bgcolor="#aa55cc"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td> <td width="100%"><table width="100%" summary="list"><tr><td width="25%" valign=top><a href="ranger.gui.widgets.console_mode.html">ranger.gui.widgets.console_mode</a><br> </td><td width="25%" valign=top><a href="ranger.fsobject.html">ranger.fsobject</a><br> </td><td width="25%" valign=top><a href="os.html">os</a><br> </td><td width="25%" valign=top><a href="shutil.html">shutil</a><br> </td></tr></table></td></tr></table><p> <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> <tr bgcolor="#ee77aa"> <td colspan=3 valign=bottom>&nbsp;<br> <font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr> <tr><td bgcolor="#ee77aa"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td> <td width="100%"><dl> <dt><font face="helvetica, arial"><a href="ranger.shared.html#EnvironmentAware">ranger.shared.EnvironmentAware</a>(<a href="ranger.shared.html#Awareness">ranger.shared.Awareness</a>) </font></dt><dd> <dl> <dt><font face="helvetica, arial"><a href="ranger.actions.html#Actions">Actions</a>(<a href="ranger.shared.html#EnvironmentAware">ranger.shared.EnvironmentAware</a>, <a href="ranger.shared.settings.html#SettingsAware">ranger.shared.settings.SettingsAware</a>) </font></dt></dl> </dd> <dt><font face="helvetica, arial"><a href="ranger.shared.settings.html#SettingsAware">ranger.shared.settings.SettingsAware</a>(<a href="builtins.html#object">builtins.object</a>) </font></dt><dd> <dl> <dt><font face="helvetica, arial"><a href="ranger.actions.html#Actions">Actions</a>(<a href="ranger.shared.html#EnvironmentAware">ranger.shared.EnvironmentAware</a>, <a href="ranger.shared.settings.html#SettingsAware">ranger.shared.settings.SettingsAware</a>) </font></dt></dl> </dd> </dl> <p> <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> <tr bgcolor="#ffc8d8"> <td colspan=3 valign=bottom>&nbsp;<br> <font color="#000000" face="helvetica, arial"><a name="Actions">class <strong>Actions</strong></a>(<a href="ranger.shared.html#EnvironmentAware">ranger.shared.EnvironmentAware</a>, <a href="ranger.shared.settings.html#SettingsAware">ranger.shared.settings.SettingsAware</a>)</font></td></tr> <tr><td bgcolor="#ffc8d8"><tt>&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td> <td width="100%"><dl><dt>Method resolution order:</dt> <dd><a href="ranger.actions.html#Actions">Actions</a></dd> <dd><a href="ranger.shared.html#EnvironmentAware">ranger.shared.EnvironmentAware</a></dd> <dd><a href="ranger.shared.html#Awareness">ranger.shared.Awareness</a></dd> <dd><a href="ranger.shared.settings.html#SettingsAware">ranger.shared.settings.SettingsAware</a></dd> <dd><a href="builtins.html#object">builtins.object</a></dd> </dl> <hr> Methods defined here:<br> <dl><dt><a name="Actions-cd"><strong>cd</strong></a> = <a href="#Actions-enter_dir">enter_dir</a>(self, path)</dt></dl> <dl><dt><a name="Actions-copy"><strong>copy</strong></a>(self)</dt><dd><tt>Copy&nbsp;the&nbsp;selected&nbsp;items</tt></dd></dl> <dl><dt><a name="Actions-cut"><strong>cut</strong></a>(self)</dt></dl> <dl><dt><a name="Actions-delete"><strong>delete</strong></a>(self)</dt></dl> <dl><dt><a name="Actions-display_command_help"><strong>display_command_help</strong></a>(self, console_widget)</dt></dl> <dl><dt><a name="Actions-display_file"><strong>display_file</strong></a>(self)</dt></dl> <dl><dt><a name="Actions-display_help"><strong>display_help</strong></a>(self, topic<font color="#909090">='index'</font>, narg<font color="#909090">=None</font>)</dt></dl> <dl><dt><a name="Actions-display_log"><strong>display_log</strong></a>(self)</dt></dl> <dl><dt><a name="Actions-edit_file"><strong>edit_file</strong></a>(self)</dt><dd><tt>Calls&nbsp;execute_file&nbsp;with&nbsp;the&nbsp;current&nbsp;file&nbsp;and&nbsp;app='editor'</tt></dd></dl> <dl><dt><a name="Actions-enter_bookmark"><strong>enter_bookmark</strong></a>(self, key)</dt><dd><tt>Enter&nbsp;the&nbsp;bookmark&nbsp;with&nbsp;the&nbsp;name&nbsp;&lt;key&gt;</tt></dd></dl> <dl><dt><a name="Actions-enter_dir"><strong>enter_dir</strong></a>(self, path)</dt><dd><tt>Enter&nbsp;the&nbsp;directory&nbsp;at&nbsp;the&nbsp;given&nbsp;path</tt></dd></dl> <dl><dt><a name="Actions-execute_command"><strong>execute_command</strong></a>(self, cmd, **kw)</dt></dl> <dl><dt><a name="Actions-execute_file"><strong>execute_file</strong></a>(self, files, **kw)</dt><dd><tt>Execute&nbsp;a&nbsp;file.<br> app&nbsp;is&nbsp;the&nbsp;name&nbsp;of&nbsp;a&nbsp;method&nbsp;in&nbsp;Applications,&nbsp;without&nbsp;the&nbsp;"app_"<br> flags&nbsp;is&nbsp;a&nbsp;string&nbsp;consisting&nbsp;of&nbsp;applications.ALLOWED_FLAGS<br> mode&nbsp;is&nbsp;a&nbsp;positive&nbsp;integer.<br> Both&nbsp;flags&nbsp;and&nbsp;mode&nbsp;specify&nbsp;how&nbsp;the&nbsp;program&nbsp;is&nbsp;run.</tt></dd></dl> <dl><dt><a name="Actions-exit"><strong>exit</strong></a>(self)</dt><dd><tt>Exit&nbsp;the&nbsp;program</tt></dd></dl> <dl><dt><a name="Actions-force_load_preview"><strong>force_load_preview</strong></a>(self)</dt></dl> <dl><dt><a name="Actions-handle_mouse"><strong>handle_mouse</strong></a>(self)</dt><dd><tt>Handle&nbsp;mouse-buttons&nbsp;if&nbsp;one&nbsp;was&nbsp;pressed</tt></dd></dl> <dl><dt><a name="Actions-history_go"><strong>history_go</strong></a>(self, relative)</dt><dd><tt>Move&nbsp;back&nbsp;and&nbsp;forth&nbsp;in&nbsp;the&nbsp;history</tt></dd></dl> <dl><dt><a name="Actions-mark"><strong>mark</strong></a>(self, all<font color="#909090">=False</font>, toggle<font color="#909090">=False</font>, val<font color="#909090">=None</font>, movedown<font color="#909090">=None</font>)</dt><dd><tt>A&nbsp;wrapper&nbsp;for&nbsp;the&nbsp;directory.mark_xyz&nbsp;functions.<br> &nbsp;<br> Arguments:<br> all&nbsp;-&nbsp;change&nbsp;all&nbsp;files&nbsp;of&nbsp;the&nbsp;current&nbsp;directory&nbsp;at&nbsp;once?<br> toggle&nbsp;-&nbsp;toggle&nbsp;the&nbsp;marked-status?<br> val&nbsp;-&nbsp;mark&nbsp;or&nbsp;unmark?</tt></dd></dl> <dl><dt><a name="Actions-mkdir"><strong>mkdir</strong></a>(self, name)</dt></dl> <dl><dt><a name="Actions-move_left"><strong>move_left</strong></a>(self, narg<font color="#909090">=1</font>)</dt><dd><tt>Enter&nbsp;the&nbsp;parent&nbsp;directory</tt></dd></dl> <dl><dt><a name="Actions-move_pointer"><strong>move_pointer</strong></a>(self, relative<font color="#909090">=0</font>, absolute<font color="#909090">=None</font>, narg<font color="#909090">=None</font>)</dt><dd><tt>Move&nbsp;the&nbsp;pointer&nbsp;down&nbsp;by&nbsp;&lt;relative&gt;&nbsp;or&nbsp;to&nbsp;&lt;absolute&gt;</tt></dd></dl> <dl><dt><a name="Actions-move_pointer_by_pages"><strong>move_pointer_by_pages</strong></a>(self, relative)</dt><dd><tt>Move&nbsp;the&nbsp;pointer&nbsp;down&nbsp;by&nbsp;&lt;relative&gt;&nbsp;pages</tt></dd></dl> <dl><dt><a name="Actions-move_pointer_by_percentage"><strong>move_pointer_by_percentage</strong></a>(self, relative<font color="#909090">=0</font>, absolute<font color="#909090">=None</font>, narg<font color="#909090">=None</font>)</dt><dd><tt>Move&nbsp;the&nbsp;pointer&nbsp;down&nbsp;by&nbsp;&lt;relative&gt;%&nbsp;or&nbsp;to&nbsp;&lt;absolute&gt;%</tt></dd></dl> <dl><dt><a name="Actions-move_right"><strong>move_right</strong></a>(self, mode<font color="#909090">=0</font>)</dt><dd><tt>Enter&nbsp;the&nbsp;current&nbsp;directory&nbsp;or&nbsp;execute&nbsp;the&nbsp;current&nbsp;file</tt></dd></dl> <dl><dt><a name="Actions-notify"><strong>notify</strong></a>(self, text, duration<font color="#909090">=4</font>, bad<font color="#909090">=False</font>)</dt></dl> <dl><dt><a name="Actions-open_console"><strong>open_console</strong></a>(self, mode<font color="#909090">=':'</font>, string<font color="#909090">=''</font>)</dt><dd><tt>Open&nbsp;the&nbsp;console&nbsp;if&nbsp;the&nbsp;current&nbsp;UI&nbsp;supports&nbsp;that</tt></dd></dl> <dl><dt><a name="Actions-paste"><strong>paste</strong></a>(self)</dt><dd><tt>Paste&nbsp;the&nbsp;selected&nbsp;items&nbsp;into&nbsp;the&nbsp;current&nbsp;directory</tt></dd></dl> <dl><dt><a name="Actions-paste_symlink"><strong>paste_symlink</strong></a>(self)</dt></dl> <dl><dt><a name="Actions-redraw_window"><strong>redraw_window</strong></a>(self)</dt><dd><tt>Redraw&nbsp;the&nbsp;window</tt></dd></dl> <dl><dt><a name="Actions-reload_cwd"><strong>reload_cwd</strong></a>(self)</dt></dl> <dl><dt><a name="Actions-rename"><strong>rename</strong></a>(self, src, dest)</dt></dl> <dl><dt><a name="Actions-reset"><strong>reset</strong></a>(self)</dt><dd><tt>Reset&nbsp;the&nbsp;filemanager,&nbsp;clearing&nbsp;the&nbsp;directory&nbsp;buffer</tt></dd></dl> <dl><dt><a name="Actions-resize"><strong>resize</strong></a>(self)</dt><dd><tt>Update&nbsp;the&nbsp;size&nbsp;of&nbsp;the&nbsp;UI</tt></dd></dl> <dl><dt><a name="Actions-scroll"><strong>scroll</strong></a>(self, relative)</dt><dd><tt>Scroll&nbsp;down&nbsp;by&nbsp;&lt;relative&gt;&nbsp;lines</tt></dd></dl> <dl><dt><a name="Actions-search"><strong>search</strong></a>(self, order<font color="#909090">=None</font>, forward<font color="#909090">=True</font>)</dt></dl> <dl><dt><a name="Actions-set_bookmark"><strong>set_bookmark</strong></a>(self, key)</dt><dd><tt>Set&nbsp;the&nbsp;bookmark&nbsp;with&nbsp;the&nbsp;name&nbsp;&lt;key&gt;&nbsp;to&nbsp;the&nbsp;current&nbsp;directory</tt></dd></dl> <dl><dt><a name="Actions-set_filter"><strong>set_filter</strong></a>(self, fltr)</dt></dl> <dl><dt><a name="Actions-set_search_method"><strong>set_search_method</strong></a>(self, order, forward<font color="#909090">=True</font>)</dt></dl> <dl><dt><a name="Actions-sort"><strong>sort</strong></a>(self, func<font color="#909090">=None</font>, reverse<font color="#909090">=None</font>)</dt></dl> <dl><dt><a name="Actions-tag_remove"><strong>tag_remove</strong></a>(self, movedown<font color="#909090">=None</font>)</dt></dl> <dl><dt><a name="Actions-tag_toggle"><strong>tag_toggle</strong></a>(self, movedown<font color="#909090">=None</font>)</dt></dl> <dl><dt><a name="Actions-toggle_boolean_option"><strong>toggle_boolean_option</strong></a>(self, string)</dt><dd><tt>Toggle&nbsp;a&nbsp;boolean&nbsp;option&nbsp;named&nbsp;&lt;string&gt;</tt></dd></dl> <dl><dt><a name="Actions-unset_bookmark"><strong>unset_bookmark</strong></a>(self, key)</dt><dd><tt>Delete&nbsp;the&nbsp;bookmark&nbsp;with&nbsp;the&nbsp;name&nbsp;&lt;key&gt;</tt></dd></dl> <hr> Data and other attributes defined here:<br> <dl><dt><strong>search_forward</strong> = False</dl> <dl><dt><strong>search_method</strong> = 'ctime'</dl> <hr> Data and other attributes inherited from <a href="ranger.shared.html#EnvironmentAware">ranger.shared.EnvironmentAware</a>:<br> <dl><dt><strong>env</strong> = None</dl> <hr> Data descriptors inherited from <a href="ranger.shared.html#Awareness">ranger.shared.Awareness</a>:<br> <dl><dt><strong>__dict__</strong></dt> <dd><tt>dictionary&nbsp;for&nbsp;instance&nbsp;variables&nbsp;(if&nbsp;defined)</tt></dd> </dl> <dl><dt><strong>__weakref__</strong></dt> <dd><tt>list&nbsp;of&nbsp;weak&nbsp;references&nbsp;to&nbsp;the&nbsp;object&nbsp;(if&nbsp;defined)</tt></dd> </dl> <hr> Data and other attributes inherited from <a href="ranger.shared.settings.html#SettingsAware">ranger.shared.settings.SettingsAware</a>:<br> <dl><dt><strong>settings</strong> = &lt;ranger.ext.openstruct.OpenStruct object at 0x7f6a9067fbd0&gt;</dl> </td></tr></table></td></tr></table> </body></html>