filelist-utils
Advanced tools
Comparing version 0.9.1 to 0.10.0
@@ -1,8 +0,9 @@ | ||
import { PartialFile, PartialFileList } from './PartialFile'; | ||
import { FileCollection } from './FileCollection'; | ||
import { FileCollectionItem } from './FileCollectionItem'; | ||
export declare type StringObject = Record<string, string>; | ||
declare type GroupByOption = 'baseDir' | 'extension' | 'filename' | ((file?: PartialFile, fileInfo?: StringObject) => string); | ||
interface GroupOfFiles { | ||
declare type GroupByOption = 'baseDir' | 'extension' | 'filename' | ((file?: FileCollectionItem, fileInfo?: StringObject) => string); | ||
interface GroupOfFileCollection { | ||
meta: StringObject; | ||
key: string; | ||
fileList: PartialFileList; | ||
fileCollection: FileCollection; | ||
} | ||
@@ -20,4 +21,4 @@ export interface GroupFilesOptions { | ||
} | ||
export declare function groupFiles(fileList: PartialFileList, options?: GroupFilesOptions): GroupOfFiles[]; | ||
export declare function groupFiles(fileCollection: FileCollection, options?: GroupFilesOptions): GroupOfFileCollection[]; | ||
export {}; | ||
//# sourceMappingURL=groupFiles.d.ts.map |
@@ -1,5 +0,7 @@ | ||
export function groupFiles(fileList, options = {}) { | ||
import { FileCollection } from './FileCollection'; | ||
export function groupFiles(fileCollection, options = {}) { | ||
const { groupBy = 'baseDir', meta } = options; | ||
const fileCollectionItems = fileCollection.items; | ||
let results = {}; | ||
for (const file of fileList) { | ||
for (const file of fileCollectionItems) { | ||
const key = getKey(file, groupBy); | ||
@@ -10,8 +12,12 @@ if (!results[key]) { | ||
key, | ||
fileList: [], | ||
fileCollectionItems: [], | ||
}; | ||
} | ||
results[key].fileList.push(file); | ||
results[key].fileCollectionItems.push(file); | ||
} | ||
return Object.keys(results).map((key) => results[key]); | ||
return Object.keys(results).map((key) => ({ | ||
meta: results[key].meta, | ||
key, | ||
fileCollection: new FileCollection(results[key].fileCollectionItems), | ||
})); | ||
} | ||
@@ -46,9 +52,7 @@ function getMeta(key, meta) { | ||
return { | ||
baseDir: file.webkitRelativePath.replace(/\/[^/]*$/, ''), | ||
extension: file.webkitRelativePath.replace(/^.*\./, ''), | ||
filename: file.webkitRelativePath | ||
.replace(/^.*\//, '') | ||
.replace(/.[^.]*$/, ''), | ||
baseDir: file.relativePath.replace(/\/[^/]*$/, ''), | ||
extension: file.relativePath.replace(/^.*\./, ''), | ||
filename: file.relativePath.replace(/^.*\//, '').replace(/.[^.]*$/, ''), | ||
}; | ||
} | ||
//# sourceMappingURL=groupFiles.js.map |
@@ -1,8 +0,8 @@ | ||
export * from './fileListFromPath'; | ||
export * from './fileListFromZip'; | ||
export * from './fileListFromWebservice'; | ||
export * from './fileListUnzip'; | ||
export * from './fileListUngzip'; | ||
export * from './fileCollectionFromPath'; | ||
export * from './fileCollectionFromZip'; | ||
export * from './fileCollectionFromWebservice'; | ||
export * from './fileCollectionFromFileList'; | ||
export * from './groupFiles'; | ||
export * from './PartialFile'; | ||
export * from './FileCollection'; | ||
export * from './FileCollectionItem'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -1,8 +0,8 @@ | ||
export * from './fileListFromPath'; | ||
export * from './fileListFromZip'; | ||
export * from './fileListFromWebservice'; | ||
export * from './fileListUnzip'; | ||
export * from './fileListUngzip'; | ||
export * from './fileCollectionFromPath'; | ||
export * from './fileCollectionFromZip'; | ||
export * from './fileCollectionFromWebservice'; | ||
export * from './fileCollectionFromFileList'; | ||
export * from './groupFiles'; | ||
export * from './PartialFile'; | ||
export * from './FileCollection'; | ||
export * from './FileCollectionItem'; | ||
//# sourceMappingURL=index.js.map |
@@ -1,3 +0,3 @@ | ||
import { PartialFile } from './PartialFile'; | ||
export declare function ungzipStream(file: PartialFile): ReadableStream<any>; | ||
import { FileCollectionItem } from './FileCollectionItem'; | ||
export declare function ungzipStream(file: FileCollectionItem): ReadableStream<any>; | ||
//# sourceMappingURL=ungzipStream.browser.d.ts.map |
@@ -1,4 +0,3 @@ | ||
/// <reference types="node" /> | ||
import { PartialFile } from './PartialFile'; | ||
export declare function ungzipStream(file: PartialFile): import("zlib").Gunzip; | ||
import { FileCollectionItem } from './FileCollectionItem'; | ||
export declare function ungzipStream(file: FileCollectionItem): any; | ||
//# sourceMappingURL=ungzipStream.d.ts.map |
import { createGunzip } from 'zlib'; | ||
export function ungzipStream(file) { | ||
//@ts-expect-error Should fix this definition | ||
return file.stream().pipe(createGunzip()); | ||
} | ||
//# sourceMappingURL=ungzipStream.js.map |
@@ -1,8 +0,9 @@ | ||
import { PartialFile, PartialFileList } from './PartialFile'; | ||
import { FileCollection } from './FileCollection'; | ||
import { FileCollectionItem } from './FileCollectionItem'; | ||
export declare type StringObject = Record<string, string>; | ||
declare type GroupByOption = 'baseDir' | 'extension' | 'filename' | ((file?: PartialFile, fileInfo?: StringObject) => string); | ||
interface GroupOfFiles { | ||
declare type GroupByOption = 'baseDir' | 'extension' | 'filename' | ((file?: FileCollectionItem, fileInfo?: StringObject) => string); | ||
interface GroupOfFileCollection { | ||
meta: StringObject; | ||
key: string; | ||
fileList: PartialFileList; | ||
fileCollection: FileCollection; | ||
} | ||
@@ -20,4 +21,4 @@ export interface GroupFilesOptions { | ||
} | ||
export declare function groupFiles(fileList: PartialFileList, options?: GroupFilesOptions): GroupOfFiles[]; | ||
export declare function groupFiles(fileCollection: FileCollection, options?: GroupFilesOptions): GroupOfFileCollection[]; | ||
export {}; | ||
//# sourceMappingURL=groupFiles.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.groupFiles = void 0; | ||
function groupFiles(fileList, options = {}) { | ||
const FileCollection_1 = require("./FileCollection"); | ||
function groupFiles(fileCollection, options = {}) { | ||
const { groupBy = 'baseDir', meta } = options; | ||
const fileCollectionItems = fileCollection.items; | ||
let results = {}; | ||
for (const file of fileList) { | ||
for (const file of fileCollectionItems) { | ||
const key = getKey(file, groupBy); | ||
@@ -13,8 +15,12 @@ if (!results[key]) { | ||
key, | ||
fileList: [], | ||
fileCollectionItems: [], | ||
}; | ||
} | ||
results[key].fileList.push(file); | ||
results[key].fileCollectionItems.push(file); | ||
} | ||
return Object.keys(results).map((key) => results[key]); | ||
return Object.keys(results).map((key) => ({ | ||
meta: results[key].meta, | ||
key, | ||
fileCollection: new FileCollection_1.FileCollection(results[key].fileCollectionItems), | ||
})); | ||
} | ||
@@ -50,9 +56,7 @@ exports.groupFiles = groupFiles; | ||
return { | ||
baseDir: file.webkitRelativePath.replace(/\/[^/]*$/, ''), | ||
extension: file.webkitRelativePath.replace(/^.*\./, ''), | ||
filename: file.webkitRelativePath | ||
.replace(/^.*\//, '') | ||
.replace(/.[^.]*$/, ''), | ||
baseDir: file.relativePath.replace(/\/[^/]*$/, ''), | ||
extension: file.relativePath.replace(/^.*\./, ''), | ||
filename: file.relativePath.replace(/^.*\//, '').replace(/.[^.]*$/, ''), | ||
}; | ||
} | ||
//# sourceMappingURL=groupFiles.js.map |
@@ -1,8 +0,8 @@ | ||
export * from './fileListFromPath'; | ||
export * from './fileListFromZip'; | ||
export * from './fileListFromWebservice'; | ||
export * from './fileListUnzip'; | ||
export * from './fileListUngzip'; | ||
export * from './fileCollectionFromPath'; | ||
export * from './fileCollectionFromZip'; | ||
export * from './fileCollectionFromWebservice'; | ||
export * from './fileCollectionFromFileList'; | ||
export * from './groupFiles'; | ||
export * from './PartialFile'; | ||
export * from './FileCollection'; | ||
export * from './FileCollectionItem'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -17,9 +17,9 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./fileListFromPath"), exports); | ||
__exportStar(require("./fileListFromZip"), exports); | ||
__exportStar(require("./fileListFromWebservice"), exports); | ||
__exportStar(require("./fileListUnzip"), exports); | ||
__exportStar(require("./fileListUngzip"), exports); | ||
__exportStar(require("./fileCollectionFromPath"), exports); | ||
__exportStar(require("./fileCollectionFromZip"), exports); | ||
__exportStar(require("./fileCollectionFromWebservice"), exports); | ||
__exportStar(require("./fileCollectionFromFileList"), exports); | ||
__exportStar(require("./groupFiles"), exports); | ||
__exportStar(require("./PartialFile"), exports); | ||
__exportStar(require("./FileCollection"), exports); | ||
__exportStar(require("./FileCollectionItem"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -1,3 +0,3 @@ | ||
import { PartialFile } from './PartialFile'; | ||
export declare function ungzipStream(file: PartialFile): ReadableStream<any>; | ||
import { FileCollectionItem } from './FileCollectionItem'; | ||
export declare function ungzipStream(file: FileCollectionItem): ReadableStream<any>; | ||
//# sourceMappingURL=ungzipStream.browser.d.ts.map |
@@ -1,4 +0,3 @@ | ||
/// <reference types="node" /> | ||
import { PartialFile } from './PartialFile'; | ||
export declare function ungzipStream(file: PartialFile): import("zlib").Gunzip; | ||
import { FileCollectionItem } from './FileCollectionItem'; | ||
export declare function ungzipStream(file: FileCollectionItem): any; | ||
//# sourceMappingURL=ungzipStream.d.ts.map |
@@ -6,2 +6,3 @@ "use strict"; | ||
function ungzipStream(file) { | ||
//@ts-expect-error Should fix this definition | ||
return file.stream().pipe((0, zlib_1.createGunzip)()); | ||
@@ -8,0 +9,0 @@ } |
{ | ||
"name": "filelist-utils", | ||
"version": "0.9.1", | ||
"description": "Create a FileList from a path or a zip file", | ||
"version": "0.10.0", | ||
"description": "Create a FileCollection from a path or a zip file", | ||
"main": "./lib/index.js", | ||
@@ -9,5 +9,5 @@ "module": "./lib-esm/index.js", | ||
"browser": { | ||
"./lib-esm/fileListFromPath.js": "./lib-esm/fileListFromPath.browser.js", | ||
"./lib-esm/fileCollectionFromPath.js": "./lib-esm/fileCollectionFromPath.browser.js", | ||
"./lib-esm/ungzipStream.js": "./lib-esm/ungzipStream.browser.js", | ||
"./lib/fileListFromPath.js": "./lib/fileListFromPath.browser.js", | ||
"./lib/fileCollectionFromPath.js": "./lib/fileCollectionFromPath.browser.js", | ||
"./lib/ungzipStream.js": "./lib/ungzipStream.browser.js" | ||
@@ -24,2 +24,3 @@ }, | ||
"scripts": { | ||
"build": "npm run tsc-esm && cheminfo-build --entry lib-esm/index.js --root FileListUtil", | ||
"check-types": "tsc --noEmit", | ||
@@ -51,3 +52,4 @@ "clean": "rimraf lib lib-esm", | ||
"@types/pako": "^2.0.0", | ||
"eslint": "^8.23.1", | ||
"cheminfo-build": "^1.1.11", | ||
"eslint": "^8.24.0", | ||
"eslint-config-cheminfo-typescript": "^11.1.0", | ||
@@ -54,0 +56,0 @@ "jest": "^29.0.3", |
@@ -8,8 +8,24 @@ # filelist-utils | ||
Create a FileList from a path or a zip. | ||
Create a FileCollection from various sources. | ||
In order to make compatible code between the browser and node and to allow drag and drop of a folder it is useful to deal with [FileList](https://developer.mozilla.org/en-US/docs/Web/API/FileList) (that implements an Iterator of [File](https://developer.mozilla.org/en-US/docs/Web/API/File)). | ||
## Introduction | ||
This library allows to create such a [FileList](https://developer.mozilla.org/en-US/docs/Web/API/FileList) from a directory path (only available in nodejs) or from a zip file (available from nodejs and the browser). | ||
In order to make compatible code between the browser and node and to allow drag and drop of a folder it is useful to an abstraction level. | ||
This package allows to create a `FileCollection` from various sources: | ||
- a [FileList](https://developer.mozilla.org/en-US/docs/Web/API/FileList) (that implements an Iterator of [File](https://developer.mozilla.org/en-US/docs/Web/API/File)). | ||
- a relative path (with its basedir) | ||
- a webservice that returns a JSON containing an array of object that has the following properties: `relativePath`, `name`, `lastModified`, `size` | ||
A `FileCollection` has an iterator on `FileCollectionItem` that has the following properties: | ||
- lastModified: number; | ||
- name: string; | ||
- relativePath: string; | ||
- size: number; | ||
- arrayBuffer(): Promise<ArrayBuffer>; | ||
- stream(): ReadableStream<Uint8Array>; | ||
- text(): Promise<string>; | ||
## Installation | ||
@@ -22,12 +38,12 @@ | ||
```js | ||
import { fileListFromPath } from 'filelist-utils'; | ||
import { fileCollectionFromPath } from 'filelist-utils'; | ||
const fileList = fileListFromPath(join(__dirname)); | ||
const fileCollection = fileCollectionFromPath(__dirname); | ||
``` | ||
```js | ||
import { fileListFromZip } from 'filelist-utils'; | ||
import { fileCollectionFromZip } from 'filelist-utils'; | ||
const zip = readFileSync(join(__dirname, 'test.zip')); | ||
const fileList = fileListFromZip(zip); | ||
const fileCollection = fileCollectionFromZip(zip); | ||
``` | ||
@@ -34,0 +50,0 @@ |
@@ -1,2 +0,3 @@ | ||
import { PartialFile, PartialFileList } from './PartialFile'; | ||
import { FileCollection } from './FileCollection'; | ||
import { FileCollectionItem } from './FileCollectionItem'; | ||
@@ -9,8 +10,14 @@ export type StringObject = Record<string, string>; | ||
| 'filename' | ||
| ((file?: PartialFile, fileInfo?: StringObject) => string); | ||
| ((file?: FileCollectionItem, fileInfo?: StringObject) => string); | ||
interface GroupOfFileCollection { | ||
meta: StringObject; | ||
key: string; | ||
fileCollection: FileCollection; | ||
} | ||
interface GroupOfFiles { | ||
meta: StringObject; | ||
key: string; | ||
fileList: PartialFileList; | ||
fileCollectionItems: FileCollectionItem[]; | ||
} | ||
@@ -31,10 +38,12 @@ | ||
export function groupFiles( | ||
fileList: PartialFileList, | ||
fileCollection: FileCollection, | ||
options: GroupFilesOptions = {}, | ||
) { | ||
): GroupOfFileCollection[] { | ||
const { groupBy = 'baseDir', meta } = options; | ||
const fileCollectionItems: FileCollectionItem[] = fileCollection.items; | ||
let results: Record<string, GroupOfFiles> = {}; | ||
for (const file of fileList) { | ||
for (const file of fileCollectionItems) { | ||
const key = getKey(file, groupBy); | ||
@@ -45,12 +54,16 @@ if (!results[key]) { | ||
key, | ||
fileList: [], | ||
fileCollectionItems: [], | ||
}; | ||
} | ||
results[key].fileList.push(file); | ||
results[key].fileCollectionItems.push(file); | ||
} | ||
return Object.keys(results).map((key) => results[key]); | ||
return Object.keys(results).map((key) => ({ | ||
meta: results[key].meta, | ||
key, | ||
fileCollection: new FileCollection(results[key].fileCollectionItems), | ||
})); | ||
} | ||
function getMeta(key: string, meta?: RegExp) { | ||
function getMeta(key: string, meta?: RegExp): Record<string, string> { | ||
if (!meta) return {}; | ||
@@ -62,3 +75,3 @@ const matcher = key.match(meta); | ||
function getKey(file: PartialFile, groupBy: GroupByOption) { | ||
function getKey(file: FileCollectionItem, groupBy: GroupByOption) { | ||
if (typeof groupBy === 'string') { | ||
@@ -81,10 +94,8 @@ const fileInfo = getFileInfo(file); | ||
function getFileInfo(file: PartialFile) { | ||
function getFileInfo(file: FileCollectionItem) { | ||
return { | ||
baseDir: file.webkitRelativePath.replace(/\/[^/]*$/, ''), | ||
extension: file.webkitRelativePath.replace(/^.*\./, ''), | ||
filename: file.webkitRelativePath | ||
.replace(/^.*\//, '') | ||
.replace(/.[^.]*$/, ''), | ||
baseDir: file.relativePath.replace(/\/[^/]*$/, ''), | ||
extension: file.relativePath.replace(/^.*\./, ''), | ||
filename: file.relativePath.replace(/^.*\//, '').replace(/.[^.]*$/, ''), | ||
}; | ||
} |
@@ -1,7 +0,7 @@ | ||
export * from './fileListFromPath'; | ||
export * from './fileListFromZip'; | ||
export * from './fileListFromWebservice'; | ||
export * from './fileListUnzip'; | ||
export * from './fileListUngzip'; | ||
export * from './fileCollectionFromPath'; | ||
export * from './fileCollectionFromZip'; | ||
export * from './fileCollectionFromWebservice'; | ||
export * from './fileCollectionFromFileList'; | ||
export * from './groupFiles'; | ||
export * from './PartialFile'; | ||
export * from './FileCollection'; | ||
export * from './FileCollectionItem'; |
import { ungzip } from 'pako'; | ||
import { PartialFile } from './PartialFile'; | ||
import { FileCollectionItem } from './FileCollectionItem'; | ||
export function ungzipStream(file: PartialFile) { | ||
export function ungzipStream(file: FileCollectionItem) { | ||
return new ReadableStream({ | ||
@@ -7,0 +7,0 @@ start(controller) { |
import { createGunzip } from 'zlib'; | ||
import { PartialFile } from './PartialFile'; | ||
import { FileCollectionItem } from './FileCollectionItem'; | ||
export function ungzipStream(file: PartialFile) { | ||
export function ungzipStream(file: FileCollectionItem) { | ||
//@ts-expect-error Should fix this definition | ||
return file.stream().pipe(createGunzip()); | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 2 instances in 1 package
131321
156
2030
61
0
12
4