From e27e522a5f10a1017d55d0af18fe4d081128eea9 Mon Sep 17 00:00:00 2001 From: Muhammad Fawwaz Orabi Date: Fri, 15 Sep 2017 15:31:32 +0300 Subject: [PATCH 1/3] Update JSZip * Remove deprecated methods * Add `generateNodeStream` * Add `version` * Add missing properties on JSZipObject * Stricter typing for `async`, `generateAsync`. --- types/jszip/index.d.ts | 144 ++++++++++++++++++++++--------------- types/jszip/jszip-tests.ts | 3 +- 2 files changed, 87 insertions(+), 60 deletions(-) diff --git a/types/jszip/index.d.ts b/types/jszip/index.d.ts index b44d2bc538..160c236ece 100644 --- a/types/jszip/index.d.ts +++ b/types/jszip/index.d.ts @@ -1,7 +1,11 @@ // Type definitions for JSZip // Project: http://stuk.github.com/jszip/ -// Definitions by: mzeiher +// Definitions by: mzeiher , forabi // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + + +/// interface JSZip { files: {[key: string]: JSZipObject}; @@ -26,11 +30,11 @@ interface JSZip { * Add a file to the archive * * @param path Relative path to file - * @param content Content of the file + * @param data Content of the file * @param options Optional information about the file * @return JSZip object */ - file(path: string, data: any, options?: JSZipFileOptions): JSZip; + file(path: string, data: InputByType[T] | Promise, options?: JSZipFileOptions): JSZip; /** * Return an new JSZip instance with the given folder as root @@ -72,26 +76,22 @@ interface JSZip { remove(path: string): JSZip; /** - * @deprecated since version 3.0 - * @see {@link generateAsync} - * http://stuk.github.io/jszip/documentation/upgrade_guide.html + * Generates a new archive asynchronously + * + * @param options Optional options for the generator + * @param onUpdate The optional function called on each internal update with the metadata. + * @return The serialized archive */ - generate(options?: JSZipGeneratorOptions): any; + generateAsync(options?: JSZipGeneratorOptions, onUpdate?: OnUpdateCallback): Promise; /** * Generates a new archive asynchronously * * @param options Optional options for the generator - * @return The serialized archive + * @param onUpdate The optional function called on each internal update with the metadata. + * @return A Node.js `ReadableStream` */ - generateAsync(options?: JSZipGeneratorOptions, onUpdate?: Function): Promise; - - /** - * @deprecated since version 3.0 - * @see {@link loadAsync} - * http://stuk.github.io/jszip/documentation/upgrade_guide.html - */ - load(): void; + generateNodeStream(options?: JSZipGeneratorOptions<'nodebuffer'>, onUpdate?: OnUpdateCallback): NodeJS.ReadStream; /** * Deserialize zip file asynchronously @@ -100,44 +100,65 @@ interface JSZip { * @param options Options for deserializing * @return Returns promise */ - loadAsync(data: any, options?: JSZipLoadOptions): Promise; + loadAsync(data: InputFileFormat, options?: JSZipLoadOptions): Promise; } -type Serialization = ("string" | "text" | "base64" | "binarystring" | "uint8array" | - "arraybuffer" | "blob" | "nodebuffer"); +type Compression = 'STORE' | 'DEFLATE'; + +type ReadFileFormat = 'base64' | 'string' | 'text' | 'binarystring' | 'array' | 'unit8array' | 'arraybuffer' | 'blob' | 'nodebuffer'; + +type InputByType = { + base64: string; + string: string; + text: string; + binarystring: string; + array: Array; + unit8array: Uint8Array; + arraybuffer: ArrayBuffer; + blob: Blob; +} + +type InputType = keyof InputByType; + +type InputFileFormat = string | ArrayBuffer | Uint8Array | Buffer | Blob | NodeJS.ReadableStream; + +type OutputByType = { + base64: string; + text: string; + binarystring: string; + array: Array; + unit8array: Uint8Array; + arraybuffer: ArrayBuffer; + blob: Blob; + nodebuffer: Buffer; +} +type OutputType = keyof OutputByType; + +type Metadata = { + percent: number; + currentFile: string; +}; + +type OnUpdateCallback = (metadata: Metadata) => void; interface JSZipObject { name: string; dir: boolean; date: Date; comment: string; + /** The UNIX permissions of the file, if any. */ + unixPermissions: number | string | null; + /** The UNIX permissions of the file, if any. */ + dosPermissions: number | null; options: JSZipObjectOptions; /** * Prepare the content in the asked type. * @param {String} type the type of the result. - * @param {Function} onUpdate a function to call on each internal update. + * @param {OnUpdateCallback} onUpdate a function to call on each internal update. * @return Promise the promise of the result. */ - async(type: Serialization, onUpdate?: Function): Promise; - - /** - * @deprecated since version 3.0 - */ - asText(): void; - /** - * @deprecated since version 3.0 - */ - asBinary(): void; - /** - * @deprecated since version 3.0 - */ - asArrayBuffer(): void; - /** - * @deprecated since version 3.0 - */ - asUint8Array(): void; - //asNodeBuffer(): void; + async(type: T, onUpdate?: OnUpdateCallback): Promise; } interface JSZipFileOptions { @@ -149,37 +170,38 @@ interface JSZipFileOptions { optimizedBinaryString?: boolean; createFolders?: boolean; dir?: boolean; + + /** 6 bits number. The DOS permissions of the file, if any. */ + dosPermissions?: number | null; + /** + * 16 bits number. The UNIX permissions of the file, if any. + * Also accepts a `string` representing the octal value: `"644"`, `"755"`, etc. + */ + unixPermissions?: number | string | null; } interface JSZipObjectOptions { - /** deprecated */ - base64: boolean; - /** deprecated */ - binary: boolean; - /** deprecated */ - dir: boolean; - /** deprecated */ - date: Date; - compression: string; + compression: Compression; } -interface JSZipGeneratorOptions { - /** deprecated */ - base64?: boolean; - /** DEFLATE or STORE */ - compression?: string; - /** base64 (default), string, uint8array, arraybuffer, blob */ - type?: string; +interface JSZipGeneratorOptions { + compression?: Compression; + compressionOptions?: null | { + level: number; + } + type?: T; comment?: string; /** * mime-type for the generated file. * Useful when you need to generate a file with a different extension, ie: “.ods”. + * @default 'application/zip' */ mimeType?: string; - /** streaming uses less memory */ + encodeFileName?: (filename: string) => string; + /** Stream the files and create file descriptors */ streamFiles?: boolean; /** DOS (default) or UNIX */ - platform?: string; + platform?: 'DOS' | 'UNIX'; } interface JSZipLoadOptions { @@ -208,7 +230,7 @@ declare var JSZip: { * @param data Serialized zip archive * @param options Description of the serialized zip archive */ - (data: any, options?: JSZipLoadOptions): JSZip; + (data: InputFileFormat, options?: JSZipLoadOptions): JSZip; /** * Create JSZip instance @@ -221,10 +243,14 @@ declare var JSZip: { * @param data Serialized zip archive * @param options Description of the serialized zip archive */ - new (data: any, options?: JSZipLoadOptions): JSZip; + new (data: InputFileFormat, options?: JSZipLoadOptions): JSZip; prototype: JSZip; support: JSZipSupport; + external: { + Promise: PromiseConstructorLike; + }; + version: string; } declare module "jszip" { diff --git a/types/jszip/jszip-tests.ts b/types/jszip/jszip-tests.ts index f7a4b9f10f..afbe3cb585 100644 --- a/types/jszip/jszip-tests.ts +++ b/types/jszip/jszip-tests.ts @@ -14,7 +14,7 @@ function createTestZip(): JSZip { return zip } -function filterWithFileAsync(zip: JSZip, as: Serialization, +function filterWithFileAsync(zip: JSZip, as: OutputType, cb: (relativePath: string, file: JSZipObject, value: any) => boolean) : Promise { var promises: Promise[] = []; @@ -124,6 +124,7 @@ function testJSZipRemove() { }).catch((e: any) => log(SEVERITY.ERROR, e)); } + function log(severity:number, message: any) { var log = ""; switch(severity) { From 2b36dbf78518af249b130cfd648a3e5e8f65a3f2 Mon Sep 17 00:00:00 2001 From: Muhammad Fawwaz Orabi Date: Fri, 15 Sep 2017 16:15:17 +0300 Subject: [PATCH 2/3] Fix all linting issues --- types/jszip/index.d.ts | 306 ++++++++++++++++++------------------- types/jszip/jszip-tests.ts | 91 +++++------ types/jszip/tsconfig.json | 7 +- types/jszip/tslint.json | 6 + 4 files changed, 208 insertions(+), 202 deletions(-) create mode 100644 types/jszip/tslint.json diff --git a/types/jszip/index.d.ts b/types/jszip/index.d.ts index 160c236ece..98b66aed3a 100644 --- a/types/jszip/index.d.ts +++ b/types/jszip/index.d.ts @@ -1,14 +1,139 @@ -// Type definitions for JSZip +// Type definitions for JSZip 3.1 // Project: http://stuk.github.com/jszip/ // Definitions by: mzeiher , forabi // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.2 - +// TypeScript Version: 2.3 /// +interface JSZipSupport { + arraybuffer: boolean; + uint8array: boolean; + blob: boolean; + nodebuffer: boolean; +} + +type Compression = 'STORE' | 'DEFLATE'; + +interface Metadata { + percent: number; + currentFile: string; +} + +type OnUpdateCallback = (metadata: Metadata) => void; + +interface InputByType { + base64: string; + string: string; + text: string; + binarystring: string; + array: number[]; + unit8array: Uint8Array; + arraybuffer: ArrayBuffer; + blob: Blob; +} + +interface OutputByType { + base64: string; + text: string; + binarystring: string; + array: number[]; + unit8array: Uint8Array; + arraybuffer: ArrayBuffer; + blob: Blob; + nodebuffer: Buffer; +} + +type InputFileFormat = string | ArrayBuffer | Uint8Array | Buffer | Blob | NodeJS.ReadableStream; + +declare namespace JSZip { + type InputType = keyof InputByType; + + type OutputType = keyof OutputByType; + + interface JSZipObject { + name: string; + dir: boolean; + date: Date; + comment: string; + /** The UNIX permissions of the file, if any. */ + unixPermissions: number | string | null; + /** The UNIX permissions of the file, if any. */ + dosPermissions: number | null; + options: JSZipObjectOptions; + + /** + * Prepare the content in the asked type. + * @param {String} type the type of the result. + * @param {OnUpdateCallback} onUpdate a function to call on each internal update. + * @return Promise the promise of the result. + */ + async(type: T, onUpdate?: OnUpdateCallback): Promise; + } + + interface JSZipFileOptions { + /** Set to `true` if the data is `base64` encoded. For example image data from a `` element. Plain text and HTML do not need this option. */ + base64?: boolean; + /** Set to `true` if the data should be treated as raw content, `false` if this is a text. If `base64` is used, + * this defaults to `true`, if the data is not a `string`, this will be set to `true`. + */ + binary?: boolean; + /** + * The last modification date, defaults to the current date. + */ + date?: Date; + compression?: string; + comment?: string; + /** Set to `true` if (and only if) the input is a "binary string" and has already been prepared with a `0xFF` mask. */ + optimizedBinaryString?: boolean; + /** Set to `true` if folders in the file path should be automatically created, otherwise there will only be virtual folders that represent the path to the file. */ + createFolders?: boolean; + /** Set to `true` if this is a directory and content should be ignored. */ + dir?: boolean; + + /** 6 bits number. The DOS permissions of the file, if any. */ + dosPermissions?: number | null; + /** + * 16 bits number. The UNIX permissions of the file, if any. + * Also accepts a `string` representing the octal value: `"644"`, `"755"`, etc. + */ + unixPermissions?: number | string | null; + } + + interface JSZipObjectOptions { + compression: Compression; + } + + interface JSZipGeneratorOptions { + compression?: Compression; + compressionOptions?: null | { + level: number; + }; + type?: T; + comment?: string; + /** + * mime-type for the generated file. + * Useful when you need to generate a file with a different extension, ie: “.ods”. + * @default 'application/zip' + */ + mimeType?: string; + encodeFileName?(filename: string): string; + /** Stream the files and create file descriptors */ + streamFiles?: boolean; + /** DOS (default) or UNIX */ + platform?: 'DOS' | 'UNIX'; + } + + interface JSZipLoadOptions { + base64?: boolean; + checkCRC32?: boolean; + optimizedBinaryString?: boolean; + createFolders?: boolean; + } +} + interface JSZip { - files: {[key: string]: JSZipObject}; + files: {[key: string]: JSZip.JSZipObject}; /** * Get a file from the archive @@ -16,7 +141,7 @@ interface JSZip { * @param Path relative path to file * @return File matching path, null if no file found */ - file(path: string): JSZipObject; + file(path: string): JSZip.JSZipObject; /** * Get files matching a RegExp from archive @@ -24,7 +149,7 @@ interface JSZip { * @param path RegExp to match * @return Return all matching files or an empty array */ - file(path: RegExp): JSZipObject[]; + file(path: RegExp): JSZip.JSZipObject[]; /** * Add a file to the archive @@ -34,7 +159,8 @@ interface JSZip { * @param options Optional information about the file * @return JSZip object */ - file(path: string, data: InputByType[T] | Promise, options?: JSZipFileOptions): JSZip; + file(path: string, data: InputByType[T] | Promise, options?: JSZip.JSZipFileOptions): this; + file(path: string, data: null, options?: JSZip.JSZipFileOptions & { dir: true }): this; /** * Return an new JSZip instance with the given folder as root @@ -50,14 +176,14 @@ interface JSZip { * @param name RegExp to match * @return New array of JSZipFile objects which match the RegExp */ - folder(name: RegExp): JSZipObject[]; + folder(name: RegExp): JSZip.JSZipObject[]; /** * Call a callback function for each entry at this folder level. * * @param callback function */ - forEach(callback: (relativePath: string, file: JSZipObject) => void): void; + forEach(callback: (relativePath: string, file: JSZip.JSZipObject) => void): void; /** * Get all files wchich match the given filter function @@ -65,7 +191,7 @@ interface JSZip { * @param predicate Filter function * @return Array of matched elements */ - filter(predicate: (relativePath: string, file: JSZipObject) => boolean): JSZipObject[]; + filter(predicate: (relativePath: string, file: JSZip.JSZipObject) => boolean): JSZip.JSZipObject[]; /** * Removes the file or folder from the archive @@ -82,7 +208,7 @@ interface JSZip { * @param onUpdate The optional function called on each internal update with the metadata. * @return The serialized archive */ - generateAsync(options?: JSZipGeneratorOptions, onUpdate?: OnUpdateCallback): Promise; + generateAsync(options?: JSZip.JSZipGeneratorOptions, onUpdate?: OnUpdateCallback): Promise; /** * Generates a new archive asynchronously @@ -91,7 +217,7 @@ interface JSZip { * @param onUpdate The optional function called on each internal update with the metadata. * @return A Node.js `ReadableStream` */ - generateNodeStream(options?: JSZipGeneratorOptions<'nodebuffer'>, onUpdate?: OnUpdateCallback): NodeJS.ReadStream; + generateNodeStream(options?: JSZip.JSZipGeneratorOptions<'nodebuffer'>, onUpdate?: OnUpdateCallback): NodeJS.ReadStream; /** * Deserialize zip file asynchronously @@ -100,150 +226,22 @@ interface JSZip { * @param options Options for deserializing * @return Returns promise */ - loadAsync(data: InputFileFormat, options?: JSZipLoadOptions): Promise; -} + loadAsync(data: InputFileFormat, options?: JSZip.JSZipLoadOptions): Promise; -type Compression = 'STORE' | 'DEFLATE'; - -type ReadFileFormat = 'base64' | 'string' | 'text' | 'binarystring' | 'array' | 'unit8array' | 'arraybuffer' | 'blob' | 'nodebuffer'; - -type InputByType = { - base64: string; - string: string; - text: string; - binarystring: string; - array: Array; - unit8array: Uint8Array; - arraybuffer: ArrayBuffer; - blob: Blob; -} - -type InputType = keyof InputByType; - -type InputFileFormat = string | ArrayBuffer | Uint8Array | Buffer | Blob | NodeJS.ReadableStream; - -type OutputByType = { - base64: string; - text: string; - binarystring: string; - array: Array; - unit8array: Uint8Array; - arraybuffer: ArrayBuffer; - blob: Blob; - nodebuffer: Buffer; -} -type OutputType = keyof OutputByType; - -type Metadata = { - percent: number; - currentFile: string; -}; - -type OnUpdateCallback = (metadata: Metadata) => void; - -interface JSZipObject { - name: string; - dir: boolean; - date: Date; - comment: string; - /** The UNIX permissions of the file, if any. */ - unixPermissions: number | string | null; - /** The UNIX permissions of the file, if any. */ - dosPermissions: number | null; - options: JSZipObjectOptions; - - /** - * Prepare the content in the asked type. - * @param {String} type the type of the result. - * @param {OnUpdateCallback} onUpdate a function to call on each internal update. - * @return Promise the promise of the result. - */ - async(type: T, onUpdate?: OnUpdateCallback): Promise; -} - -interface JSZipFileOptions { - base64?: boolean; - binary?: boolean; - date?: Date; - compression?: string; - comment?: string; - optimizedBinaryString?: boolean; - createFolders?: boolean; - dir?: boolean; - - /** 6 bits number. The DOS permissions of the file, if any. */ - dosPermissions?: number | null; - /** - * 16 bits number. The UNIX permissions of the file, if any. - * Also accepts a `string` representing the octal value: `"644"`, `"755"`, etc. - */ - unixPermissions?: number | string | null; -} - -interface JSZipObjectOptions { - compression: Compression; -} - -interface JSZipGeneratorOptions { - compression?: Compression; - compressionOptions?: null | { - level: number; - } - type?: T; - comment?: string; - /** - * mime-type for the generated file. - * Useful when you need to generate a file with a different extension, ie: “.ods”. - * @default 'application/zip' - */ - mimeType?: string; - encodeFileName?: (filename: string) => string; - /** Stream the files and create file descriptors */ - streamFiles?: boolean; - /** DOS (default) or UNIX */ - platform?: 'DOS' | 'UNIX'; -} - -interface JSZipLoadOptions { - base64?: boolean; - checkCRC32?: boolean; - optimizedBinaryString?: boolean; - createFolders?: boolean; -} - -interface JSZipSupport { - arraybuffer: boolean; - uint8array: boolean; - blob: boolean; - nodebuffer: boolean; -} - -declare var JSZip: { /** * Create JSZip instance */ + + /** + * Create JSZip instance + * If no parameters given an empty zip archive will be created + * + * @param data Serialized zip archive + * @param options Description of the serialized zip archive + */ + new (data?: InputFileFormat, options?: JSZip.JSZipLoadOptions): this; + (): JSZip; - /** - * Create JSZip instance - * If no parameters given an empty zip archive will be created - * - * @param data Serialized zip archive - * @param options Description of the serialized zip archive - */ - (data: InputFileFormat, options?: JSZipLoadOptions): JSZip; - - /** - * Create JSZip instance - */ - new (): JSZip; - /** - * Create JSZip instance - * If no parameters given an empty zip archive will be created - * - * @param data Serialized zip archive - * @param options Description of the serialized zip archive - */ - new (data: InputFileFormat, options?: JSZipLoadOptions): JSZip; prototype: JSZip; support: JSZipSupport; @@ -253,6 +251,6 @@ declare var JSZip: { version: string; } -declare module "jszip" { - export = JSZip; -} +declare var JSZip: JSZip; + +export = JSZip; diff --git a/types/jszip/jszip-tests.ts b/types/jszip/jszip-tests.ts index afbe3cb585..2d5ab19305 100644 --- a/types/jszip/jszip-tests.ts +++ b/types/jszip/jszip-tests.ts @@ -1,32 +1,34 @@ -var SEVERITY = { +import * as JSZip from 'jszip'; + +const SEVERITY = { DEBUG: 0, INFO: 1, WARN: 2, ERROR: 3, FATAL: 4 -} +}; function createTestZip(): JSZip { - var zip = new JSZip(); + const zip = new JSZip(); zip.file("test.txt", "test string"); zip.file("test", null, { dir: true }); zip.file("test/test.txt", "test string"); - return zip + return zip; } -function filterWithFileAsync(zip: JSZip, as: OutputType, - cb: (relativePath: string, file: JSZipObject, value: any) => boolean) - : Promise { - var promises: Promise[] = []; - var promiseIndices: {[key: string]: number} = {}; - zip.forEach((relativePath: string, file: JSZipObject) => { - var promise = file.async(as); +function filterWithFileAsync(zip: JSZip, as: JSZip.OutputType, + cb: (relativePath: string, file: JSZip.JSZipObject, value: any) => boolean) + : Promise { + const promises: Array> = []; + const promiseIndices: {[key: string]: number} = {}; + zip.forEach((relativePath: string, file: JSZip.JSZipObject) => { + const promise = file.async(as); promiseIndices[file.name] = promises.length; promises.push(promise); }); - return Promise.all(promises).then(function(values: any[]) { - var filtered = zip.filter((relativePath: string, file: JSZipObject) => { - var index = promiseIndices[file.name]; + return Promise.all(promises).then((values: any[]) => { + const filtered = zip.filter((relativePath: string, file: JSZip.JSZipObject) => { + const index = promiseIndices[file.name]; return cb(relativePath, file, values[index]); }); return Promise.resolve(filtered); @@ -34,12 +36,12 @@ function filterWithFileAsync(zip: JSZip, as: OutputType, } function testJSZip() { - var zip = createTestZip(); - zip.generateAsync({compression: "DEFLATE", type: "base64"}).then(function(serializedZip: any) { - var newJszip = new JSZip(); + const zip = createTestZip(); + zip.generateAsync({compression: "DEFLATE", type: "base64"}).then((serializedZip: any) => { + const newJszip = new JSZip(); return newJszip.loadAsync(serializedZip, {base64: true/*, checkCRC32: true*/}); - }).then(function(newJszip: JSZip) { - newJszip.file("test.txt").async('text').then(function(text: string) { + }).then((newJszip: JSZip) => { + newJszip.file("test.txt").async('text').then((text: string) => { if (text === "test string") { log(SEVERITY.INFO, "all ok"); } else { @@ -47,7 +49,7 @@ function testJSZip() { } }).catch((e: any) => log(SEVERITY.ERROR, e)); - newJszip.file("test/test.txt").async('text').then(function(text: string) { + newJszip.file("test/test.txt").async('text').then((text: string) => { if (text === "test string") { log(SEVERITY.INFO, "all ok"); } else { @@ -55,20 +57,20 @@ function testJSZip() { } }).catch((e: any) => log(SEVERITY.ERROR, e)); - var folder = newJszip.folder("test"); - folder.file("test.txt").async('text').then(function(text: string) { - if (text == "test string") { + const folder = newJszip.folder("test"); + folder.file("test.txt").async('text').then((text: string) => { + if (text === "test string") { log(SEVERITY.INFO, "all ok"); } else { log(SEVERITY.ERROR, "wrong file"); } }).catch((e: any) => log(SEVERITY.ERROR, e)); - var folders = newJszip.folder(new RegExp("^test")); + const folders = newJszip.folder(new RegExp("^test")); - if (folders.length == 1) { + if (folders.length === 1) { log(SEVERITY.INFO, "all ok"); - if (folders[0].dir == true) { + if (folders[0].dir) { log(SEVERITY.INFO, "all ok"); } else { log(SEVERITY.ERROR, "wrong file"); @@ -77,11 +79,11 @@ function testJSZip() { log(SEVERITY.ERROR, "wrong number of folder"); } - var files = newJszip.file(new RegExp("^test")); - if (files.length == 2) { + const files = newJszip.file(new RegExp("^test")); + if (files.length === 2) { log(SEVERITY.INFO, "all ok"); - Promise.all([files[0].async('text'), files[1].async('text')]).then(function(texts: string[]) { - if (texts[0] == "test string" && texts[1] == 'test string') { + Promise.all([files[0].async('text'), files[1].async('text')]).then((texts: string[]) => { + if (texts[0] === "test string" && texts[1] === 'test string') { log(SEVERITY.INFO, "all ok"); } else { log(SEVERITY.ERROR, "wrong data in files"); @@ -91,32 +93,32 @@ function testJSZip() { log(SEVERITY.ERROR, "wrong number of files"); } - filterWithFileAsync(newJszip, 'text', (relativePath: string, file: JSZipObject, text: string) => { - if (text == "test string") { + filterWithFileAsync(newJszip, 'text', (relativePath: string, file: JSZip.JSZipObject, text: string) => { + if (text === "test string") { return true; } return false; - }).then(function(filterFiles: JSZipObject[]) { - if (filterFiles.length == 2) { + }).then((filterFiles: JSZip.JSZipObject[]) => { + if (filterFiles.length === 2) { log(SEVERITY.INFO, "all ok"); } else { log(SEVERITY.ERROR, "wrong number of files"); } }).catch((e: any) => log(SEVERITY.ERROR, e)); - }).catch((e: any)=> { console.error(e) }); + }).catch((e: any) => { console.error(e); }); } function testJSZipRemove() { - var newJszip = createTestZip(); + const newJszip = createTestZip(); newJszip.remove("test/test.txt"); - filterWithFileAsync(newJszip, 'text', (relativePath: string, file: JSZipObject, text: string) => { - if (text == "test string") { + filterWithFileAsync(newJszip, 'text', (relativePath: string, file: JSZip.JSZipObject, text: string) => { + if (text === "test string") { return true; } return false; - }).then(function(filterFiles: JSZipObject[]) { - if (filterFiles.length == 1) { + }).then((filterFiles: JSZip.JSZipObject[]) => { + if (filterFiles.length === 1) { log(SEVERITY.INFO, "all ok"); } else { log(SEVERITY.ERROR, "wrong number of files"); @@ -124,10 +126,9 @@ function testJSZipRemove() { }).catch((e: any) => log(SEVERITY.ERROR, e)); } - -function log(severity:number, message: any) { - var log = ""; - switch(severity) { +function log(severity: number, message: any) { + let log = ""; + switch (severity) { case 0: log += "[DEBUG] "; break; @@ -144,7 +145,7 @@ function log(severity:number, message: any) { log += "[FATAL] "; break; default: - log += "[INFO]" + log += "[INFO]"; break; } console.log(log += message); diff --git a/types/jszip/tsconfig.json b/types/jszip/tsconfig.json index 02f0123b77..80c7836f06 100644 --- a/types/jszip/tsconfig.json +++ b/types/jszip/tsconfig.json @@ -1,13 +1,14 @@ { "compilerOptions": { "module": "commonjs", + "target": "es5", "lib": [ - "es6", + "es2015", "dom" ], "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": false, + "strictNullChecks": true, "baseUrl": "../", "typeRoots": [ "../" @@ -20,4 +21,4 @@ "index.d.ts", "jszip-tests.ts" ] -} \ No newline at end of file +} diff --git a/types/jszip/tslint.json b/types/jszip/tslint.json new file mode 100644 index 0000000000..71ee04c4e1 --- /dev/null +++ b/types/jszip/tslint.json @@ -0,0 +1,6 @@ +{ + "extends": "dtslint/dt.json", + "rules": { + "no-unnecessary-generics": false + } +} From 50fcb97a2fda6125fe18750e5e3a405d181fab50 Mon Sep 17 00:00:00 2001 From: Muhammad Fawwaz Orabi Date: Sat, 16 Sep 2017 19:49:11 +0300 Subject: [PATCH 3/3] More fixes --- types/jszip/index.d.ts | 7 ++++--- types/jszip/jszip-tests.ts | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/types/jszip/index.d.ts b/types/jszip/index.d.ts index 98b66aed3a..7ea6180080 100644 --- a/types/jszip/index.d.ts +++ b/types/jszip/index.d.ts @@ -44,7 +44,7 @@ interface OutputByType { nodebuffer: Buffer; } -type InputFileFormat = string | ArrayBuffer | Uint8Array | Buffer | Blob | NodeJS.ReadableStream; +type InputFileFormat = InputByType[keyof InputByType] | NodeJS.ReadableStream; declare namespace JSZip { type InputType = keyof InputByType; @@ -69,6 +69,7 @@ declare namespace JSZip { * @return Promise the promise of the result. */ async(type: T, onUpdate?: OnUpdateCallback): Promise; + nodeStream(type?: 'nodestream', onUpdate?: OnUpdateCallback): NodeJS.ReadableStream; } interface JSZipFileOptions { @@ -163,7 +164,7 @@ interface JSZip { file(path: string, data: null, options?: JSZip.JSZipFileOptions & { dir: true }): this; /** - * Return an new JSZip instance with the given folder as root + * Returns an new JSZip instance with the given folder as root * * @param name Name of the folder * @return New JSZip object with the given folder as root or null @@ -186,7 +187,7 @@ interface JSZip { forEach(callback: (relativePath: string, file: JSZip.JSZipObject) => void): void; /** - * Get all files wchich match the given filter function + * Get all files which match the given filter function * * @param predicate Filter function * @return Array of matched elements diff --git a/types/jszip/jszip-tests.ts b/types/jszip/jszip-tests.ts index 2d5ab19305..81ccda09db 100644 --- a/types/jszip/jszip-tests.ts +++ b/types/jszip/jszip-tests.ts @@ -37,7 +37,7 @@ function filterWithFileAsync(zip: JSZip, as: JSZip.OutputType, function testJSZip() { const zip = createTestZip(); - zip.generateAsync({compression: "DEFLATE", type: "base64"}).then((serializedZip: any) => { + zip.generateAsync({compression: "DEFLATE", type: "base64"}).then((serializedZip) => { const newJszip = new JSZip(); return newJszip.loadAsync(serializedZip, {base64: true/*, checkCRC32: true*/}); }).then((newJszip: JSZip) => {