Parse .gitignore files into an array of patterns.
-
Parse .gitignore content: Extracts patterns from the content of a
.gitignore
file. -
Parse .gitignore file: Reads and extracts patterns from a
.gitignore
file at a given path. - Dedupe patterns: Removes duplicate patterns.
-
Convert patterns: Converts
.gitignore
patterns to minimatch patterns or regular expressions. - Flat configuration: Converts an array of patterns to a flat configuration object.
- Pattern matching: Tests if a pattern is ignored based on accept and ignore regular expressions.
bun i -D @lg/gitignore
import fs from 'node:fs'
import { parse } from '@lg/gitignore'
const patterns = parse(fs.readFileSync('.gitignore', 'utf-8'))
console.log(patterns) // [ "node_modules", "dist" ]
import { parsePath } from '@lg/gitignore'
const patterns = parsePath('.gitignore')
console.log(patterns) // [ "node_modules", "dist" ]
import { dedupe } from '@lg/gitignore'
const patterns = ['node_modules', 'node_modules', 'dist']
const uniquePatterns = dedupe(patterns)
console.log(uniquePatterns) // ['node_modules', 'dist']
import { toFlatConfig } from '@lg/gitignore'
const patterns = ['node_modules', 'dist']
const flatConfig = toFlatConfig(patterns, { name: 'ignore-files' })
console.log(flatConfig) // { name: 'ignore-files', ignores: ['**/node_modules', '**/dist'] }
import { ignore, toRegex } from '@lg/gitignore'
const patterns = ['.abc/*', '!.abc/d/']
const regex = toRegex(patterns)
console.log(ignore(regex, '.abc/a.js')) // true
console.log(ignore(regex, '.abc/d/e.js')) // false
Parses the given content of a .gitignore
file and extracts the patterns.
-
content: The content of the
.gitignore
file. -
options: Options for parsing.
- dedupe: Whether to remove duplicate patterns.
Reads the contents of the given gitignore filepath and extracts the patterns.
-
filepath: The path to the
.gitignore
file. -
options: Options for parsing the file.
- dedupe: Whether to remove duplicate patterns.
- strict: Whether to throw an error if the file path is invalid.
Removes duplicate patterns from the array.
- patterns: The array of patterns to dedupe.
Converts a .gitignore
pattern to a minimatch pattern.
-
pattern: The
.gitignore
pattern to convert.
Converts an array of .gitignore
patterns to a flat configuration object.
-
patterns: The array of
.gitignore
patterns to convert. -
options: Options for the flat configuration.
- name: Name of the configuration.
Converts an array of patterns into regular expressions for matching.
- patterns: The array of patterns to convert.
Tests if a pattern is ignored based on the provided accept and ignore regular expressions.
- regex: The compiled accept and ignore regular expressions.
- pattern: The pattern to test.
MIT License © 2022-PRESENT Iván Máximiliano, Lo Giudice