Skip to content

Instantly share code, notes, and snippets.

View demoive's full-sized avatar

Paulo Ávila demoive

  • Barcelona
View GitHub Profile
@demoive
demoive / slugify.js
Last active February 24, 2021 08:59
Converts a string to a "URL-safe" slug
/**
* Converts a string to a "URL-safe" slug.
* Allows for some customization with two optional parameters:
*
* @param {string} Delimiter used. If not specified, defaults to a dash "-"
* @param {array} Adds to the list of non-alphanumeric characters which
* will be converted to the delimiter. The default list includes:
* ['–', '—', '―', '~', '\\', '/', '|', '+', '\'', '‘', '’', ' ']
*/
if (!String.prototype.slugify) {
@demoive
demoive / gist:4500041
Last active December 10, 2015 22:08
Snippet - Code Injection Injects some new code to the end of a pre-existing function, essentially redefining it.
if (typeof(MYFUNC) === "function") {
eval(String(MYFUNC).replace(/\}\s*$/, ";NEWCODE;}"));
}
@demoive
demoive / gist:4500046
Last active December 10, 2015 22:08
Snippet - eval() Equivalent Equivalent to eval("CODE TO BE EVALED");
(new Function("return " + "CODE TO BE EVALED"))();
@demoive
demoive / gist:4500063
Last active December 10, 2015 22:08
Snippet - setInterval Variant
(function INTERVAL() {
// do stuff
setTimeout(INTERVAL, 5);
}());
@demoive
demoive / Preferences.sublime-settings
Last active December 10, 2015 22:08
My Preferences.sublime-settings
{
"color_scheme": "Packages/Color Scheme - Default/Twilight.tmTheme",
"font_face": "monaco",
"font_size": 12.0,
"ignored_packages":
[
"Vintage"
],
"rulers":
[
@demoive
demoive / gist:4563398
Last active December 11, 2015 06:59
CDNJS Combo Loader

CDNJS Combo Loader

A proposal for CDNJS to support combined requests allowing their resources to be served in a reduced number of HTTP connections (inspiration by YUI's combo loading service).

[UPDATE: This has been offically declined in issue 791 for CDNJS. I'm keeping the proposal in this gist for posterity].

Example

Instead of four separate requests:

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
@demoive
demoive / gist:4648979
Last active December 11, 2015 19:29
Snippet - CSS Hide text
.hide-text {
display: block;
white-space: nowrap;
text-indent: 100%;
overflow: hidden;
}
.hide {
position: absolute;
clip: rect(1px, 1px, 1px, 1px);
@demoive
demoive / console-animations.js
Last active December 14, 2015 11:08
These "micro programs" mimic animations when run in a console. They are experiments based on a personal challenge to do "something interesting" in under 150 characters.
// growing (alternating characters) line
c=0;b="";setInterval("console.log(b=b+(c++%2?'-':'|'))",100)
// same, but w/o setInterval() ∴ w/o eval()
c=0;b="";(function f(){console.log(b=b+(c++%2?'-':'|'));setTimeout(f,100)}())
// wave
i=j=0;s="";setInterval("if(!(i++%25))++j;console.log(j%2?s+='*':s=s.substr(1))",15)
// same, but w/o setInterval() ∴ w/o eval()
@demoive
demoive / validoku.js
Last active December 15, 2015 00:59
Validates a Sudoku game. The input (vals) is assumed to be an array of arrays with numerical values from 1 through 9.
/**
* Validates a Sudoku game.
* The input <vals> is assumed to be an array of arrays with numerical values from 1 through 9.
*/
function validoku(vals) {
var val,
x, y, blockX,
uniqRow = {},
uniqCols = [{}, {}, {}, {}, {}, {}, {}, {}, {}], // this construct could be "built up" on each loop iteration, but it's done this way for clarity of example
uniqBlocks = [{}, {}, {}];
@demoive
demoive / supplant.js
Last active December 16, 2015 20:58
Variable substitution on a string. Modified from Douglas Crockford's to use {{double curly braces}}.
/**
* Variable substitution on a string.
*
* Scans through a string looking for expressions enclosed within double curly braces.
* If an expression is found, use it as a key on the object,
* and if the key has a string or number value,
* it is substituted for the bracket expression and it repeats.
*
* Originally by Douglas Crockford: http://javascript.crockford.com/remedial.html
*/