In ClojureScript, to check if a map contains a specific key, you can use the contains? function. However, since your goal is to check if the :on-click function contains the keyword :navigate-to-page, you will need a different approach because :on-click is associated with an anonymous function (a function literal), making direct inspection not straightforward.
Since Clojure and ClojureScript do not offer built-in reflection capabilities to inspect the source code of a function at runtime, you cannot directly inspect the body of the anonymous function for the presence of :navigate-to-page. However, if your requirement is to ensure that the action for navigating to a page is correctly set for certain elements, consider a few alternative approaches that could achieve a similar outcome:
1. Structured Metadata
Instead of embedding the logic within an anonymous function and trying to inspect it, consider structuring your data to carry metadata or specific flags that indicate the desired behavior. For example:
{:action :invoices
:label "Invoices"
:navigate? true
:navigate-to {:page :invoice.job/page
:params {:id job-id}}
:on-click #(dispatch [:navigate-to-page :invoice.job/page {:id job-id}])}
Here, :navigate? is a straightforward flag that you add to items that should trigger navigation, and :navigate-to clearly specifies the target. This approach not only makes it easy to check for the presence of navigation intent but also keeps the target navigation details readily available without inspecting the function.
2. Structured Function Invocation
If you prefer or need to encode behaviors within functions, consider using a more structured approach for function invocation. For example:
(defn navigate-to-page [target params]
#(dispatch [:navigate-to-page target params]))
{:action :invoices
:label "Invoices"
:on-click (navigate-to-page :invoice.job/page {:id job-id})}
In this structure, you create a higher-order function (navigate-to-page) that returns your anonymous function. While you still can't easily inspect the returned function to see if it contains :navigate-to-page, this structure allows you to control navigation more centrally and clearly indicates where navigation is intended.
3. Code Comments or Documentation
Sometimes, a simple and practical approach can be to document the intents and behaviors using comments effectively. This does not provide a programmatic way to check for the presence of :navigate-to-page but can help maintain clarity in the codebase.
Conclusion
Since inspecting the body of an anonymous function for specific keywords isn't feasible in ClojureScript, adapting your approach to structuring your data or functions might provide a practical solution. By making the intent and behavior more explicit in your data structures or through higher-level abstractions, you maintain clarity and control without needing to introspect function bodies.