Format a string containing a filesystem path or URL with Javascript, Typescript, or Php
When developing an application that interacts with file system paths or internet URLs, it is fairly common to see bugs and problems that are related to an incorrect format of these elements. This may be caused by our application handling these values incorrectly or by our users introducing them with an unexpected format.
To avoid problems it is important to make sure that every path and URL that is used by our application is consistently formatted. Things like using the same separator character, avoiding it from being duplicated, or making sure that all paths end without a trailing separator will help us code a cleaner and bug-free application.
You can play and test it online here
The formatPath() method
The TurboCommons StringUtils class contains a general-purpose formatPath() method which converts any given raw path to a standardized format. The main operations that are performed by this function are the following:
- Duplicate separator characters are removed: "a\\b\c" will become "a/b/c"
- All separator characters are unified to the same one: "a\b/c\d" will become "a/b/c/d"
- No trailing separator will exist: "a\b\c\" will become "a\b\c"
So let's see some examples of how to use it with different programming languages:
Apply standard format to any path or URL with Php
Download the latest TurboCommons phar file from the downloads section, place it on your project as a dependency and run the following code:
require 'path-to-your-dependencies-folder/turbocommons-php-X.X.X.phar';
use org\turbocommons\src\main\php\utils\StringUtils;
echo StringUtils::formatPath('test//test/')."<br>";
echo StringUtils::formatPath('////test//////test////')."<br>";
echo StringUtils::formatPath('C:\\User///projects\\\\//project1', '\\');
The output of the previous code should be:
test/test
/test/test
C:\User\projects\project1
Apply standard format to any path or URL with javascript on a website
Download the latest turbocommons-es5.js file from the downloads section or use npm to add the dependency to your project (npm install turbocommons-es5). Then run the following code:
<script src="turbocommons-es5/turbocommons-es5.js"></script>
<script>
var StringUtils = org_turbocommons.StringUtils;
console.log(StringUtils.formatPath('test//test/'));
console.log(StringUtils.formatPath('////test//////test////'));
console.log(StringUtils.formatPath('C:\\User///projects\\\\//project1', '\\'));
</script>
The console log for the previous code should be:
test/test
/test/test
C:\User\projects\project1
Apply standard format to any path or URL with Typescript (TS)
The recommended way is to use npm to obtain the turbocommons dependency by executing the following command at the root of your project:
npm install turbocommons-ts
Or you can download the latest turbocommons-ts files from the downloads section and copy the dependency by yourself. Then run the following code:
import { StringUtils } from 'turbocommons-ts';
console.log(StringUtils.formatPath('test//test/'));
console.log(StringUtils.formatPath('////test//////test////'));
console.log(StringUtils.formatPath('C:\\User///projects\\\\//project1', '\\'));
The output of the previous code should be:
test/test
/test/test
C:\User\projects\project1
Apply standard format to any path or URL on a NodeJs App
Install the dependency by executing the following command at the root of your project:
npm install turbocommons-ts
And then run the following code:
const {StringUtils} = require('turbocommons-ts');
console.log(StringUtils.formatPath('test//test/'));
console.log(StringUtils.formatPath('////test//////test////'));
console.log(StringUtils.formatPath('C:\\User///projects\\\\//project1', '\\'));
Which should output:
test/test
/test/test
C:\User\projects\project1