Snippetify is a script that is designed to make StackSnippets with JS submissions on the main site easy.
Examples
Let's say we are answering a simple challenge to repeat a string N times. A possible JS submission might be:
a=>b=>a.repeat(b)
that is called with the first parameter the string to repeat and the second param (uses currying) is the number of times to repeat that.
Let's say we want that in a stack snippet. Here's the old-fashioned way:
var repeat =
//actual code
a=>b=>a.repeat(b);
document.getElementById("submit").onclick = function(){
document.getElementById("output").innerText = repeat(document.getElementById("string").value)(document.getElementById("repeat-count").value);
};
input,button{
display: block;
}
<input id = "string">
<input type = "number" id = "repeat-count">
<button id = "submit">Submit</button>
<div id = "output"></div>
That's alot of code for just a=>b=>a.repeat(b)
. If you use Snippetify, you can do this:
Snippetify(a=>b=>a.repeat(b));
<script src="https://programmer5000.com/snippetify.min.js"></script>
<input type = "text" data-attribute = "0">
<input type = "number" data-attribute = "1">
<button>Submit</button>
<div data-output></div>
Better, huh! If you pass Snippetify function that accepts 1 argument (all currying functions) and you have more than one input it will know to use currying. But it gets better:
Snippetify(a=>b=>a.repeat(b));
<script src="https://programmer5000.com/snippetify.min.js"></script>
<input type = "text" data-attribute>
<input type = "number" data-attribute>
<button>Submit</button>
<div data-output></div>
If you want your parameters passed in order of the <input>
s provided, you can just use data-attribute
with no value. But it gets even better:
Snippetify(a=>b=>a.repeat(b));
<script src="https://programmer5000.com/snippetify.min.js"></script>
<input type = "text">
<input type = "number">
<button>Submit</button>
<div data-output></div>
Snippetify will use all your <input>
s (and <textarea>
s) if you don't use data-attribute
. Still too much work? Try this:
Snippetify(a=>b=>a.repeat(b));
<script src="https://programmer5000.com/snippetify.min.js"></script>
<input type = "text">
<input type = "number">
<button>Submit</button>
<div></div>
You don't need data-output
if you would like output to go to the first <div>
in the DOM. One final laziness addition (this works only with non-currying functions and text-only inputs):
Snippetify((a,b)=>a + b);
<script src="https://programmer5000.com/snippetify.min.js"></script>
Yep, Snippetify will even create everything for you if it can.
Usage
When you add a code snippet, add this to your HTML (before anything else)
<script src="https://programmer5000.com/snippetify.min.js"></script>
See above for markup needed.
What this post is for
Have any bugs feature requests, or general feedback? Comment or answer below.
Snippetify License
CC0. You can use it in any answer to a code golf challenge.
Snippetify
with some code counts as "linking".) (See under "disclose source" here.) \$\endgroup\$<pre>
would generally be more useful, but especially for ascii-art. \$\endgroup\$submit
and encapsulate it in aform
for semantics and accessibility. \$\endgroup\$<form>
s are blocked in Stack Snippets in browsers that support frame options. \$\endgroup\$