Skip to content

Instantly share code, notes, and snippets.

View template.js
// Simple JavaScript Templating
// John Resig - http://ejohn.org/ - MIT Licensed
(function(){
var cache = {};
this.tmpl = function tmpl(str, data){
// Figure out if we're getting a template, or if we need to
// load the template - and be sure to cache the result.
var fn = !/\W/.test(str) ?
cache[str] = cache[str] ||
View slug.js
var slug = function (text) {
return text
.toLowerCase()
.replace(/[^\w ]+/g,'')
.replace(/ +/g,'-');
};
View smart_truncate.js
var truncate = function(string, n, terminator) {
if (typeof string !== 'string') {
return;
}
var lastIndex = string.lastIndexOf(' ', n), ret = string.toString();
if(ret.length <= n) return ret;
if (lastIndex != -1) {
View trunc.js
String.prototype.trunc = function(n){
return this.length>n ? this.substr(0,n-1)+'&hellip;' : this;
};
View wtf.js
var fn = function even (n) {
if (n === 0) {
return true
}
else return !even(n - 1)
}
fn(5);
//=> false
@weslleyaraujo
weslleyaraujo / prototype-pattern.js
Last active Aug 29, 2015
Pattern for private methods using prototype
View prototype-pattern.js
var ClassName = (function () {
var
// here is the place to inject private methods
_private = {},
// all attributes values goes here
attributes = {
};
function ClassName (args) {
View ignore.sh
function gi() {
rm .gitignore;
(curl http://www.gitignore.io/api/$@) >> .gitignore;
}
@weslleyaraujo
weslleyaraujo / module.js
Created Sep 22, 2014
Module pattern for class
View module.js
;(function (root) {
var defaults = {
defaultValue: 'thats a default value'
};
// constructor class
function ClassName (options) {
this.options = $.extend(defaults, options);
console.log('im the constructor');
@weslleyaraujo
weslleyaraujo / template.html
Created Sep 24, 2014
Simple Javascript Templating by John Resig
View template.html
<script type="text/html" id="item_tmpl">
<div id="<%=id%>" class="<%=(i % 2 == 1 ? " even" : "")%>">
<div class="grid_1 alpha right">
<img class="righted" src="<%=profile_image_url%>"/>
</div>
<div class="grid_6 omega contents">
<p><b><a href="/<%=from_user%>"><%=from_user%></a>:</b> <%=text%></p>
</div>
</div>
</script>
View typeof.js
function TypeOf () {
return Object.prototype.toString.call(this)
.replace(/(\bobject\b\s)/g, '')
.replace(/\[/g, '')
.replace(/\]/g, '')
.toLowerCase();
};
console.log(TypeOf.call({})); // object
console.log(TypeOf.call([])) // array