Skip to content

Instantly share code, notes, and snippets.

@rex
Last active December 18, 2015 16:29
Show Gist options
  • Select an option

  • Save rex/5811540 to your computer and use it in GitHub Desktop.

Select an option

Save rex/5811540 to your computer and use it in GitHub Desktop.
Awesome little helper methods to make life easier
/**
* These methods are written for and intended to be used within a Node.js environment.
* The only implied dependencies are the built-in Node.js API
*
* @author Pierce Moore <me@prex.io>
*/
/**
* In Node.js, this function expands the ~ character in a path that designates a user's home directory
*/
var osPath = function(pathString) {
if (pathString.substr(0,1) === '~')
pathString = process.env.HOME + pathString.substr(1)
return path.resolve(pathString)
}
/**
* These helpers and methods are all written in plain Jane JavaScript and are
* intended to be used in that context, without any external dependencies.
*
* @author Pierce Moore <me@prex.io>
*/
/**
* Extracted this from the Underscore.js source code, a performant way to determine if an object is a callable function
*/
var isFunction = function(obj) {
return !!(obj && obj.constructor && obj.call && obj.apply)
}
/**
* Repeat a string n times
* Extracted from this excellent blog post: https://javascriptweblog.wordpress.com/2010/06/01/a-tracer-utility-in-2kb/
*/
String.prototype.times = function(count) {
return count < 1 ? '' : new Array(count + 1).join(this);
}
/**
* These mixins are intended to be used to make Underscore more awesome than it already is.
* The only implied dependency in the following methods is Underscore.js.
*
* @author Pierce Moore <me@prex.io>
*/
_.mixin({
/**
* Extracted _.after from the Underscore source code and modified heavily. This function is
* intended to fire the callback function every X iterations, instead of just once after the
* iteration limit is complete.
*/
step : function(step, func) {
var count = 0
return function() {
if (++count % step == 0) {
func.apply(this, arguments)
}
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment