extract-path-from-string
v1.0.0
Published
Extracting file path from a string.
Maintainers
Readme
extract-path-from-string
Extracting file path from a string.
Table of Contents
Install
pnpm add extract-path-from-string
# or
npm i extract-path-from-string
# or
yarn add extract-path-from-stringUsage
Get the path from a single-line comment.
import { JsTsCommentExtractor } from 'extract-path-from-string';
console.log(JsTsCommentExtractor.extract('// src/single.tsx')); // src/single.tsx
console.log(JsTsCommentExtractor.extract('~/username/abc.jsonl')) // ~/username/abc.jsonl
console.log(JsTsCommentExtractor.extract('/absolute/path/filename.ext')) // /absolute/path/filename.ext
console.log(JsTsCommentExtractor.extract('@scope/pkg/dist/index.js')) // @scope/pkg/dist/index.jsGet the path from the multi-line comment.
const comment = `/** multi env config
* dev: config/dev.tsx
* test: config/test.tsx
* prod: config/prod.tsx */`
const result = JsTsCommentExtractor.extract(comment);
console.log(result); // config/dev.tsx
const result2 = JsTsCommentExtractor.extractFromContent(comment);
console.log(result2); // [ 'config/dev.tsx' ]
const result3 = JsTsCommentExtractor.extractFromContent(comment, { findAll: true });
console.log(result3); // [ 'config/dev.tsx', 'config/test.tsx', 'config/prod.tsx' ]In addition to these, other extractors can be used, such as HandlebarsCommentExtractor, HtmlCommentExtractor, YamlCommentExtractor, CommentExtractorFactory, and GenericExtractor. CommentExtractorFactory is a factory function for extractors. If you don't want to import a specific extractor, you can use GenericExtractor.
Match the path in the string. Essential language features to consider.
import { matchPath } from 'extract-path-from-string';
console.log(matchPath('file.txt', '*.txt')); // [ 'file.txt' ]
console.log(matchPath('file.txt', ['*.txt'])) // [ 'file.txt' ]API
All *Extractor classes implement the Extractor interface.
export interface Extractor {
/** Extract file paths from a single comment */
extract(comment: string): string | null;
/** Extract all file paths from the file content */
extractFromContent(content: string, options?: ExtractOptions): string[];
}
export interface ExtractOptions {
/** Whether to find all matches (default: false, only return the first one) */
findAll?: boolean;
/** Maximum number of lines to search (default: unlimited) */
maxLines?: number;
/**
* Regular expression flags https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions#advanced_searching_with_flags
*/
flags?: string;
}Features
- ✅ Supports single-line comments (
//,#,//!,{{}},<!-- -->) - ✅ Supports multi-line comments (
/* */,{{}},<!-- -->) - ✅ Supports relative paths, absolute paths, and home directory paths
- ✅ Supports npm scoped packages (
@scope/package) - ✅ Supports multi-level file extensions (
.d.ts,.spec.ts) - ✅ Supports line and column numbers (
main.go#L6-L9) - ✅ Supports query parameters and anchors (
src/alert.html?plain=1,src/file.html?key=value#anchor) - ✅ Configurable search options
