I’m attempting to write my first jQuery plugin to query multi-dimensional arrays of complex objects. It kind of functions how I want it, but right now it receives a property name and value as input for comparison of the items. I want to modify it so that it can receive jQuery’s selector syntax as input in order to filter my objects in a more encompassing manner.
I want to be able to provide syntax the same as or similar to jquery’s ‘native’ selector syntax:
"string"
"number"
"boolean"
"object"
"string,number,boolean"
"object[FirstName='Ben'][LastName='Alabaster']"
"object[LastName^='Ala']"
"object[LastName$='er']"
etc.
Are there any tutorials or plugins that demonstrate this ability to parse selector syntax for comparing against objects?
The selector syntax that jQuery uses was abstracted into the Sizzle library. you can view the source to Sizzle on GitHub.
Sizzle is pretty clearly customized to query against a document’s Document Object Model, so you’d be doing modifications to get it to query against other kinds of data. Have a look at lines 294 to 303:
And here’s code that appears to parse attribute selectors like
^=and~=, lines 661 to 691:The way these regular expressions and logic are used may give you ideas as to how to implement your data structure queries.