Skip to content
This repository has been archived by the owner on Jun 24, 2024. It is now read-only.

Commit

Permalink
feat: add directory method (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
sveisvei authored Nov 17, 2017
1 parent 1299fe6 commit 85c4a10
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ Async method for writing file to storage

Async method for checking if file exist in storage

### dir(directoryName: string): Promise<Array>

Async method for getting all files in a given directory

### writer(type)

Expand Down
19 changes: 19 additions & 0 deletions lib/sink.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const common = require('asset-pipe-common');
const stream = require('readable-stream');
const concat = require('concat-stream');
const assert = require('assert');
const path = require('path');

class WriteStream extends stream.PassThrough {
constructor(db, type) {
Expand Down Expand Up @@ -84,6 +85,24 @@ module.exports = class SinkMem extends EventEmitter {
return !!this.db[fileName];
}

async dir(directoryName) {
const results = Object.keys(this.db)
.filter(key => {
const dirname = `/${path.dirname(key)}`.replace(/\.$/, '');
return dirname === directoryName;
})
.map(key => ({
fileName: key,
content: this.db[key],
}));
if (results.length === 0) {
throw new Error(
`Missing folder with name "${directoryName}" or empty result`
);
}
return results;
}

writer(type) {
assert(type, '"type" is missing');
return new WriteStream(this.db, type);
Expand Down
45 changes: 45 additions & 0 deletions test/__snapshots__/sink.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,48 @@
exports[`.set() - should not set value if missing value 1`] = `[AssertionError [ERR_ASSERTION]: "fileContent" is missing]`;

exports[`.set() - should not set value if missing value 2`] = `[Error: No file with name "some-key-1"]`;

exports[`dir() - should error when folder does not exist 1`] = `"Missing folder with name \\"/dir/missing\\" or empty result"`;

exports[`dir() - should list 1 file from a directory 1`] = `
Array [
Object {
"content": "value-1",
"fileName": "some-key-1",
},
]
`;

exports[`dir() - should list 3 files from a directory 1`] = `
Array [
Object {
"content": "value-1",
"fileName": "some-key-1",
},
Object {
"content": "value-1",
"fileName": "some-key-2",
},
Object {
"content": "value-1",
"fileName": "some-key-3",
},
]
`;

exports[`dir() - should list 3 files from a sub directory 1`] = `
Array [
Object {
"content": "wanted-1",
"fileName": "dir/sub/some-key-1",
},
Object {
"content": "wanted-2",
"fileName": "dir/sub/some-key-2",
},
Object {
"content": "wanted-3",
"fileName": "dir/sub/some-key-3",
},
]
`;
43 changes: 43 additions & 0 deletions test/sink.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,49 @@ test('.has() - should return true if value present', async () => {
expect(await sink.has('some-key-1')).toBe(true);
});

test('dir() - should list 1 file from a directory', async () => {
const sink = new Sink();

await sink.set('some-key-1', 'value-1');
expect(await sink.dir('/')).toMatchSnapshot();
});

test('dir() - should list 3 files from a directory', async () => {
const sink = new Sink();

await sink.set('some-key-1', 'value-1');
await sink.set('some-key-2', 'value-1');
await sink.set('some-key-3', 'value-1');
expect(await sink.dir('/')).toMatchSnapshot();
});

test('dir() - should list 3 files from a sub directory', async () => {
const sink = new Sink();

await sink.set('some-key-1', 'value-1');
await sink.set('some-key-2', 'value-1');
await sink.set('another/some-key-3', 'value-1');
await sink.set('dir/sub/some-key-1', 'wanted-1');
await sink.set('dir/sub/some-key-2', 'wanted-2');
await sink.set('dir/sub/some-key-3', 'wanted-3');
expect(await sink.dir('/dir/sub')).toMatchSnapshot();
});

test('dir() - should error when folder does not exist', async () => {
expect.assertions(1);
const sink = new Sink();

await sink.set('some-key-1', 'value-1');
await sink.set('some-key-2', 'value-1');
await sink.set('another/some-key-3', 'value-1');
await sink.set('dir/sub/some-key-1', 'wanted-1');
try {
await sink.dir('/dir/missing');
} catch (e) {
expect(e.message).toMatchSnapshot();
}
});

test('.writer() - no value for "type" argument - should throw', () => {
const sink = new Sink();
expect(() => {
Expand Down

0 comments on commit 85c4a10

Please sign in to comment.