UNPKG

linguist-js

Version:

Analyse the programming languages used in a folder or from raw content, using the same rules that GitHub Linguist does.

38 lines (37 loc) 1.14 kB
import ignore from 'ignore'; /** Stores parsed attribute information per file glob */ export default class Attributes { #attributes; constructor() { this.#attributes = {}; } get attributes() { return this.#attributes; } add(glob, attributes) { this.#attributes[glob] = attributes; } getFlaggedGlobs(attr, val) { return Object.entries(this.#attributes) .filter(([, attrs]) => attrs[attr] === val) .map(([glob]) => glob); } findAttrsForPath(relFilePath) { const resultAttrs = {}; for (const glob in this.#attributes) { const matchingAttrs = this.#attributes[glob]; // Check if glob matches rel path if (ignore().add(glob).ignores(relFilePath)) { for (const [attr, val] of Object.entries(matchingAttrs)) { if (val != null) { resultAttrs[attr] = val; } } } } if (!JSON.stringify(resultAttrs).length) { return null; } return resultAttrs; } }