pybricksMicropython/lib: add pythonFileExtensionRegex

This can be reused in code to work with file names.
This commit is contained in:
David Lechner
2022-03-11 15:41:16 -06:00
parent e36a30de4c
commit 7b095765ac
2 changed files with 28 additions and 1 deletions
+25 -1
View File
@@ -1,7 +1,31 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2022 The Pybricks Authors
import { FileNameValidationResult, pythonFileExtension, validateFileName } from './lib';
import {
FileNameValidationResult,
pythonFileExtension,
pythonFileExtensionRegex,
validateFileName,
} from './lib';
describe('pythonFileExtensionRegex', () => {
it.each(['test.py', 'test.PY', 'test.Py', 'test.pY'])(
'should be case insensitive',
(testCase) => {
expect(testCase.match(pythonFileExtensionRegex)).not.toBeNull();
},
);
it('should not match the extension in the middle of a string', () => {
expect('test.py.test'.match(pythonFileExtensionRegex)).toBeNull();
});
it('should only match the extension and not any other part of the string', () => {
const match = 'test.py'.match(pythonFileExtensionRegex);
expect(match).toHaveLength(1);
expect(match).toContain(pythonFileExtension);
});
});
describe('validateFileName', () => {
it('should allow file names with underscores', () => {
+3
View File
@@ -4,6 +4,9 @@
/** The Python file extension ('.py') */
export const pythonFileExtension = '.py';
/** A regular expression that matches the Python file extension. */
export const pythonFileExtensionRegex = /\.[Pp][Yy]$/;
/** The Python file MIME type ('text/x-python') */
export const pythonFileMimeType = 'text/x-python';