pybricksMicropython/lib: add module

This creates a new module for dealing with stuff specific to Pybricks
MicroPython.
This commit is contained in:
David Lechner
2022-03-11 15:41:16 -06:00
parent 38b9bb6656
commit e36a30de4c
8 changed files with 142 additions and 73 deletions
+54
View File
@@ -0,0 +1,54 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2022 The Pybricks Authors
import { FileNameValidationResult, pythonFileExtension, validateFileName } from './lib';
describe('validateFileName', () => {
it('should allow file names with underscores', () => {
expect(validateFileName('file_name', pythonFileExtension, [])).toBe(
FileNameValidationResult.IsOk,
);
});
it('should allow file names with dashes', () => {
expect(validateFileName('file-name', pythonFileExtension, [])).toBe(
FileNameValidationResult.IsOk,
);
});
it('should not allow empty strings', () => {
expect(validateFileName('', pythonFileExtension, [])).toBe(
FileNameValidationResult.IsEmpty,
);
});
it('should not allow file names with spaces', () => {
expect(validateFileName('file name', pythonFileExtension, [])).toBe(
FileNameValidationResult.HasSpaces,
);
});
it('should not allow file names that start with numbers', () => {
expect(validateFileName('1test', pythonFileExtension, [])).toBe(
FileNameValidationResult.HasInvalidFirstCharacter,
);
});
it('should not allow file names with symbols', () => {
expect(validateFileName('test$', pythonFileExtension, [])).toBe(
FileNameValidationResult.HasInvalidCharacters,
);
});
it('it should not allow file names that include the file extension', () => {
expect(validateFileName('test.py', pythonFileExtension, [])).toBe(
FileNameValidationResult.HasFileExtension,
);
});
it('should not allow file names that match existing files', () => {
expect(validateFileName('test', pythonFileExtension, ['test.py'])).toBe(
FileNameValidationResult.AlreadyExists,
);
});
});
+66
View File
@@ -0,0 +1,66 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2022 The Pybricks Authors
/** The Python file extension ('.py') */
export const pythonFileExtension = '.py';
/** The Python file MIME type ('text/x-python') */
export const pythonFileMimeType = 'text/x-python';
/** File name validation results. */
export enum FileNameValidationResult {
/** The file name is acceptable. */
IsOk,
/** The file name is an empty string. */
IsEmpty,
/** The file name contains spaces. */
HasSpaces,
/** The file name include the file file extension. */
HasFileExtension,
/** The first character is not a letter or underscore. */
HasInvalidFirstCharacter,
/** The file name has invalid characters. */
HasInvalidCharacters,
/** A file with the same name already exists. */
AlreadyExists,
}
/**
* Validates the file name according to a number of criteria.
*
* @param fileName The file name (without extension).
* @param extension The file extension (including ".").
* @param existingFiles List of existing files.
* @returns The result of the validation.
*/
export function validateFileName(
fileName: string,
extension: string,
existingFiles: ReadonlyArray<string>,
): FileNameValidationResult {
if (existingFiles.includes(`${fileName}${extension}`)) {
return FileNameValidationResult.AlreadyExists;
}
if (fileName.length === 0) {
return FileNameValidationResult.IsEmpty;
}
if (fileName.match(/\s/)) {
return FileNameValidationResult.HasSpaces;
}
if (fileName.endsWith(extension)) {
return FileNameValidationResult.HasFileExtension;
}
if (!fileName.match(/^[a-zA-Z_]/)) {
return FileNameValidationResult.HasInvalidFirstCharacter;
}
if (!fileName.match(/^[a-zA-Z0-9_-]+$/)) {
return FileNameValidationResult.HasInvalidCharacters;
}
return FileNameValidationResult.IsOk;
}