From c365fcd0357679ade2d7403f13631fcd1e78a09c Mon Sep 17 00:00:00 2001 From: Mathis Zeiher Date: Fri, 5 Apr 2013 11:02:02 +0200 Subject: [PATCH] Added definitions for JSZip library --- jszip/jszip-test.ts | 132 ++++++++++++++++++++++++++++++++ jszip/jszip.d.ts | 183 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 315 insertions(+) create mode 100644 jszip/jszip-test.ts create mode 100644 jszip/jszip.d.ts diff --git a/jszip/jszip-test.ts b/jszip/jszip-test.ts new file mode 100644 index 0000000000..9222cbb5bc --- /dev/null +++ b/jszip/jszip-test.ts @@ -0,0 +1,132 @@ +/// +/// + +var SEVERITY = { + DEBUG: 0, + INFO: 1, + WARN: 2, + ERROR: 3, + FATAL: 4 +} + +function testJSZip() { + + var newJszip = new JSZip(); + + newJszip.file("test.txt", "test string"); + newJszip.file("test/test.txt", "test string"); + + var serializedZip = newJszip.generate({compression: "DEFLATE", type:"base64"}); + + newJszip = new JSZip(); + newJszip.load(serializedZip, {base64: true, checkCRC32: true}); + + if(newJszip.file("test.txt").data === "test string") { + log(SEVERITY.INFO, "all ok"); + } else { + log(SEVERITY.ERROR, "no matching file found"); + } + if(newJszip.file("test/test.txt").data === "test string") { + log(SEVERITY.INFO, "all ok"); + } else { + log(SEVERITY.ERROR, "no matching file found"); + } + + var folder = newJszip.folder("test"); + if(folder.file("test.txt").data == "test string") { + log(SEVERITY.INFO, "all ok"); + } + else { + log(SEVERITY.ERROR, "wrong file"); + } + + var folders = newJszip.folder(new RegExp("^test")); + + if(folders.length == 1) { + log(SEVERITY.INFO, "all ok"); + if(folders[0].options.dir == true) { + log(SEVERITY.INFO, "all ok"); + } + else { + log(SEVERITY.ERROR, "wrong file"); + } + } else { + log(SEVERITY.ERROR, "wrong number of folder"); + } + + var files = newJszip.file(new RegExp("^test")); + if(files.length == 2) { + log(SEVERITY.INFO, "all ok"); + if(files[0].data == "test string" && files[1].data == "test string") { + log(SEVERITY.INFO, "all ok"); + } + else { + log(SEVERITY.ERROR, "wrong data in files"); + } + } + else { + log(SEVERITY.ERROR, "wrong number of files"); + } + + var filterFiles = newJszip.filter((relativePath: string, file: jszip.JSZipFile) => { + if(file.data == "test string") { + return true; + } + return false; + }); + + if(filterFiles.length == 2) { + log(SEVERITY.INFO, "all ok"); + } + else { + log(SEVERITY.ERROR, "wrong number of files"); + } + + newJszip.remove("test/test.txt"); + + filterFiles = newJszip.filter((relativePath: string, file: jszip.JSZipFile) => { + if(file.data == "test string") { + return true; + } + return false; + }); + + if(filterFiles.length == 1) { + log(SEVERITY.INFO, "all ok"); + } + else { + log(SEVERITY.ERROR, "wrong number of files"); + } + + log(SEVERITY.INFO, newJszip.crc32("Test")); + log(SEVERITY.INFO, newJszip.utf8encode("Test")); + log(SEVERITY.INFO, newJszip.utf8decode("Test")); + newJszip.clone(); +} + +function log(severity:number, message: any) { + var log = ""; + switch(severity) { + case 0: + log += "[DEBUG] "; + break; + case 1: + log += "[INFO] "; + break; + case 2: + log += "[WARN] "; + break; + case 3: + log += "[ERROR] "; + break; + case 4: + log += "[FATAL] "; + break; + default: + log += "[INFO]" + break; + } + console.log(log += message); +} + +testJSZip(); \ No newline at end of file diff --git a/jszip/jszip.d.ts b/jszip/jszip.d.ts new file mode 100644 index 0000000000..af6c58329c --- /dev/null +++ b/jszip/jszip.d.ts @@ -0,0 +1,183 @@ +// Type definitions for JSZip +// Project: http://stuk.github.com/jszip/ +// Definitions by: mzeiher +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +module jszip { + export interface JSZip { + /** + * Get a file from the archive + * + * @param path {string} relative path to file + * + * @return {JSZipFile} file matching path, null if no file found + */ + file(path: string): JSZipFile; + + /** + * Get files matching a RegExp from archive + * + * @param path {RegExp} RegExp to match + * + * @return {JSZipFile[]} return all matching files or an empty array + */ + file(path: RegExp): JSZipFile[]; + + /** + * Add a file to the archive + * + * @param path {string} relative path to file + * @param content {any} content of the file + * @param options {JSZipOptions} optional information about the file + * + * @return {JSZip} JSZip object + */ + file(path: string, content: any, options?: JSZipOptions): JSZip; + + /** + * Return an new JSZip instance with the given folder as root + * + * @param name {string} name of the folder + * + * @return {JSZip} new JSZip object with the given folder as root or null + */ + folder(name: string): JSZip; + + /** + * Returns new JSZip instances with the matching folders as root + * + * @param name {RegExp} RegExp to match + * + * @return {JSZipFile[]} new array of JSZipFile objects which match the RegExp + */ + folder(name: RegExp): JSZipFile[]; + + /** + * Removes the file or folder from the archive + * + * @param path {string} relative path of file or folder + * + * @return {JSZip} returns the JSZip instance + */ + remove(path: string): JSZip; + + /** + * Generates a new archive + * + * @param options {JSZipGeneratorOptions} optional options for the generator + * + * @return {any} the serialized archive + */ + generate(options?: JSZipGeneratorOptions): any; + + /** + * Deserialize zip file + * + * @param data {any} serialized zip file + * @param options {JSZipOptions} options for deserializing + * + * @return {JSZip} returns the JSZip instance + */ + load(data: any, options: JSZipOptions): JSZip; + + /** + * Get all files wchich match the given filter function + * + * @param {function} filter function + * + * @return {JSZipFile[]} array of matched elements + */ + filter(predicate: (relativePath: string, file: JSZipFile) => bool): JSZipFile[]; + + /** + * Calculate crc32 of given string + * + * @param data {string} string to calculate crc32 from + * @param crc {number} optional: initializer for crc calc + * + * @return {number} calculated crc32 number + */ + crc32(data: string, crc?: number): number; + + /** + * Clone JSSZip instance + * + * return {JSZip} cloned instsance + */ + clone(): JSZip; + + /** + * UTF8 encode a string + * + * @param data {string} string to encode + */ + utf8encode(data: string): string; + + /** + * UTF8 decode a string + * + * @param data {string} string to decode + */ + utf8decode(data: string): string; + + } + + export interface JSZipSupport { + arraybuffer: bool; + uint8array: bool; + blob: bool; + } + + export interface JSZipGeneratorOptions { + base64?: bool; //deprecated + compression: string; //DEFLATE or STORE + type: string; //base64 (default), string, uint8array, blob + } + + export interface JSZipOptions { + base64: bool; + checkCRC32: bool; + } + + export interface JSZipFile { + name: string; + data: any; + options: JSZipFileOptions; + + asText(): string; + asBinary(): any; + asArrayBuffer(): ArrayBuffer; + asUint8Array(): Uint8Array; + } + + export interface JSZipFileOptions { + base64: bool; + binary: bool; + dir: bool; + date: Date; + } + + export interface JSZipBase64 { + } +} + +declare var JSZip: { + /** + * Create JSZip instance + * If no parameters given an empty zip archive will be created + * + * @param data {any} serialized zip archive + * @param options {JSZipOptions} description of the serialized zip archive + */ + new(data?: any, options?: jszip.JSZipOptions): jszip.JSZip; + + prototype: jszip.JSZip; + support : jszip.JSZipSupport; +} + +declare var JSZipBase64: { + encode(input: string, utf8?: any): string; + decode(input: string, utf8?: any): string; + + prototype: jszip.JSZipBase64; +} \ No newline at end of file