{"id":13878580,"url":"https://github.com/styd/functionable-json","last_synced_at":"2025-10-28T02:02:17.946Z","repository":{"id":56847855,"uuid":"205432577","full_name":"styd/functionable-json","owner":"styd","description":"Generate JSON with JavaScript functions from ruby hash","archived":false,"fork":false,"pushed_at":"2020-04-17T13:28:09.000Z","size":11,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-13T05:51:34.611Z","etag":null,"topics":["function","javascript","json","json-generator","ruby"],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/styd.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-08-30T17:59:44.000Z","updated_at":"2022-04-28T13:02:33.000Z","dependencies_parsed_at":"2022-09-09T06:23:59.905Z","dependency_job_id":null,"html_url":"https://github.com/styd/functionable-json","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/styd%2Ffunctionable-json","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/styd%2Ffunctionable-json/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/styd%2Ffunctionable-json/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/styd%2Ffunctionable-json/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/styd","download_url":"https://codeload.github.com/styd/functionable-json/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252931480,"owners_count":21827109,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["function","javascript","json","json-generator","ruby"],"created_at":"2024-08-06T08:01:53.760Z","updated_at":"2025-10-28T02:02:12.905Z","avatar_url":"https://github.com/styd.png","language":"Ruby","readme":"[![Gem Version](https://img.shields.io/gem/v/functionable-json.svg?label=functionable-json)](https://rubygems.org/gems/functionable-json)\n[![Build Status](https://travis-ci.org/styd/functionable-json.svg?branch=master)](https://travis-ci.org/styd/functionable-json)\n\nAccording to [RFC8259](https://tools.ietf.org/html/rfc8259):\n\n\u003e JSON can represent four primitive types (`strings`, `numbers`, `booleans`,\n\u003e and `null`) and two structured types (`objects` and `arrays`).\n\nNow, with this gem, you can have the 7th one, `function`.\n\nSo, basically, what I'm saying is the json is no longer conform with the\n**RFC8259**. The resulting function is not guaranteed to be executable in \nJavaScript because it doesn't check what's inside the function. It also cannot\nbe parsed back into hash. So, use it at your own risk!\n\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'functionable-json'\n```\n\nAnd then execute:\n\n    $ bundle\n\n\n## Usage\n\n```ruby\n{\n  amount: function(val) {\n           return '$' + parseFloat(val).toLocaleString();\n         }\n}.to_json\n#=\u003e \"{\\\"amount\\\":function(val) { return '$' + parseFloat(val).toLocaleString(); }}\"\n```\n\nBut, don't go wild just yet because there are limitations:\n- You cannot use `if` keyword because the way JavaScript uses it is different from\n  ruby. Use ternary operators instead.\n- Also, don't put multiple functions on the same line. You'll get unexpected result\n  if you do.\n- You should end every statement with `;`.\n\nIf you try the above example on your REPL (e.g. `irb` or `pry`) it won't\nwork since the gem will try to find the file that stores the code. So, you\nneed to put it on a file and run it.\n\n### How can a JavaScript function be called from Ruby?\n\nWell, technically, it's not a JavaScript function. It's a Ruby method named\n`function` that's available in any object. In JavaScript, a form like that is\na _function declaration_, but in Ruby it's a _method invocation_. So, in that case,\n`val` needs to already exist before you call it, and it is. Both `function` and\n`val` are already methods of an object. If you want to use arguments with\nanother name, you need to declare them as variables first.\n\n```ruby\narg1, arg2 = nil\n{\n  equality: function(arg1, arg2) {\n              return arg1 + ' == ' + arg2\n            }\n}.to_json\n#=\u003e \"{\\\"equality\\\":function(arg1, arg2) { return arg1 + ' == ' + arg2 }}\"\n```\n\n### Use Case\n\nOne example use case is [ApexCharts.RB](https://github.com/styd/apexcharts.rb)\nformatter or other charting libraries'. You can add tooltip formatter like this:\n\n```erb\n\u003c%= line_chart data, tooltip: {y: {formatter: function(val) { return '$' + parseFloat(val).toLocaleString(); }}} %\u003e\n\n```\n\n\n## License\n\nThe gem is available as open source under the terms of the\n[MIT License](https://opensource.org/licenses/MIT).\n","funding_links":[],"categories":["Ruby"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstyd%2Ffunctionable-json","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstyd%2Ffunctionable-json","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstyd%2Ffunctionable-json/lists"}