-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moving isValidFileName into Utilities
Signed-off-by: Amber Torrise <[email protected]>
- Loading branch information
Amber Torrise
committed
Oct 19, 2023
1 parent
1ae9a6f
commit fbe99e5
Showing
2 changed files
with
87 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* This program and the accompanying materials are made available under the terms of the | ||
* Eclipse Public License v2.0 which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Copyright Contributors to the Zowe Project. | ||
* | ||
*/ | ||
|
||
export class Utilities{ | ||
/** | ||
* Check to ensure filename only contains valid characters | ||
* @param {fileName} string - filename to be evaluated | ||
* @returns {Promise<boolean>} - promise that resolves to true if filename contains valid characters | ||
* @memberof Utilities | ||
*/ | ||
public static async isValidFileName(fileName: string): Promise<boolean> { | ||
//to prevent magic number eslint errors | ||
//(valid characters deduced from https://en.wikipedia.org/wiki/ISO/IEC_8859-1) | ||
const iso8859_1_start_first = 32; // first valid code point for first chunk of valid characters in the ISO/IEC 8859-1 table | ||
const iso8859_1_end_first = 127; | ||
const iso8859_1_start_second = 160; //second chunk of valid characters | ||
const iso8859_1_end_second = 255; | ||
const binary = 2; | ||
const hexadecimal = 16; | ||
|
||
const unicodeString = fileName.split('').map(char => `U+${char.charCodeAt(0).toString(hexadecimal).toUpperCase()}`).join(' '); | ||
const codePoints = unicodeString.split(' '); | ||
for (const codePoint of codePoints) { | ||
// Extract the decimal representation from the code point (e.g., ☻ = U+263B => 9787) | ||
const decimalRepresentation = parseInt(codePoint.substring(binary), hexadecimal); | ||
|
||
// Check if the code point is in the range of valid characters | ||
const validRanges = [ | ||
{ start: iso8859_1_start_first, end: iso8859_1_end_first }, | ||
{ start: iso8859_1_start_second, end: iso8859_1_end_second } | ||
]; | ||
|
||
const isValidCharacter = validRanges.some(range => { | ||
return decimalRepresentation >= range.start && decimalRepresentation <= range.end; | ||
}); | ||
|
||
if (!isValidCharacter) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} |
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