Skip to content

Instantly share code, notes, and snippets.

View zykadelic's full-sized avatar

Andreas Fransson zykadelic

View GitHub Profile
@zykadelic
zykadelic / object.slice.js
Created June 27, 2014 11:39
Returns a new object with only the keys provided as arguments, leaves old object intact
if(!Object.prototype.slice){
Object.prototype.slice = function(){
var returnObj = {};
for(var i = 0, j = arguments.length; i < j; i++){
if(this.hasOwnProperty(arguments[i])){
returnObj[arguments[i]] = this[arguments[i]];
}
}
return returnObj;
}
@zykadelic
zykadelic / object.except.js
Last active August 29, 2015 14:03
Cleans up the object from the given arguments
if(typeof Object.prototype.except !== 'function'){
Object.prototype.except = function(){
for(var i = 0, j = arguments.length; i < j; i++){
if(this.hasOwnProperty(arguments[i])){
delete this[arguments[i]];
}
}
return this;
}
}
// Douglas Crockford's Supplant
if(!String.prototype.supplant){
String.prototype.supplant = function(o){
return this.replace(/\{([^{}]*)\}/g, function(a, b){
var r = o[b];
return typeof r === 'string' || typeof r === 'number' ? r : a;
});
};
}
@zykadelic
zykadelic / array.remove.js
Created March 25, 2014 17:32
Returns a new array without any occurrence of the argument.
Array.prototype.remove = function(obj){
// Copy the array, so we don't override it
var array = this.slice(0);
for(var i = 0; i < array.length; i++){
// Use while-loop to find adjacent equal objects
while(array[i] === obj){
// Remove this[i]
array.splice(i, 1)[0];
@zykadelic
zykadelic / default_form_builder.rb
Last active December 24, 2015 13:39 — forked from calvincorreli/set_default_form_builder.rb
Set the default FormBuilder without having to restart the server on each change made to the DefaultFormBuilder class
# in app/helpers
class DefaultFormBuilder < ActionView::Helpers::FormBuilder
# Example of an added method that wraps custom textfields, where type can be
# :email_field, :text_field, etc.
def textfield(type, method, attributes = {})
@template.content_tag :div, class: 'textfield' do
@template.send(type, @object_name, method, objectify_options(attributes))
end
end
@zykadelic
zykadelic / array.includes.js
Last active December 19, 2015 21:29
See if the array contains the value, returns a boolean. Supports a detection between similar objects, but then requires the equalObjects function (https://gist.github.com/zykadelic/6020024).
if(!Array.prototype.includes){
Array.prototype.includes = function(value, detectSimilarObjects){
if(typeof detectSimilarObjects === 'undefined' || typeof value !== 'object'){
detectSimilarObjects = false;
}
if(detectSimilarObjects === true && typeof equalObjects !== 'function'){
throw Error("Array.includes() does not recognize equalObjects(). Get it here: https://gist.github.com/zykadelic/6020024");
}
for(i = 0; i < this.length; i++){
if(detectSimilarObjects ? equalObjects(this[i], value) : this[i] === value){
@zykadelic
zykadelic / equal_objects.js
Created July 17, 2013 12:14
See if two different objects have identical key-value pairs
function equalObjects(object, otherObject){
var objects = [object, otherObject];
for(var i = 0; i < objects.length; i++){
for(key in objects[i]){
var isUnequalObject = typeof(object[key]) === 'object' && !equalObjects(object[key], otherObject[key]);
if(!object || !otherObject || isUnequalObject || object[key] !== otherObject[key]){
return false;
}
}
}
if(!Date.now){
Date.now = function now(){
return new Date().getTime();
}
}
@zykadelic
zykadelic / jquery.placeholder.css
Last active December 14, 2015 23:49
Cross-browser placeholder support, a modified version of Caleb Ogden’s jQuery placeholder
.ui-placeholder-wrap {
position: relative;
display: block;
-moz-user-select: -moz-none;
-khtml-user-select: none;
-webkit-user-select: none;
user-select: none;
}
.ui-placeholder {
position: absolute;
@zykadelic
zykadelic / array.destroy.js
Last active January 27, 2016 21:08
Destroy all occurrences of the argument from an array. Returns the destroyed element.
Array.prototype.destroy = function(obj){
// Return null if no objects were found and removed
var destroyed = null;
for(var i = 0; i < this.length; i++){
// Use while-loop to find adjacent equal objects
while(this[i] === obj){
// Remove this[i] and store it within destroyed