-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
209 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
frontend/libs/studio-pure-functions/src/FileNameUtils/FilenameUtils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { FileNameUtils } from './FilenameUtils'; | ||
|
||
describe('FileNameUtils', () => { | ||
describe('removeExtension', () => { | ||
it('Removes extension from filename if it exists', () => { | ||
expect(FileNameUtils.removeExtension('filename.txt')).toEqual('filename'); | ||
expect(FileNameUtils.removeExtension('filename.xsd')).toEqual('filename'); | ||
expect(FileNameUtils.removeExtension('.abc')).toEqual(''); | ||
expect(FileNameUtils.removeExtension('filename.schema.json')).toEqual('filename.schema'); | ||
}); | ||
|
||
it('Removes the extension for filenames with special characters', () => { | ||
expect(FileNameUtils.removeExtension('file name.txt')).toBe('file name'); | ||
expect(FileNameUtils.removeExtension('my-file.name$!.txt')).toBe('my-file.name$!'); | ||
expect(FileNameUtils.removeExtension('.hiddenfile.txt')).toBe('.hiddenfile'); | ||
expect(FileNameUtils.removeExtension('file123.456.txt')).toBe('file123.456'); | ||
}); | ||
|
||
it('Returns same input string if there is no extension', () => { | ||
expect(FileNameUtils.removeExtension('filename')).toEqual('filename'); | ||
expect(FileNameUtils.removeExtension('')).toEqual(''); | ||
}); | ||
|
||
it('returns an empty string if the filename starts with dot', () => { | ||
expect(FileNameUtils.removeExtension('.hiddenfile')).toBe(''); | ||
expect(FileNameUtils.removeExtension('.')).toBe(''); | ||
}); | ||
}); | ||
|
||
describe('removeSchemaExtension', () => { | ||
it('Removes .schema.json extension from filename if it exists', () => { | ||
expect(FileNameUtils.removeSchemaExtension('filename.schema.json')).toEqual('filename'); | ||
expect(FileNameUtils.removeSchemaExtension('filename.SCHEMA.JSON')).toEqual('filename'); | ||
}); | ||
|
||
it('Removes .xsd extension from filename if it exists', () => { | ||
expect(FileNameUtils.removeSchemaExtension('filename.xsd')).toEqual('filename'); | ||
expect(FileNameUtils.removeSchemaExtension('filename.XSD')).toEqual('filename'); | ||
}); | ||
|
||
it('Returns entire input string if there is no .schema.json or .xsd extension', () => { | ||
expect(FileNameUtils.removeSchemaExtension('filename.xml')).toEqual('filename.xml'); | ||
}); | ||
}); | ||
|
||
describe('isXsdFile', () => { | ||
it('Returns true if filename has an XSD extension', () => { | ||
expect(FileNameUtils.isXsdFile('filename.xsd')).toBe(true); | ||
expect(FileNameUtils.isXsdFile('filename.XSD')).toBe(true); | ||
}); | ||
|
||
it('Returns false if filename does not have an XSD extension', () => { | ||
expect(FileNameUtils.isXsdFile('filename.schema.json')).toBe(false); | ||
expect(FileNameUtils.isXsdFile('filename')).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('extractFilename', () => { | ||
it('Returns filename if path contains a slash', () => { | ||
expect(FileNameUtils.extractFilename('/path/to/filename')).toEqual('filename'); | ||
expect(FileNameUtils.extractFilename('/path/to/filename.json')).toEqual('filename.json'); | ||
}); | ||
|
||
it('Returns path if path does not contain a slash', () => { | ||
expect(FileNameUtils.extractFilename('filename')).toEqual('filename'); | ||
expect(FileNameUtils.extractFilename('filename.json')).toEqual('filename.json'); | ||
}); | ||
}); | ||
|
||
describe('removeFileNameFromPath', () => { | ||
it('Returns file path without file name', () => { | ||
expect(FileNameUtils.removeFileNameFromPath('/path/to/filename')).toEqual('/path/to/'); | ||
expect(FileNameUtils.removeFileNameFromPath('/path/to/filename.json')).toEqual('/path/to/'); | ||
}); | ||
|
||
it('Returns file path without file name and last slash if "excludeLastSlash" is true', () => { | ||
expect(FileNameUtils.removeFileNameFromPath('/path/to/filename', true)).toEqual('/path/to'); | ||
expect(FileNameUtils.removeFileNameFromPath('/path/to/filename.json', true)).toEqual( | ||
'/path/to', | ||
); | ||
}); | ||
|
||
it('Returns empty string if path is only fileName', () => { | ||
expect(FileNameUtils.removeFileNameFromPath('filename')).toEqual(''); | ||
expect(FileNameUtils.removeFileNameFromPath('filename.json')).toEqual(''); | ||
}); | ||
|
||
it('Returns empty string if path is only fileName and "excludeLastSlash" is true', () => { | ||
expect(FileNameUtils.removeFileNameFromPath('filename', true)).toEqual(''); | ||
expect(FileNameUtils.removeFileNameFromPath('filename.json', true)).toEqual(''); | ||
}); | ||
}); | ||
}); |
42 changes: 42 additions & 0 deletions
42
frontend/libs/studio-pure-functions/src/FileNameUtils/FilenameUtils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { StringUtils } from '@studio/pure-functions'; | ||
|
||
export class FileNameUtils { | ||
/** | ||
* Remove extension from filename. | ||
* @param filename | ||
* @returns filename without extension | ||
*/ | ||
static removeExtension = (filename: string): string => { | ||
const indexOfLastDot = filename.lastIndexOf('.'); | ||
return indexOfLastDot < 0 ? filename : filename.substring(0, indexOfLastDot); | ||
}; | ||
|
||
/** | ||
* Remove json.schema or .xsd extension from filename. | ||
* @param filename | ||
* @returns filename without extension if the extension is ".schema.json" or ".xsd", otherwise the filename is returned unchanged. | ||
*/ | ||
static removeSchemaExtension = (filename: string): string => | ||
StringUtils.removeEnd(filename, '.schema.json', '.xsd'); | ||
|
||
/** | ||
* Remove json.schema or .xsd extension from filename. | ||
* @param filename | ||
* @returns filename without extension if the extension is ".schema.json" or ".xsd", otherwise the filename is returned unchanged. | ||
*/ | ||
static isXsdFile = (filename: string): boolean => filename.toLowerCase().endsWith('.xsd'); | ||
|
||
static extractFilename = (path: string): string => { | ||
const indexOfLastSlash = path.lastIndexOf('/'); | ||
return indexOfLastSlash < 0 ? path : path.substring(indexOfLastSlash + 1); | ||
}; | ||
|
||
static removeFileNameFromPath = (path: string, excludeLastSlash: boolean = false): string => { | ||
const fileName = this.extractFilename(path); | ||
const indexOfLastSlash = path.lastIndexOf('/'); | ||
return path.slice( | ||
0, | ||
path.lastIndexOf(excludeLastSlash && indexOfLastSlash > 0 ? '/' + fileName : fileName), | ||
); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { FileNameUtils } from './FilenameUtils'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.