Advertisement
Guest User

renameFiles-specs.js

a guest
Aug 30th, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. /* eslint-env jasmine */
  2. /* eslint-disable no-undef */
  3.  
  4. describe('The function `renameFiles`', () => {
  5. /*
  6. You are given an array of desired filenames in the order of their creation.
  7. Since two files cannot have equal names, the one which comes later will have
  8. an addition to its name in a form of (n), where "n" is the smallest positive
  9. integer. There shouldn't be any duplicate names.
  10.  
  11. Return an array of names that will be given to the files.
  12.  
  13. Example:
  14.  
  15. let files = ['myFile', 'anotherFile', 'family-picture', 'myFile', 'another-file', 'myFile'];
  16. let newFileNames = renameFiles(files);
  17. console.log(newFilesNames);
  18. --> ['myFile', 'anotherFile', 'family-picture', 'myFile(1)', 'anotherFile(1)', 'myFile(2)']
  19. */
  20.  
  21. it('returns an array', () => {
  22. expect(
  23. Array.isArray(renameFiles(['file', 'fileTwo', 'fileThree', 'fileFour']))
  24. ).toEqual(true);
  25. });
  26.  
  27. it('does not rename files if duplicates are not present', () => {
  28. expect(
  29. renameFiles(['FullstackTestFirst', 'GuessingGame', 'FileWatcher'])
  30. ).toEqual(['FullstackTestFirst', 'GuessingGame', 'FileWatcher']);
  31. });
  32.  
  33. it('renames files if there are duplicates by adding `(n)` to the end of the filename where `n` is the smallest positive integer that the obtained name did not use.', () => {
  34. expect(renameFiles(['hello', 'world', 'hello'])).toEqual([
  35. 'hello',
  36. 'world',
  37. 'hello(1)',
  38. ]);
  39. });
  40.  
  41. it('does not rename files to names that are already taken', () => {
  42. expect(
  43. renameFiles([
  44. 'a(1)',
  45. 'a(6)',
  46. 'a',
  47. 'a',
  48. 'a',
  49. 'a',
  50. 'a',
  51. 'a',
  52. 'a',
  53. 'a',
  54. 'a',
  55. 'a',
  56. ])
  57. ).toEqual([
  58. 'a(1)',
  59. 'a(6)',
  60. 'a',
  61. 'a(2)',
  62. 'a(3)',
  63. 'a(4)',
  64. 'a(5)',
  65. 'a(7)',
  66. 'a(8)',
  67. 'a(9)',
  68. 'a(10)',
  69. 'a(11)',
  70. ]);
  71. //This definitely increases the difficulty of the problem! If a file already exists, you can't use that name. For example, if file(3) already exists, you shouldn't name another file 'file(3)'.
  72. });
  73.  
  74. //This is a tricky one! You will need to use all the tools (and debugging tools) to get to a solution.
  75. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement