Skip to content

Commit

Permalink
feat: allow to 'zip' a fileCollection instance
Browse files Browse the repository at this point in the history
This also removes fileCollectionItemsZip that was likely not used
  • Loading branch information
lpatiny committed Feb 3, 2023
1 parent 2afa8f4 commit 6962cf6
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 80 deletions.
12 changes: 4 additions & 8 deletions src/FileCollection.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { FileCollectionItem } from './FileCollectionItem';
import {
fileCollectionItemsZip,
FileCollectionItemsZipOptions,
} from './fileCollectionItemsZip';
import { fileCollectionToZip } from './fileCollectionToZip';

export class FileCollection {
readonly files: FileCollectionItem[];
Expand All @@ -19,11 +16,10 @@ export class FileCollection {
* Zip the FileCollection
* This method returns a new FileCollection that contains only one FileItem that
* is the zipped file (called by default 'file.zip')
* Not sure this is super useful and we should probably remove it and replace it by fileCollectionToZip
*/
async zip(options: FileCollectionItemsZipOptions = {}) {
return new FileCollection([
await fileCollectionItemsZip(this.files, options),
]);
async zip() {
return fileCollectionToZip(this);
}

[Symbol.iterator]() {
Expand Down
21 changes: 17 additions & 4 deletions src/__tests__/FileCollection.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/prefer-regexp-exec */
import { join } from 'path';

import { fileCollectionFromPath } from '../fileCollectionFromPath';
import { fileCollectionFromZip } from '../fileCollectionFromZip';

describe('FileCollection', () => {
it('filter', async () => {
Expand All @@ -18,8 +18,21 @@ describe('FileCollection', () => {
).toStrictEqual(['dir1/dir3/e.txt - e.txt', 'dir1/dir3/f.txt - f.txt']);

const zipped = await fileCollection.zip();
const zippedFile = zipped.files[0];
expect(zippedFile.relativePath).toBe('file.zip');
expect((await zippedFile.arrayBuffer()).byteLength).toBe(612);
expect(zipped).toHaveLength(612);

const unzippedFileCollection = await fileCollectionFromZip(zipped);

expect(
Array.from(
unzippedFileCollection.files.map(
(a) => `${a.relativePath} - ${a.name}`,
),
),
).toStrictEqual([
'dir1/a.txt - a.txt',
'dir1/b.txt - b.txt',
'dir1/dir3/e.txt - e.txt',
'dir1/dir3/f.txt - f.txt',
]);
});
});
20 changes: 0 additions & 20 deletions src/__tests__/fileCollectionItemsZip.test.ts

This file was deleted.

5 changes: 2 additions & 3 deletions src/__tests__/fileCollectionToZip.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ describe('fileCollectionToZip', () => {
join(__dirname, 'data'),
);

const arrayBuffer = await fileCollectionToZip(fileCollection);

const unzipped = await fileCollectionFromZip(arrayBuffer);
const typedArray = await fileCollectionToZip(fileCollection);
const unzipped = await fileCollectionFromZip(typedArray);
const paths = unzipped.files.map((file) => file.relativePath);
expect(paths).toStrictEqual([
'data/dir1/a.txt',
Expand Down
42 changes: 0 additions & 42 deletions src/fileCollectionItemsZip.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/fileCollectionToZip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import { FileCollection } from './FileCollection';
*/
export async function fileCollectionToZip(
fileCollection: FileCollection,
): Promise<ArrayBuffer> {
): Promise<Uint8Array> {
const jsZip = new JSZip();

for (const file of fileCollection) {
jsZip.file(file.relativePath, await file.arrayBuffer());
}

return jsZip.generateAsync({ type: 'arraybuffer' });
return jsZip.generateAsync({ type: 'uint8array' });
}
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export * from './fileCollectionFromWebservice';
export * from './fileCollectionFromFileList';
export * from './fileCollectionFromFileArray';
export * from './fileCollectionFromFiles';
export * from './fileCollectionToZip';
export * from './groupFiles';
export * from './FileCollection';
export * from './FileCollectionItem';

0 comments on commit 6962cf6

Please sign in to comment.