Skip to content

Commit

Permalink
refactor: code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sectsect committed Sep 2, 2023
1 parent 20301e9 commit a40e249
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import fg from 'fast-glob';
import { Pattern } from 'fast-glob/out/types';
import type { Pattern } from 'fast-glob/out/types';

interface EntryPoints {
[key: string]: string;
}
type EntryPoints = Record<string, string>;

/**
* The function `splitString` takes a string and a separator as input and returns an array of
* substrings split by the separator.
* @param stringToSplit - The `stringToSplit` parameter is a string that you want to split
* into an array of substrings.
* @param separator - The separator parameter is a string that specifies the character(s) or
* regular expression used to split the stringToSplit parameter into an array of substrings.
*/
const splitString = (stringToSplit: string, separator: string) =>
stringToSplit.split(separator);

/**
* The function `dropUnderscoreFiles` filters out entries in an object that start with an underscore or
* contain a forward slash followed by an underscore.
* @param obj - The `obj` parameter is an object of type `EntryPoints`.
* @returns The function `dropUnderscoreFiles` returns an object of type `EntryPoints`.
*/
const dropUnderscoreFiles = (obj: EntryPoints) => {
const r: EntryPoints = {};
Object.entries(obj).forEach(([key, val]) => {
Expand All @@ -18,11 +30,29 @@ const dropUnderscoreFiles = (obj: EntryPoints) => {
return r;
};

/**
* The function `createRegex` creates a regular expression that matches file extensions specified in an
* array.
* @param ext - An array of file extensions.
* @returns The function `createRegex` returns a regular expression object.
*/
const createRegex = (ext: string[]) => {
const d = ext.map((v: string) => `\\.${v}`);
return new RegExp(`(${d.join('|')})$`, 'gi');
};

/**
* The function `WebpackSweetEntry` takes in file paths and returns an object with entry points for
* Webpack, removing the parent directory and file extension from the keys.
* @param paths - The `paths` parameter is a pattern or an array of patterns that
* specify the files or directories to include in the entry points. These patterns can use glob syntax
* to match multiple files or directories.
* @param ext - The `ext` parameter is the file extension(s) to filter the
* paths by. It can be a string or an array of strings. By default, it is set to `'js'`.
* @param parentdir - The `parentdir` parameter is a string that represents the parent directory
* of the entry points. It is used to extract the key for each entry point.
* @returns The function `WebpackSweetEntry` returns an object of type `EntryPoints`.
*/
export const WebpackSweetEntry = (
paths: Pattern | Pattern[],
ext: string | string[] = 'js',
Expand Down

0 comments on commit a40e249

Please sign in to comment.