-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Fredrick Galoso
committed
May 27, 2012
1 parent
2deba4b
commit faace52
Showing
1 changed file
with
76 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* Crackle | ||
* jQuery comparator, equality, and JavaScript utility functions | ||
* git@github.com:wayoutmind/crackle.git | ||
*/ | ||
|
||
// ==ClosureCompiler== | ||
// @compilation_level SIMPLE_OPTIMIZATIONS | ||
// @output_file_name crackle.min.js | ||
// ==/ClosureCompiler== | ||
|
||
(function($) { | ||
var Crackle = { | ||
/** | ||
* Return elements of `self` not in `that` | ||
* @param self {Array} | ||
* @param that {Array} | ||
*/ | ||
difference: function(self) { | ||
return { | ||
from: function(that) { | ||
return self.filter(function(e) { | ||
return (that.indexOf(e) < 0); | ||
}); | ||
} | ||
}; | ||
}, | ||
/** | ||
* Return jQuery objects from HTML string(s) | ||
* @param self {String Array} or {String} | ||
*/ | ||
parse: function(self) { | ||
if (Array.isArray(self)) { | ||
return self.map(function(html) { | ||
return $(html); | ||
}); | ||
} else { | ||
return $(self); | ||
} | ||
}, | ||
/** | ||
* Return HTML strings from jQuery object(s) | ||
* @param self {jQuery Object Array} or {jQuery Object} | ||
*/ | ||
stringify: function(self) { | ||
if (typeof self === 'string') { | ||
return self; | ||
} else if (Array.isArray(self)) { | ||
return self.map(function(object) { | ||
return object.outerHTML; | ||
}); | ||
} else { | ||
return self.outerHTML; | ||
} | ||
}, | ||
/** | ||
* Fuzzy similarity comparator | ||
*/ | ||
compare: function(self) { | ||
return { | ||
to: function(that) { | ||
if (Crackle.stringify(self) === Crackle.stringify(that)) { | ||
return true; | ||
} else if ($(self).attr('id') === $(that).attr('id')) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
}; | ||
} | ||
}; | ||
|
||
window.Crackle = Crackle; | ||
|
||
})(jQuery); |