Last active
December 27, 2015 03:49
-
-
Save roobie/7262279 to your computer and use it in GitHub Desktop.
javacsript misc stuff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Examples: | |
*/ | |
(function(definition) { | |
if (typeof require !== "undefined") { | |
define('fun', [], definition); | |
} else if (typeof module !== "undefined" && module.exports) { | |
module.exports.fun = definition(); | |
} else { | |
fun = definition(); | |
} | |
})(function() { | |
// ======================================================================== | |
// Closure vars | |
// ======================================================================== | |
var slice = [].slice; | |
var extend = function extend(destination) { | |
var dest = destination || {}, | |
args = slice.call(arguments, 1), | |
max = args.length, | |
i, fromObj, k; | |
for (i = 0; i < max; i++) { | |
fromObj = args[i]; | |
for (k in fromObj) { | |
if (fromObj.hasOwnProperty(k)) { | |
dest[k] = fromObj[k]; | |
} | |
} | |
} | |
return dest; | |
}; | |
var curryThis = function curryThis() { | |
if (typeof this !== "function") { return; } | |
var arity = this.length, | |
totalArgs = [], | |
fn = this; | |
function currier () { | |
totalArgs = totalArgs.concat(slice.call(arguments)); | |
if (totalArgs.length >= arity) { | |
return fn.apply(this, totalArgs); | |
} else { | |
return currier; | |
} | |
} | |
return currier; | |
} | |
var flipThis = function flipThis() { | |
var fn = this; | |
return fun(function flipper(a, b) { | |
if (arguments.length === 2) { | |
return fn.call(this, b, a); | |
} else { | |
return function flipper(b) { | |
return fn.call(this, b, a); | |
} | |
} | |
}) | |
}; | |
var composeThis = function composeThis(otherFn) { | |
var f = this, | |
g = otherFn; | |
return fun(function(a) { | |
return f(g(a)); | |
}); | |
}; | |
// ======================================================================== | |
// Extender: | |
// ======================================================================== | |
var fun = function fun(fn) { | |
return extend(fn, { | |
curry: curryThis.bind(fn), | |
composeWith: composeThis.bind(fn), | |
flip: flipThis.bind(fn) | |
}); | |
}; | |
// ======================================================================== | |
// Main API | |
// ======================================================================== | |
var curry = fun.curry = function curry(fn) { | |
return curryThis.apply(fn, arguments); | |
} | |
var flip = fun.flip = function flip(fn) { | |
return flipThis.apply(fn, arguments); | |
}; | |
var compose = fun.compose = function compose(f, g) { | |
return composeThis.call(f, g); | |
}; | |
var pipeline = fun.pipeline = flip(compose); | |
var maybe = fun.maybe = function(fn) { | |
return function (p) { | |
if (p !== null && p !== void 0) { | |
return fn.call(this, p) | |
} | |
} | |
}; | |
var invoke = fun.invoke = function invoke(fn) { | |
var args = slice.call(arguments, 1); | |
return function (instance) { | |
return fn.apply(instance, args) | |
} | |
}; | |
var once = fun.once = function once(fn) { | |
var hasRun = false; | |
return function() { | |
if (!hasRun) { | |
hasRun = true; | |
return fn.apply(this, arguments); | |
} | |
}; | |
}; | |
var fluent = fun.fluent = function fluent(methodBody) { | |
return function () { | |
methodBody.apply(this, arguments); | |
return this; | |
}; | |
}; | |
var setter = fun.setter = pipeline(maybe, fluent); | |
// ======================================================================== | |
// Mathematical operations | |
// ======================================================================== | |
var add = fun.add = fun(function(a, b) { | |
return a + b; | |
}); | |
var sub = fun.sub = fun(function(a, b) { | |
return a - b; | |
}); | |
var mul = fun.mul = fun(function(a, b) { | |
return a * b; | |
}); | |
var div = fun.div = fun(function(a, b) { | |
return a / b; | |
}); | |
var inc = fun.inc = add.curry()(1); | |
var dec = fun.dec = sub.flip().curry()(1); | |
// ======================================================================== | |
// Sequences | |
// ======================================================================== | |
var iterate = fun.iterate = fun(function(iteratorFn, initState) { | |
var state; | |
return function() { | |
state = iteratorFn(typeof state !== "undefined" ? state : initState); | |
return state; | |
}; | |
}); | |
var take = fun.take = fun(function(generatorFn, howMany, accumulator) { | |
if (howMany < 1) { | |
return accumulator; | |
} else { | |
return take( | |
generatorFn, | |
fun.dec(howMany), | |
(accumulator ? accumulator : []).concat([generatorFn()])); | |
} | |
}); | |
var range = fun.range = fun(function range(from, to, callback) { | |
if (from >= to) { return; } | |
while (from < to) { | |
callback(from); | |
from++; | |
} | |
}); | |
var memoized = fun.memoized = function memoized (fn, keymaker) { | |
var lookupTable = {}, | |
key, | |
value; | |
keymaker || (keymaker = function (args) { | |
return JSON.stringify(args) | |
}); | |
return function () { | |
var key = keymaker.call(this, arguments); | |
return lookupTable[key] || ( | |
lookupTable[key] = fn.apply(this, arguments) | |
); | |
}; | |
}; | |
return fun; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
if(!moment) { | |
return; | |
} | |
var currentMoment = new moment(); | |
var resetTimePart = function (mom) { | |
var m = new moment({ | |
y: mom.year(), | |
M: mom.month(), | |
d: mom.date(), | |
h: 0, | |
m: 0, | |
s: 0, | |
ms: 0 | |
}); | |
return m; | |
}; | |
var getFirstDayOfMonth = function (mom) { | |
var m = new moment(mom); | |
var d = m.date(); | |
return m.subtract('days', d - 1); | |
}; | |
var getFirstDayInWeek = function (mom) { | |
var m = new moment(mom); | |
var wd = m.weekday(); | |
return m.subtract('days', wd > 0 ? wd - 1 : 6); | |
}; | |
var getWeek = function (mom) { | |
var m = new moment(mom); | |
var firstDayInWeek = getFirstDayInWeek(m); | |
var result = []; | |
for (var i = 0, max = 7; i < max; i++) { | |
result.push(new moment(firstDayInWeek).add('days', i)); | |
} | |
return result; | |
}; | |
var getFirstWeekInMonth = function (mom) { | |
var firstDayOfMonth = getFirstDayOfMonth(mom); | |
return getWeek(firstDayOfMonth); | |
}; | |
var getNextWeek = function (mom) { | |
var m = new moment(mom); | |
return getWeek(m.add('days', 7)); | |
}; | |
var getMonth = function (mom) { | |
var m = new moment(mom); | |
var firstDayOfMonth = getFirstDayOfMonth(m); | |
var result = []; | |
for (var i = 0, max = m.daysInMonth(); i < max; i++) { | |
result.push(new moment(firstDayOfMonth).add('days', i)); | |
} | |
return result; | |
}; | |
var getMonthPlusAdjoining = function (mom) { | |
var firstWeekInMonth = getFirstWeekInMonth(mom); | |
var firstDayInFirstWeekInMonth = firstWeekInMonth[0]; | |
var moreThanFourWeeks = mom.daysInMonth() / 7 > 4; | |
var result = fwim; | |
var nextWeek; | |
for (var i = 0, maxi = moreThanFourWeeks ? 4 : 3; i < maxi; i++) { | |
nextWeek = getNextWeek(result.slice(-1)[0]); | |
result = result.concat(nextWeek); | |
} | |
return result; | |
}; | |
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function extend(destination) { | |
var args = [].slice.call(arguments, 1), | |
i, k, len = args.length; | |
for(i = 0; i < len; i++) { | |
for (k in args[i]) { | |
if (args[i].hasOwnProperty(k)) { | |
destination[k] = args[i][k]; | |
} | |
} | |
} | |
return destination; | |
} | |
function isa(cfg) { | |
if (!cfg) { return; } | |
if (typeof this === "function") { | |
cfg.subclass = this; | |
} | |
cfg.subclass.prototype = Object.create(cfg.superclass.prototype, cfg.props); | |
cfg.subclass.prototype.constructor = cfg.subclass; | |
cfg.subclass.prototype.base = new cfg.superclass(); | |
return cfg.subclass; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var rand = function rand(min, max) { | |
if (max === void 0) { | |
max = min; | |
min = 0; | |
} else if (min > max) { | |
throw new Error("Min can't be more than max"); | |
} | |
var rn = Math.random(); | |
return Math.floor(rn * (max - min) + min); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment