Skip to content

Instantly share code, notes, and snippets.

@michaeljacobdavis
Created August 23, 2012 21:25
Show Gist options
  • Save michaeljacobdavis/3442037 to your computer and use it in GitHub Desktop.
Save michaeljacobdavis/3442037 to your computer and use it in GitHub Desktop.
FileNameFormat
(function ($) {
$.fn.formatfilename = function (options) {
var settings = $.extend({
'length': '40'
}, options);
return this.each(function () {
var $this = $(this);
var text = $.trim($this.text());
if (text.length >= settings.length) {
var ext = (function () {
var loc = text.lastIndexOf(".");
if (loc) {
var ext = text.substring(loc, text.length);
text = text.substring(0, loc);
return ext;
}
else {
return "";
}
})();
text = text.substring(0, settings.length-ext.length) + ext;
}
$this.text(text);
});
};
})(jQuery);
$("body").append($("<span>blah.txt</span>").formatfilename({length:6})); // bl.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment