Skip to content

Commit

Permalink
Merge pull request #496 from chris48s/dedupe-sort
Browse files Browse the repository at this point in the history
de-dupe and sort array of filenames
  • Loading branch information
chris48s committed Aug 19, 2024
2 parents e3f23b4 + be22e08 commit fd7fe9c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ function Validator() {
filenames = filenames.concat(matches);
}

// de-dupe and sort
filenames = [...new Set(filenames)];
filenames.sort((a, b) => a.localeCompare(b));

const ttl = secondsToMilliseconds(config.cacheTtl || 0);
const cache = new Cache(getFlatCache(), ttl);

Expand Down
18 changes: 18 additions & 0 deletions src/cli.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,24 @@ describe("CLI", function () {
assert(logContainsError("Unsupported format txt"));
});
});

it("should de-duplicate and sort paths", async function () {
return cli({
patterns: [
"./testfiles/files/valid.json",
"./testfiles/files/valid.json",
"./testfiles/files/invalid.json",
],
schema: "./testfiles/schemas/schema.json",
format: "json",
}).then(() => {
const json = JSON.parse(logger.stdout[0]);
const results = json.results;
assert.equal(results.length, 2);
assert.equal(results[0].fileLocation, "./testfiles/files/invalid.json");
assert.equal(results[1].fileLocation, "./testfiles/files/valid.json");
});
});
});

describe("output formats", function () {
Expand Down
4 changes: 1 addition & 3 deletions src/glob.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import logger from "./logger.js";

async function getFiles(pattern) {
try {
let matches = await glob(pattern, { dot: true, dotRelative: true });
matches.sort((a, b) => a.localeCompare(b));
return matches;
return await glob(pattern, { dot: true, dotRelative: true });
} catch (e) {
logger.error(e.message);
return [];
Expand Down

0 comments on commit fd7fe9c

Please sign in to comment.