Created
March 30, 2013 21:30
-
-
Save AlienHoboken/5278438 to your computer and use it in GitHub Desktop.
Javascript file path shortener.
Depends on jQuery.
This file contains hidden or 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
$(document).ready(function () { | |
var str = "H:\\Informatique\\Personnalisation\\Icones\\__\\Crystal GT PNG Pack\\Applications\\licq.png"; | |
var maxLength = 25; | |
var tokens = str.split("\\"); | |
var drive = tokens[0]; | |
var fileName = tokens[tokens.length - 1]; | |
var len = drive.length + fileName.length; | |
//remove the current lenth and also space for 3 dots and 2 slashes | |
var remLen = maxLength - len - 5; | |
//if remLen < 0, then it will over flow our maxLength to still maintain drive and filename | |
if (remLen > 0) { | |
//remove first and last elements from the array | |
tokens.splice(0, 1); | |
tokens.splice(tokens.length - 1, 1); | |
//recreate our path | |
var path = tokens.join("\\"); | |
//handle the case of an odd length | |
var lenA = Math.ceil(remLen / 2); | |
var lenB = Math.floor(remLen / 2); | |
//rebuild the path from beginning and end | |
var pathA = path.substring(0, lenA); | |
var pathB = path.substring(path.length - lenB); | |
path = drive + "\\" + pathA + "..." + pathB + "\\" + fileName; | |
//write it out | |
$("body").html("Orig. Path: " + str + "<br /><br />" + | |
"New Path: " + path + "<br /><br />" + | |
"MaxLength: " + maxLength + "<br /><br />" + | |
"New Length: " + path.length); | |
} else { | |
//try and fit our maxlength by taking only drive and filename | |
$("body").html("Orig. Path: " + str + "<br /><br />" + | |
"New Path: " + drive + "\\" + fileName + "<br /><br />" + | |
"MaxLength: " + maxLength + "<br /><br />" + | |
"New Length: " + (len + 1) + "<br /><br />"); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment