Skip to content

Instantly share code, notes, and snippets.

View weslleyaraujo's full-sized avatar
💤

Weslley Araujo weslleyaraujo

💤
View GitHub Profile
@weslleyaraujo
weslleyaraujo / extend.js
Created October 4, 2014 18:46
Merge objects
Object.defineProperty(Object.prototype, 'extend', {
enumerable: false,
value: function (from) {
var props = Object.getOwnPropertyNames(from),
dest = this;
props.forEach(function(name) {
var destination;
if (name in dest) {
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
@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>
@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');
function gi() {
rm .gitignore;
(curl http://www.gitignore.io/api/$@) >> .gitignore;
}
@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) {
var fn = function even (n) {
if (n === 0) {
return true
}
else return !even(n - 1)
}
fn(5);
//=> false
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 slug = function (text) {
return text
.toLowerCase()
.replace(/[^\w ]+/g,'')
.replace(/ +/g,'-');
};
@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)+'&hellip;' : this;
};