Code Lab

Files in one folder

HardJSON and data15 min

Write a function filesInFolder(paths, folder) that returns the names of the files that are right in one folder.

  • paths is an array of file paths of a project, like the output of git ls-files: folder names and the file name joined with /, for example 'src/utils/math.js'. A path never starts with /. The same path can occur more than once.
  • folder is a string: a folder path from the project root, such as 'src' or 'src/utils'. 'src/' (with a slash at the end) means the same as 'src'. The empty string '' means the project root.
  • Return an array of file names (the last part of a path, without folders). Take only files that are right in folder, not files in its subfolders. 'src2' is a different folder from 'src'.
  • Every name is in the result once.
  • Sort the names with the default sort(): it compares strings by character codes, so 'README.md' comes before 'app.js'.
  • If no file is right in folder (also when there is no such folder or paths is empty), return an empty array.

You can split a path by / to get its parts.

Examples

filesInFolder(['src/app.js', 'src/utils/math.js', 'README.md', 'src/index.html', 'src2/x.js'], 'src')

Returns [ 'app.js', 'index.html' ]

filesInFolder(['src/app.js', 'src/utils/math.js', 'README.md'], 'src/utils')

Returns [ 'math.js' ]

filesInFolder(['src/app.js', 'README.md'], 'docs')

Returns []

Asked at ServiceNow · Source

Loading the editor...
  1. InputfilesInFolder([ 'src/app.js', 'src/utils/math.js', 'README.md', 'src/index.html', 'src2/x.js' ], 'src')
    Expected[ 'app.js', 'index.html' ]
  2. InputfilesInFolder([ 'docs/intro.md', 'tests/login.spec.ts', 'tests/cart.spec.ts' ], 'tests')
    Expected[ 'cart.spec.ts', 'login.spec.ts' ]
  3. InputfilesInFolder([ 'src/app.js', 'README.md' ], 'docs')
    Expected[]

Hidden tests run on Submit.