Skip to content

Instantly share code, notes, and snippets.

View getify's full-sized avatar
💭
Just coding

Kyle Simpson getify

💭
Just coding
View GitHub Profile
@getify
getify / gist:3421868
Created August 22, 2012 03:01
`return` syntax errors
// i understand THAT javascript doesn't assume a `;` before `return` in
// these examples. but i don't understand WHY it can't? why didn't they
// say that the grammar for `return` always assumes the previous statement
// is complete?
// NOTE: david baron pointed out that `if(a)return;` definitely wouldn't want
// to have this assumption. good point. but the others?
// NOTE: assume all these are inside a function, so `return` is safe.
@getify
getify / gist:3667624
Last active June 25, 2022 16:26
escape all (not-already-escaped) double-quote chars in a string
// NOTE: only escapes a " if it's not already escaped
function escapeDoubleQuotes(str) {
return str.replace(/\\([\s\S])|(")/g,"\\$1$2"); // thanks @slevithan!
}
escapeDoubleQuotes(`ab`); // ab => ab (nothing escaped)
escapeDoubleQuotes(`a"b`); // a"b => a\"b
escapeDoubleQuotes(`a\\"b`); // a\"b => a\"b (nothing escaped)
escapeDoubleQuotes(`a\\\\"b`); // a\\"b => a\\\"b
escapeDoubleQuotes(`a\\\\\\"b`); // a\\\"b => a\\\"b (nothing escaped)
@getify
getify / 1.json
Created September 11, 2012 06:21
how does your templating approach handle this task?
{
"settings" : {
"foo" : "low",
"bar" : "high",
"baz" : "low"
}
}
@getify
getify / 3.html
Created September 12, 2012 14:19
how "grips" templating handles it, in response to https://gist.github.com/3696453
{$: '#settings' }
<h1>Settings</h1>
{$* $.settings | name = _.key | options['low','high'] = _.value ? "checked" }
<h2>{$= name $}</h2>
{$* options }
<input type="radio" name="{$= name $}" value="{$= _.key $}" {$= _.value $}> {$= _.key $}
{$}
{$}
{$}
@getify
getify / t1
Created September 17, 2012 01:38
Here is some text
that crosses multiple lines
and sed sucks. What is
the point of sed?
@getify
getify / gist:3735885
Created September 17, 2012 06:47
bizarre: this code gzips MUCH smaller if you DON'T minify it first
(function __3__(G) {
var partial = G.definePartial,
clone = G.cloneObj,
extend = G.extend,
cID = "3";
partial(function __settings__($, _) {
$ = clone($) || {};
_ = clone(_) || {};
var i, ret = "",
ret2, $$;
// this jquery works:
$("<script></script>").attr({src: "blah.js"}).appendTo(document.head);
// this bonzo doesn't work:
bonzo(bonzo.create("<script></script>")).attr({src: "blah.js"}).appendTo(document.head);
@getify
getify / gist:3884928
Created October 13, 2012 15:05
sublime JSON coloring for Monokai theme
<dict>
<key>name</key>
<string>JSON String</string>
<key>scope</key>
<string>meta.structure.dictionary.json string.quoted.double.json</string>
<key>settings</key>
<dict>
<key>foreground</key>
<string>#E6DB74</string>
</dict>
@getify
getify / 1.js
Created November 13, 2012 00:19
illustrating some potential issues with adding `let` to code which already has `var` used in it...
// var: poor stylistic form, but no leakage
function foo() {
bar = 3;
if (true) {
var bar = 5;
}
}
// let: will leak an auto-global
function foo() {
@getify
getify / 1.js
Created November 13, 2012 14:04
more "mental tax": interpreting code that uses `let` in many nested blocks
/*
not terribly difficult to predict which statements print what.
*/
function foo() {
var a, b, c, d;
if (true) {
if (true) {
a = 1;