@@ -42,18 +42,53 @@ export const createJsVarName = (fileName: string): string => {
4242} ;
4343
4444/**
45- * Determines if a given file path points to a type declaration file (ending in .d.ts) or not. This function is
46- * case-insensitive in its heuristics.
47- * @param filePath the path to check
48- * @returns `true` if the given `filePath` points to a type declaration file, `false` otherwise
49- */
50- export const isDtsFile = ( filePath : string ) : boolean => {
51- const parts = filePath . toLowerCase ( ) . split ( '.' ) ;
52- if ( parts . length > 2 ) {
53- return parts [ parts . length - 2 ] === 'd' && parts [ parts . length - 1 ] === 'ts' ;
54- }
55- return false ;
56- } ;
45+ * Create a function that lowercases the first string parameter before passing it to the provided function
46+ * @param fn the function to pass the lowercased path to
47+ * @returns the result of the provided function
48+ */
49+ const lowerPathParam = ( fn : ( p : string ) => boolean ) => ( p : string ) => fn ( p . toLowerCase ( ) ) ;
50+
51+ /**
52+ * Determine if a stringified file path is a TypeScript declaration file based on the extension at the end of the path.
53+ * @param p the path to evaluate
54+ * @returns `true` if the path ends in `.d.ts` (case-sensitive), `false` otherwise.
55+ */
56+ export const isDtsFile = lowerPathParam ( ( p ) => p . endsWith ( '.d.ts' ) || p . endsWith ( '.d.mts' ) || p . endsWith ( '.d.cts' ) ) ;
57+
58+ /**
59+ * Determine if a stringified file path is a TypeScript file based on the extension at the end of the path. This
60+ * function does _not_ consider type declaration files (`.d.ts` files) to be TypeScript files.
61+ * @param p the path to evaluate
62+ * @returns `true` if the path ends in `.ts` (case-sensitive) but does _not_ end in `.d.ts`, `false` otherwise.
63+ */
64+ export const isTsFile = lowerPathParam (
65+ ( p : string ) => ! isDtsFile ( p ) && ( p . endsWith ( '.ts' ) || p . endsWith ( '.mts' ) || p . endsWith ( '.cts' ) ) ,
66+ ) ;
67+
68+ /**
69+ * Determine if a stringified file path is a TSX file based on the extension at the end of the path
70+ * @param p the path to evaluate
71+ * @returns `true` if the path ends in `.tsx` (case-sensitive), `false` otherwise.
72+ */
73+ export const isTsxFile = lowerPathParam (
74+ ( p : string ) => p . endsWith ( '.tsx' ) || p . endsWith ( '.mtsx' ) || p . endsWith ( '.ctsx' ) ,
75+ ) ;
76+
77+ /**
78+ * Determine if a stringified file path is a JSX file based on the extension at the end of the path
79+ * @param p the path to evaluate
80+ * @returns `true` if the path ends in `.jsx` (case-sensitive), `false` otherwise.
81+ */
82+ export const isJsxFile = lowerPathParam (
83+ ( p : string ) => p . endsWith ( '.jsx' ) || p . endsWith ( '.mjsx' ) || p . endsWith ( '.cjsx' ) ,
84+ ) ;
85+
86+ /**
87+ * Determine if a stringified file path is a JavaScript file based on the extension at the end of the path
88+ * @param p the path to evaluate
89+ * @returns `true` if the path ends in `.js` (case-sensitive), `false` otherwise.
90+ */
91+ export const isJsFile = lowerPathParam ( ( p : string ) => p . endsWith ( '.js' ) || p . endsWith ( '.mjs' ) || p . endsWith ( '.cjs' ) ) ;
5792
5893/**
5994 * Generate the preamble to be placed atop the main file of the build
0 commit comments