-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-Authored-By: Mark Wubben <mark@novemberborn.net>
- Loading branch information
1 parent
75cbc3b
commit 1222ce9
Showing
41 changed files
with
762 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
'use strict'; | ||
|
||
const micromatch = require('micromatch'); | ||
const flatten = require('lodash/flatten'); | ||
|
||
const NUMBER_REGEX = /^\d+$/; | ||
const RANGE_REGEX = /^(?<startGroup>\d+)-(?<endGroup>\d+)$/; | ||
const LINE_NUMBERS_REGEX = /^(?:\d+(?:-\d+)?,?)+$/; | ||
const DELIMITER = ':'; | ||
|
||
const distinctArray = array => [...new Set(array)]; | ||
const sortNumbersAscending = array => { | ||
const sorted = [...array]; | ||
sorted.sort((a, b) => a - b); | ||
return sorted; | ||
}; | ||
|
||
const parseNumber = string => Number.parseInt(string, 10); | ||
const removeAllWhitespace = string => string.replace(/\s/g, ''); | ||
const range = (start, end) => new Array(end - start + 1).fill(start).map((element, index) => element + index); | ||
|
||
const parseLineNumbers = suffix => sortNumbersAscending(distinctArray(flatten( | ||
suffix.split(',').map(part => { | ||
if (NUMBER_REGEX.test(part)) { | ||
return parseNumber(part); | ||
} | ||
|
||
const {groups: {startGroup, endGroup}} = RANGE_REGEX.exec(part); | ||
const start = parseNumber(startGroup); | ||
const end = parseNumber(endGroup); | ||
|
||
if (start > end) { | ||
return range(end, start); | ||
} | ||
|
||
return range(start, end); | ||
}) | ||
))); | ||
|
||
function splitPatternAndLineNumbers(pattern) { | ||
const parts = pattern.split(DELIMITER); | ||
if (parts.length === 1) { | ||
return {pattern, lineNumbers: null}; | ||
} | ||
|
||
const suffix = removeAllWhitespace(parts.pop()); | ||
if (!LINE_NUMBERS_REGEX.test(suffix)) { | ||
return {pattern, lineNumbers: null}; | ||
} | ||
|
||
return {pattern: parts.join(DELIMITER), lineNumbers: parseLineNumbers(suffix)}; | ||
} | ||
|
||
exports.splitPatternAndLineNumbers = splitPatternAndLineNumbers; | ||
|
||
function getApplicableLineNumbers(normalizedFilePath, filter) { | ||
return sortNumbersAscending(distinctArray(flatten( | ||
filter | ||
.filter(({pattern, lineNumbers}) => lineNumbers && micromatch.isMatch(normalizedFilePath, pattern)) | ||
.map(({lineNumbers}) => lineNumbers) | ||
))); | ||
} | ||
|
||
exports.getApplicableLineNumbers = getApplicableLineNumbers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.