find hidden files

This commit is contained in:
fyears 2021-11-07 12:14:14 +08:00
parent 371089e3ee
commit 385290c1cc
2 changed files with 62 additions and 3 deletions

View File

@ -5,9 +5,28 @@ import { base32 } from "rfc4648";
export type SUPPORTED_SERVICES_TYPE = "s3" | "webdav" | "ftp"; export type SUPPORTED_SERVICES_TYPE = "s3" | "webdav" | "ftp";
export const ignoreHiddenFiles = (item: string) => { /**
const basename = path.basename(item); * If any part of the file starts with '.' or '_' then it's a hidden file.
return basename === "." || basename[0] !== "."; * @param item
* @param loose
* @returns
*/
export const isHiddenPath = (item: string, loose: boolean = true) => {
const k = path.posix.normalize(item); // TODO: only unix path now
const k2 = k.split("/"); // TODO: only unix path now
// console.log(k2)
for (const singlePart of k2) {
if (singlePart === "." || singlePart === ".." || singlePart === "") {
continue;
}
if (singlePart[0] === ".") {
return true;
}
if (loose && singlePart[0] === "_") {
return true;
}
}
return false;
}; };
/** /**

40
tests/misc.test.ts Normal file
View File

@ -0,0 +1,40 @@
import * as fs from "fs";
import * as path from "path";
import { expect } from "chai";
import * as misc from '../src/misc'
describe("Misc tests", () => {
it("should find hidden file correctly", () => {
let item = '';
expect(misc.isHiddenPath(item)).to.be.false;
item = '.'
expect(misc.isHiddenPath(item)).to.be.false;
item = '..'
expect(misc.isHiddenPath(item)).to.be.false;
item = '/x/y/z/../././../a/b/c'
expect(misc.isHiddenPath(item)).to.be.false;
item = '.hidden'
expect(misc.isHiddenPath(item)).to.be.true;
item = '_hidden_loose'
expect(misc.isHiddenPath(item)).to.be.true;
expect(misc.isHiddenPath(item, false)).to.be.false;
item = '/sdd/_hidden_loose'
expect(misc.isHiddenPath(item)).to.be.true;
item = 'what/../_hidden_loose/what/what/what'
expect(misc.isHiddenPath(item)).to.be.true;
item = 'what/../_hidden_loose/what/what/what'
expect(misc.isHiddenPath(item, false)).to.be.false;
item = 'what/../_hidden_loose/../.hidden/what/what/what'
expect(misc.isHiddenPath(item, false)).to.be.true;
});
});