Added definitions for JSZip library

This commit is contained in:
Mathis Zeiher
2013-04-05 11:02:02 +02:00
parent adeeefb65a
commit c365fcd035
2 changed files with 315 additions and 0 deletions

132
jszip/jszip-test.ts Normal file
View File

@@ -0,0 +1,132 @@
/// <reference path="../jquery/jquery.d.ts" />
/// <reference path='jszip.d.ts' />
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();

183
jszip/jszip.d.ts vendored Normal file
View File

@@ -0,0 +1,183 @@
// Type definitions for JSZip
// Project: http://stuk.github.com/jszip/
// Definitions by: mzeiher <https://github.com/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;
}