![]() → path.extname documentation path.normalize const path = require ( 'path' ) let imageTypes = function isImage ( filepath ) isImage ( 'picture.png' ) // true isImage ( 'myProgram.exe' ) // false isImage ( 'pictures/selfie.jpeg' ) // true We use path.extname to get the extension of a file. For simplicity’s sake, we’ll only be checking against the most common image types. For our example we’re going to make a function that tells us if a file is an image. Say we need to know what the extension of a file is. → path.dirname documentation path.extname log ( 'The parent directory of the file is', directoryOfFile ) // The parent directory of the file is C:/Users/moose/Pictures/Photos/India2019 const path = require ( 'path' ) // Shortened for readability let filepath = 'C:/Users/./Pictures/Photos/India2019/DSC_0002.jpg' let directoryOfFile = path. path.dirname returns the lowest level directory in a file path. The path.dirname function is here for us. Sometimes we need to know the directory that a file is in, but the file path we have leads to a file within that directory. → path.basename documentation path.dirname log ( 'name of image:', imageName ) // name of image: bob_smith ![]() const path = require ( 'path' ) // Shortened for readability let filepath = 'C:/Users/./intro-to-the-path-module/images/useravatars/bob_smith.png' let imageName = path. Now this is cool and all, but what if we want it without the extension? Lucky for us, we just have to tell path.basename to remove it. log ( 'name of image:', imageName ) // name of image: bob_smith.png For this example, let’s say we want to know the name of an image, but we were passed the whole file path. ![]() In layman’s terms, it returns either the name of the file or directory that the file path refers to. log ( 'the file path of the image is', filepath ) // the filepath of the image is // C:/Users/./intro-to-the-path-module/images/useravatars/bob_smith.png // (actual output shortened for readability)Īccording to the path docs, the path.basename method will give you the trailing part of a path. join (_dirname, '/images/useravatars/', imageName, '.png' ) // We'll talk about what _dirname does a little later on.Ĭonsole. const path = require ( 'path' ) let imageName = 'bob_smith' let filepath = path. For simplicity’s sake, we’ll assume it’s a png. ![]() For this example, let’s say that we need the file path of an image, and we have the name of the image. The join method takes two or more parts of a file path and joins them into one string that can be used anywhere that requires a file path. One of the most commonly used path methods is path.join. Now that that’s out of the way, let’s look at all the things we can use path for. To read more about the differences in the way path works on POSIX systems and Windows, see the path documentation. Something of note: path works a little bit differently depending on your OS, but that’s beyond the scope of this article. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |