Skip to content

Instantly share code, notes, and snippets.

@husa
husa / type.js
Last active August 29, 2015 13:56
get proper type of any value
var toString = Object.prototype.toString,
regexp = /\[object (.*?)\]/;
function type(o) {
var match = toString.call(o).match(regexp);
return match[1].toLowerCase();
}
@husa
husa / getURLParams.js
Last active August 29, 2015 14:07
get URL params from string
function getURLParams(url, includeEmptyValues) {
return decodeURIComponent(url || window.location.search.slice(1)).
replace(/.*\?/, '').split('&').
reduce((params, value) => {
value = value.split('=')
if (includeEmptyValues || typeof value[1] !== 'undefined')
params[value[0]] = value[1];
return params;
}, {});
}
@husa
husa / decorator.coffee
Last active August 29, 2015 14:10
Simple Decorator
class Decorator
constructor: ({pre, post}) ->
return (func) ->
(args...) ->
args = pre?(args...) or args
ret = func(args...)
post?(ret) or ret
currency = new Decorator({post : (a) -> '$' + a})
@husa
husa / .eslintrc
Last active August 29, 2015 14:24
# which environments your script is designed to run in. Each environment brings with it a certain set of global variables and rules that are enabled by default.
env:
browser: true # - browser global variables.
#node: true # - Node.js global variables and Node.js-specific rules.
#worker: true # - web workers global variables.
# amd: true # - defines require() and define() as global variables as per the amd spec.
#mocha: true # - adds all of the Mocha testing global variables.
#jasmine: true # - adds all of the Jasmine testing global variables for version 1.3 and 2.0.
#phantomjs: true # - phantomjs global variables.
#jquery: true # - jquery global variables.
@husa
husa / capitalize.js
Last active August 29, 2015 14:24
Capitalize every work in a string
function capitalize (str) {
return str.replace(/(?:^|\s)\S/g, a => a.toUpperCase());
}
@husa
husa / extend.js
Last active August 29, 2015 14:25
extend. the one and only
function extend () {
return [].reduce.call(arguments, function (prev, current) {
for (var prop in current) {
if (!current.hasOwnProperty(prop)) continue;
if (/^(object|array)$/.test(type(current[prop])) && current[prop] !== current) {
if (!/^(object|array)$/.test(type(prev[prop]))) {
prev[prop] = type(current[prop]) === 'array' ? [] : {};
}
prev[prop] = extend(prev[prop], current[prop]);
} else {
@husa
husa / declaration.js
Last active December 18, 2015 02:38
how to declare a global variable or export it as CJS or AMD module if posible. 'this' must refer to global object or you can put this code into global scope
// myModule - is our global we want to export
var root = this;
if (typeof(exports) !== 'undefined') {
if (typeof(module) !== 'undefined' && module.exports) {
exports = module.exports = myModule;
}
exports.myModule = myModule;
} else {
if (typeof define === 'function' && define.amd) {
@husa
husa / node_build.json
Last active December 18, 2015 04:10
Node js Build for Sublime Text 2 To import it to Sublime Text: Tools -> Build System -> New Build System, and paste this code there To use: Tools -> Build System, and select your newly created build
// new build
{
"cmd": ["node", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.js"
}
//key bindings for build
[
{ "keys": ["ctrl+b"], "command": "build" },
{ "keys": ["ctrl++alt+b"], "command": "exec", "args": {"kill": true} }
@husa
husa / singleton.js
Created July 24, 2013 20:35
Singleton pattern with instance in a closure
function Singleton() {
var instance,
that = this || {};
Singleton = function() {
return instance;
}
Singleton.prototype = that;
@husa
husa / node-webkit_build.json
Created September 9, 2013 14:18
Node-webkit build for Sublime Text Tools > Build > New Build System
// first don't forget to add node-webkit directory to PATH
{
"cmd": ["nw", "$file_path"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "text.html"
}