Here one more detailed solution using Regular Expressions.
Alternative 1 (generic): Get a file name (whatever) from a string path.
const FILE_NAME_REGEX = /(.+)\/(.+)$/
console.log("/home/username/my-package/my-file1.ts".replace(FILE_NAME_REGEX, '$2'))
console.log("/home/username/my-package/my-file2.js".replace(FILE_NAME_REGEX, '$2'))
Alternative 2 (advanced): Get only a .ts
file name from a string path.
const JS_PATH = "/home/username/my-package/my-file.spec.js"
const TS_PATH = "/home/username/my-package/my-file.spec.ts"
// RegExp accepts only `.ts` file names in lower case with optional dashes and dots
const TS_REGEX = /(.+)\/([a-z.-]+\.(ts))$/
// a. Get only the file name of a `.ts` path
console.log(TS_PATH.replace(TS_REGEX, '$2'))
// b. Otherwise it will return the complete path
console.log(JS_PATH.replace(TS_REGEX, '$2'))
Note: Additionally you can test first the regular expressions above in order to validate them before to getting the expected value.
TS_REGEX.test(TS_PATH)
// > true
TS_REGEX.test(JS_PATH)
// > false
More information at MDN - Regular Expressions