Skip to content

Instantly share code, notes, and snippets.

View weslleyaraujo's full-sized avatar
💤

Weslley Araujo weslleyaraujo

💤
View GitHub Profile
// 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] ||
@weslleyaraujo
weslleyaraujo / trunc.js
Created March 10, 2014 14:03
Truncate string
String.prototype.trunc = function(n){
return this.length>n ? this.substr(0,n-1)+'…' : this;
};
var slug = function (text) {
return text
.toLowerCase()
.replace(/[^\w ]+/g,'')
.replace(/ +/g,'-');
};
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) {
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 August 29, 2015 14:01
Pattern for private methods using prototype
var ClassName = (function () {
var
// here is the place to inject private methods
_private = {},
// all attributes values goes here
attributes = {
};
function ClassName (args) {
function gi() {
rm .gitignore;
(curl http://www.gitignore.io/api/$@) >> .gitignore;
}
@weslleyaraujo
weslleyaraujo / module.js
Created September 22, 2014 22:23
Module pattern for class
;(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 September 24, 2014 19:09
Simple Javascript Templating by John Resig
<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>
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