{"id":153,"date":"2021-09-10T13:21:09","date_gmt":"2021-09-10T13:21:09","guid":{"rendered":"http:\/\/www.clientcyde.com\/?p=153"},"modified":"2021-09-10T13:24:46","modified_gmt":"2021-09-10T13:24:46","slug":"extract-function-arguments-and-defaults-when-using-object-destructuring","status":"publish","type":"post","link":"https:\/\/www.clientcyde.com\/?p=153","title":{"rendered":"Extract function arguments and defaults when using object destructuring"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">This was such a pain to get working, and the solution isn&#8217;t ideal but it works.  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I wanted to create an extensible library of functions which could be displayed in a UI.  So I created functions like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function myFunction({a = '1', b = 2, c = &#91;1,2], d = function(){return 'hi'}} = {}){};<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To display the function options in a UI to allow users to call them, I wanted to get all the parameters and their defaults (and not pollute the global scope).  I thought it&#8217;d be easy.  It was not.  This is my solution:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function getArguments(f) {\n  var t = {};\n  (new Function(`with(this) {\n    with(new Proxy(this,{\n      has(t, p) {return true;},\n      get(t, p) {return (p in t ? t : window)&#91;p]}\n    })) {\n      ${f.toString().match(\/^function\\s*&#91;^(]*\\(\\s*{(.*)}\\s*=\\s*{}\/ms)&#91;1]}\n    }\n  }`)).call(t);\n  return Object.entries(t).map(v =&gt; ({name: v&#91;0], value: v&#91;1]}))\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">So you can call it like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>getArguments(myFunction);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And it returns all the arguments and their default values:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><a href=\"http:\/\/www.clientcyde.com\/wp-content\/uploads\/2021\/09\/2021-09-10-15_23_30-Edit-Post-\u2039-ClientCyde-\u2014-WordPress.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.clientcyde.com\/wp-content\/uploads\/2021\/09\/2021-09-10-15_23_30-Edit-Post-\u2039-ClientCyde-\u2014-WordPress.jpg\" alt=\"\" class=\"wp-image-157\" width=\"246\" height=\"90\" srcset=\"https:\/\/www.clientcyde.com\/wp-content\/uploads\/2021\/09\/2021-09-10-15_23_30-Edit-Post-\u2039-ClientCyde-\u2014-WordPress.jpg 453w, https:\/\/www.clientcyde.com\/wp-content\/uploads\/2021\/09\/2021-09-10-15_23_30-Edit-Post-\u2039-ClientCyde-\u2014-WordPress-300x110.jpg 300w\" sizes=\"auto, (max-width: 246px) 100vw, 246px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Note: it only returns arguments which have defaults. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>This was such a pain to get working, and the solution isn&#8217;t ideal but it works. I wanted to create an extensible library of functions which could be displayed in a UI. So I created functions like this: To display the function options in a UI to allow users to call them, I wanted to &hellip; <a href=\"https:\/\/www.clientcyde.com\/?p=153\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Extract function arguments and defaults when using object destructuring<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,27],"tags":[],"class_list":["post-153","post","type-post","status-publish","format-standard","hentry","category-javascript","category-webdev"],"_links":{"self":[{"href":"https:\/\/www.clientcyde.com\/index.php?rest_route=\/wp\/v2\/posts\/153","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.clientcyde.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.clientcyde.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.clientcyde.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.clientcyde.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=153"}],"version-history":[{"count":3,"href":"https:\/\/www.clientcyde.com\/index.php?rest_route=\/wp\/v2\/posts\/153\/revisions"}],"predecessor-version":[{"id":158,"href":"https:\/\/www.clientcyde.com\/index.php?rest_route=\/wp\/v2\/posts\/153\/revisions\/158"}],"wp:attachment":[{"href":"https:\/\/www.clientcyde.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.clientcyde.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.clientcyde.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}