Skip to content

Instantly share code, notes, and snippets.

Andreas Fransson zykadelic

  • Sweden
Block or report user

Report or block zykadelic

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@zykadelic
zykadelic / object.slice.js
Created Jun 27, 2014
Returns a new object with only the keys provided as arguments, leaves old object intact
View object.slice.js
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 Aug 29, 2015
Cleans up the object from the given arguments
View object.except.js
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;
}
}
View supplant.js
// 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 Mar 25, 2014
Returns a new array without any occurrence of the argument.
View array.remove.js
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 Dec 24, 2015 — 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
View default_form_builder.rb
# 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 Dec 19, 2015
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).
View array.includes.js
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 Jul 17, 2013
See if two different objects have identical key-value pairs
View equal_objects.js
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;
}
}
}
View date.now.js
if(!Date.now){
Date.now = function now(){
return new Date().getTime();
}
}
@zykadelic
zykadelic / jquery.placeholder.css
Last active Dec 14, 2015
Cross-browser placeholder support, a modified version of Caleb Ogden’s jQuery placeholder
View jquery.placeholder.css
.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 Jan 27, 2016
Destroy all occurrences of the argument from an array. Returns the destroyed element.
View array.destroy.js
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
You can’t perform that action at this time.