jekillen Posted October 14, 2015 Share Posted October 14, 2015 I have developed a utility function for extracting path info from a string containing a (Unix style) file path: basename: the last element in the path dirname: the path info minus the last element. replacing '//' with '/ ' solves situations where there may be empty array items from splitting the path string around '/': the last char in the string may be '/', which would produce an empty array item. Also, if the first char is '/', it will produce and empty array element The last line in the function to get dirname is return pathArray.join('/').replace('//', '/').substring(0, this.length - 1); replacing '//' on the end will produce '/', so that is remove in substring call (but as I write this it is evident that it needs a bit of work: do substring call only if the last char is in fact '/') it doesn't work: nothing is returned, not even an error. if I do: var path = pathArray.join('/').replace('//', '/'); return path.substring(0, path.length - 1) I works I would think that since (in the first case) there is no declared variable to receive the results of join() and replace(), that 'this' would refer to the results of join and replace. So, the question is: why wouldn't it? Thanks for time and attention JK Link to comment Share on other sites More sharing options...
davej Posted October 14, 2015 Share Posted October 14, 2015 I have developed a utility function for extracting path info from a string containing a (Unix style) file path Can you give an example of what it would do if it worked? The value of 'this' will vary depending on how a function is called. Link to comment Share on other sites More sharing options...
justsomeguy Posted October 14, 2015 Share Posted October 14, 2015 So, the question is: why wouldn't it?this refers to the current scope, in that line it will refer to the scope context in which you call your function. It will default to the window object unless another scope is specified. Inside the substring function, if you saw the code for it, this would refer to the string object that substring got called on, but it doesn't refer to that object outside of the substring function when you call it.Saving the variable and using that to refer to the length will be the most efficient way. Academically, to do it all in one line you can split the string to an array of characters, reverse it, join it back to a string, use substring to get everything except the first character [i.e., substring(1)], and split, reverse, and join it again. Obviously that's a lot more work than just saving the variable and referring to the length that way. Link to comment Share on other sites More sharing options...
jekillen Posted October 14, 2015 Author Share Posted October 14, 2015 A short example: for a path string like: someDir/anotherDir/someFile should produce basename: someFIle dirname: someDir/anotherDir (without trailing /, IE someDir/anotherDir/ ) the following is the utility itself, for the sake of sharing: function _getPathInfo(a) { // a: string, dir/file path var getDirName = function(path) { var pathArray = [] pathArray = path.split('/'); pathArray[pathArray.length - 1] = ''; // remove last item var pathOut = pathArray.join('/').replace('//', '/'); return pathOut.substring(0, pathOut.length - 1) // doesn't work: // pathArray.join('/').replace('//', '/').substring(0, this.length - 1); } var getBaseName = function(path) { var pathArray = [] pathArray = path.split('/'); return pathArray[pathArray.length - 1] } this.dirname = getDirName(a); this.basename = getBaseName(a); } In the head script: // testCase is the id of a text input element to enter arbitrary path string // getIt is the id of a button input element to run the code. // alert produces 'basename: here dirname:' for test case input: '/going/to/here' // It should produce 'basename: here dirname: /going/to' window.onload = function() { var test = function(e) { var path = document.getElementById('testCase') if(path && path.value) { var pathInfo = new _getPathInfo(path.value) alert('basename: '+pathInfo.basename+' dirname: '+pathInfo.dirname) } } var tester = document.getElementById('getIt') if(tester) { tester.addEventListener('click', function(e){test(e)}, false) } } Link to comment Share on other sites More sharing options...
justsomeguy Posted October 14, 2015 Share Posted October 14, 2015 An alternative would be to use Array.filter to remove empty elements: pathArray.filter(function(val) {return !!val;}).join('/');That way you don't have to use replace or substring. Link to comment Share on other sites More sharing options...
davej Posted October 14, 2015 Share Posted October 14, 2015 Wouldn't it be a lot more efficient to simply use lastIndexOf ? Link to comment Share on other sites More sharing options...
justsomeguy Posted October 15, 2015 Share Posted October 15, 2015 How so? This is pretty basic: var getDirName = function(path){ var pathArray = path.split('/'); pathArray.pop(); // remove last item return pathArray.filter(function(val) {return !!val;}).join('/');} Link to comment Share on other sites More sharing options...
davej Posted October 15, 2015 Share Posted October 15, 2015 OK, my way is ugly. function breaker(str1){var str = str1.trim();var len = str.length;var last0 = 0;var last1 = 0;var file = '';var path = '';if (len>0){ last0 = str.lastIndexOf('/'); last1 = str.lastIndexOf('');}if (last0>-1 && len-1>last0){ file = str.substr(last0+1); path = str.substr(0,last0);}else if (last1>-1 && len-1>last1){ file = str.substr(last1+1); path = str.substr(0,last1);}else if(last0==-1 && last1==-1){ file = str; path = '';}alert('str=['+ str +'] nfile=['+ file +'] npath=['+ path +']');}//end of function Link to comment Share on other sites More sharing options...
jekillen Posted October 16, 2015 Author Share Posted October 16, 2015 It looks like I provoked a good discussion I have since significantly revised and expanded the utility detects if the input has any instances of / and returns empty dirname value filters out instances of ../ and .. removes unnecressary / returns anonymous object literals instead of setting public member variables. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now