CodeWars Kata: File Path Operations

The quick Code Wars Kata I am taking a look at the Kata File-Path-Operations.

My approach to solving the kata was, first to figure out what skills are needed to solve the problem. Since all three problems are similar and try to find a specific pattern in a string, the obvious answer is Regular Expressions.

The fastest way to learn, create and test your Regular Expressions is using Regex 101.

I solved the Kata using Typescript and this is my final code:

export class FileMaster {

  constructor(private filepath:string) { }

  extension() {
   return this.filepath.match(/\.(.*)/)[1];

  filename() {
   return this.filepath.match(/\/([^\/]*)\./)[1];

  dirpath() {
   return this.filepath.match(/(.*\/)/)[1];

This piece of code would not be suitable for production. It does not take into account of malformed filenames, it would not work for windows; It would have problems with folders that contain a “.” etc.

If you would be using NodeJS you would use the existing functions in the Path Module.

Leave a Reply

Your email address will not be published. Required fields are marked *