Skip to content

feat(volume): add caseSensitive option #632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/__tests__/volume.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,31 @@ describe('volume', () => {
expect(typeof vol.promises).toBe('object');
});
});
describe('caseSensitive: false', () => {
it('is case insensitive', () => {
const vol = new Volume(undefined, false);
vol.mkdirSync('/dir');
vol.writeFileSync('/Dir/Foo.txt', 'foo');
expect(vol.readFileSync('/dir/foo.TXT', {encoding: 'utf8'})).toBe('foo');
vol.unlinkSync('/dir/foo.txt');
expect(vol.existsSync('/Dir/Foo.txt')).toBe(false)
});
it('preserve original filename casing', () => {
const vol = new Volume(undefined, false);
vol.fromJSON({
'/Dir': null,
'/dir': null,
'/fileA.txt': '1',
'/filea.txt': '2',
'/FileB.txt': '1',
'/fileb.txt': '2',
});
expect(vol.readdirSync('/', {encoding: 'utf8'})).toEqual(['Dir', 'fileA.txt', 'FileB.txt']);
expect((vol.readdirSync('/', {encoding: 'utf8', withFileTypes: true}) as Dirent[]).map(({name}) => name)).toEqual(['Dir', 'fileA.txt', 'FileB.txt']);
expect(vol.readFileSync('/fileA.txt', {encoding: 'utf8'})).toBe('2');
expect(vol.readFileSync('/FileB.txt', {encoding: 'utf8'})).toBe('2');
});
});
});
describe('StatWatcher', () => {
it('.vol points to current volume', () => {
Expand Down
6 changes: 4 additions & 2 deletions src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ export class Link extends EventEmitter {
}

setChild(name: string, link: Link = new Link(this.vol, this, name)): Link {
this.children[name] = link;
this.children[this.vol.caseSensitive ? name : name.toLowerCase()] = link;
link.parent = this;
this.length++;

Expand All @@ -293,13 +293,15 @@ export class Link extends EventEmitter {
}

deleteChild(link: Link) {
delete this.children[link.getName()];
const name = link.getName();
delete this.children[this.vol.caseSensitive ? name : name.toLowerCase()];
this.length--;

this.emit('child:delete', link, this);
}

getChild(name: string): Link | undefined {
if (!this.vol.caseSensitive) name = name.toLowerCase();
if (Object.hasOwnProperty.call(this.children, name)) {
return this.children[name];
}
Expand Down
7 changes: 5 additions & 2 deletions src/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,8 @@ export class Volume {
// Current number of open files.
openFiles = 0;

caseSensitive: boolean;

StatWatcher: new () => StatWatcher;
ReadStream: new (...args) => IReadStream;
WriteStream: new (...args) => IWriteStream;
Expand All @@ -606,9 +608,10 @@ export class Volume {
return this.promisesApi;
}

constructor(props = {}) {
constructor(props = {}, caseSensitive = true) {
this.props = Object.assign({ Node, Link, File }, props);

this.caseSensitive = caseSensitive;
const root = this.createLink();
root.setNode(this.createNode(true));

Expand Down Expand Up @@ -1693,7 +1696,7 @@ export class Volume {

const list: TDataOut[] = [];
for (const name in link.children) {
list.push(strToEncoding(name, options.encoding));
list.push(strToEncoding(link.children[name]!.getName(), options.encoding));
}

if (!isWin && options.encoding !== 'buffer') list.sort();
Expand Down