Skip to content

Instantly share code, notes, and snippets.

@adriancmiranda
Last active April 15, 2022 08:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adriancmiranda/d3771de4768f841a8e3f to your computer and use it in GitHub Desktop.
Save adriancmiranda/d3771de4768f841a8e3f to your computer and use it in GitHub Desktop.
var File = function(url, object){
File.list = Array.isArray(File.list)? File.list : [];
File.progress = File.progress || 0;
this.progress = 0;
this.object = object;
this.url = url;
};
File.indexOf = function(term){
for(var index in File.list){
var file = File.list[index];
if (file.equals(term) || file.url === term || file.object === term) {
return index;
}
}
return -1;
};
File.find = function(term){
var index = File.indexOf(term);
return ~index && File.list[index];
};
File.prototype.equals = function(file){
var isFileType = file instanceof File;
return isFileType && this.url === file.url && this.object === file.object;
};
File.prototype.save = function(update){
update = typeof update === 'undefined'? true : update;
if(Array.isArray(File.list)){
var index = File.indexOf(this);
if(~index && update) {
File.list[index] = this;
console.warn('File `%s` has been loaded before and updated now for: %O.', this.url, this);
}else File.list.push(this);
console.log(File.list)
}else{
File.list = [this];
}
return this;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment