Skip to content

Instantly share code, notes, and snippets.

@insin
Created August 18, 2011 15:16
Show Gist options
  • Select an option

  • Save insin/1154287 to your computer and use it in GitHub Desktop.

Select an option

Save insin/1154287 to your computer and use it in GitHub Desktop.
Utility Block from Sacrum
!function(__global__, server) {
// =============================================================== Utilities ===
var DOMBuilder = (server ? require('DOMBuilder') : __global__.DOMBuilder)
, forms = (server ? require('newforms') : __global__.forms)
var slice = Array.prototype.slice
, toString = Object.prototype.toString
/**
* Replaces the contents of an element.
*/
function replace(el, content) {
if (typeof el == 'string') {
el = document.getElementById(el)
}
el.innerHTML = ''
if (isArray(content)) {
content = DOMBuilder.fragment(content)
}
else if (isString(content)) {
content = document.createTextNode(content)
}
el.appendChild(content)
return el
}
/**
* Binds a function with a calling context and (optionally) some curried arguments.
*/
function bind(fn, ctx) {
var curried = null
if (arguments.length > 2) {
curried = slice.call(arguments, 2)
}
var f = function() {
if (curried) {
return fn.apply(ctx, curried.concat(slice.call(arguments)))
}
return fn.apply(ctx, arguments)
}
f.func = fn
f.boundTo = ctx
return f
}
/**
* Makes a constructor inherit another constructor's prototype without having
* to actually use the constructor.
*/
function inherits(child, parent) {
var F = function() {}
F.prototype = parent.prototype
child.prototype = new F()
child.prototype.constructor = child
child.__super__ = parent.prototype
return child
}
/**
* Inherits inherit another constructor's prototype sets its prototype and
* constructor properties in one fell swoop. If a child constructor is not
* provided via prototypeProps.constructor, a new constructor will be created
* for you.
*/
function subclass(parent, prototypeProps, constructorProps) {
var child
if (prototypeProps && prototypeProps.hasOwnProperty('constructor')) {
child = prototypeProps.constructor
} else {
// Create a new constructor if one wasn't given
child = function() { return parent.apply(this, arguments) }
}
// Inherit constructor properties
extend(child, parent)
// Inherit the parent's prototype
inherits(child, parent)
// Add prototype properties, if given
if (prototypeProps) {
extend(child.prototype, prototypeProps)
}
// Add constructor properties, if given
if (constructorProps) {
extend(child, constructorProps)
}
return child
}
/**
* Creates or uses a child constructor to inherit from the the call context
* object, which is expected to be a constructor.
*/
function extendConstructor(prototypeProps, constructorProps) {
var child = subclass(this, prototypeProps, constructorProps)
child.extend = this.extend
return child
}
/**
* Special case extension function for Models - prototypeProps is required and
* is expected to contain a Meta property containing Model options, defining at
* least a name property.
*/
function extendModelConstructor(prototypeProps, constructorProps) {
// Prepare ModelOptions for the extended Model
constructorProps = constructorProps || {}
var options = new ModelOptions(prototypeProps.Meta)
delete prototypeProps.Meta
prototypeProps._meta = constructorProps._meta = options
return extendConstructor.call(this, prototypeProps, constructorProps)
}
// Assign extend constructor functions to hoisted constructors
Views.extend = extendConstructor
Model.extend = extendModelConstructor
/**
* Copies properties from one object to another.
*/
function extend(dest, src) {
for (var prop in src) {
dest[prop] = src[prop]
}
return dest
}
/**
* Pairs up items from two lists.
*/
function zip(a1, a2) {
var l = Math.min(a1.length, a2.length)
, zipped = []
for (var i = 0; i < l; i++) {
zipped.push([a1[i], a2[i]])
}
return zipped
}
function isArray(o) {
return toString.call(o) == '[object Array]'
}
function isFunction(o) {
return toString.call(o) == '[object Function]'
}
function isRegExp(o) {
return toString.call(o) == '[object RegExp]'
}
function isString(o) {
return toString.call(o) == '[object String]'
}
/**
* Finds all matches againt a RegExp, returning captured groups if present.
*/
function findAll(re, str, flags) {
if (!isRegExp(re)) {
re = new RegExp(re, flags)
}
var match = null
, matches = []
while ((match = re.exec(str)) !== null) {
switch (match.length) {
case 1:
matches.push(match[0])
break
case 2:
matches.push(match[1])
break
default:
matches.push(match.slice(1))
}
if (!re.global) {
break
}
}
return matches
}
// Simple string formatting
var formatRE = /%s/g
, formatObjRE = /{(\w+)}/g
function format(s) {
return formatArr(s, slice.call(arguments, 1))
}
function formatArr(s, args) {
var i = 0
return s.replace(formatRE, function() { return args[i++] })
}
function formatObj(s, obj) {
return s.replace(formatObjRE, function(m, p) { return obj[p] })
}
// ...
if (server) {
module.exports = Sacrum
}
else {
__global__.Sacrum = Sacrum
}
}(this, !!(typeof module != 'undefined' && module.exports))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment