Skip to content

Instantly share code, notes, and snippets.

View anasnakawa's full-sized avatar
💭
I may be slow to respond.

Anas Nakawa anasnakawa

💭
I may be slow to respond.
View GitHub Profile
/*!
* jQuery Tiny Pub/Sub - v0.X - 11/18/2010
* http://benalman.com/
*
* Original Copyright (c) 2010 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*
* Made awesome by Rick Waldron
*
@anasnakawa
anasnakawa / ko-parse-binding.js
Created December 2, 2013 07:51
parse binding attribute, the knockout way
// parse a data-bind attribute
//
// @param {string} attr
// @return {object} parsed binding object
//
// examples:
// ---------
// parseBindings( 'something: { cool: true }' ); // { something: { cool: true } }
function parseBindings( attr ) {
return new Function( "return { " + attr + " };" )();
@anasnakawa
anasnakawa / class-es6.js
Last active December 31, 2015 17:19
js OOP by Rick Waldron
class Point {
constructor( x, y ) {
this.x = x;
this.y = y;
}
plus( aPoint ) {
return new Point( this.x + aPoint.x, this.y + aPoint.y );
}
distance() {
return Math.sqrt( this.x * this.x + this.y * this.y );
@anasnakawa
anasnakawa / tiniest-wrapper.js
Last active January 4, 2016 00:09
the tiniest module wrapper for CommonJs / AMD and the browser
// Tiniest Module Wrapper - (c) Anas Nakawa - <anas.nakawa {at} gmail.com>
// License: MIT (http://www.opensource.org/licenses/mit-license.php)
(function() {
function myLibrary() {};
// CommonJs / Node
( typeof module !== "undefined" && module.exports && ( module.exports = myLibrary ) ) ||
// AMD / RequireJs
( typeof define !== "undefined" && !define(function() { return myLibrary; }) ) ||
// browser
( typeof window !== "undefined" && ( window.myLibrary = myLibrary ) );
@anasnakawa
anasnakawa / tiny-js-pubsub.js
Last active February 2, 2023 08:14
very tiny Pub/Sub implementation that utilizes native browser event methods. ( only browsers .. no IE8 )
/*! Js Pub/Sub
* http://anasnakawa.com/
* Copyright (c) Anas Nakawa
* inspired by Ben Alman's one <https://gist.github.com/cowboy/661855>
* MIT License
*/
(function( p ) {
var e = p.e = {};
@anasnakawa
anasnakawa / query-string-parse.js
Created April 1, 2014 17:38
easy parsing for both query strings & hash into a casted object literal
/**
* parse given query string url, and return it as
* object literal with proper type casting for primitives
*
* @param {string} url
* @return {object} queryString
*
* notes:
* - requires jQuery for linear typecasting
* - parameter names are all lowercased
@anasnakawa
anasnakawa / native-string-templates.js
Last active August 29, 2015 13:58
because i don't like string concatination
/**
* string parser with data passed as arguments
*
* @param {arguments}
* @return {string}
*
* example:
* --------
* 'i used both [0] & [1], however [1] looks good so far'.parse( 'iphone', 'andoird' ); // "i used both iphone & andoird, however andoird looks good so far"
* 'the weather now in [0] seems to be [1]'.parse( 'Dubai', function() { return 'very hot'; }); // "the weather now in Dubai seems to be very hot"
@anasnakawa
anasnakawa / constant.js
Last active August 29, 2015 14:00
mocking constant behaviour using `Object.defineProperty`, works on real browsers & IE9+
/**
* simulating constant behaviour where
* given value cannot be changed
*
* @param {object} target object where the constant will be defined
* @param {string} key
* @param {object} value
* @return {object} constant
*
* example
function Car() {
this.array = [];
this.log = function() {}
}
Car.prototype.something = function() {
var self = this; // <- fine
this.array.forEach( function(item) {
self.log( item );
});
@anasnakawa
anasnakawa / sass-charset-node.js
Last active August 29, 2015 14:05
adding `charset "utf-8"` to all sass files in the current directory
/**
* node js module to add `@charset "utf-8"` to all
* sass files in the current directory
* -----------------------------------
* sovles this bug:
* https://github.com/sass/sass/issues/1037
* https://github.com/Compass/compass/issues/205
*/
var fs = require( 'fs' );