Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add v1 of typescript definition file #9369

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions examples/node/domstubs.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ function DOMElement(name) {
this.childNodes = [];
this.attributes = {};
this.textContent = '';
this.parentNode = undefined;

if (name === 'style') {
this.sheet = {
Expand Down Expand Up @@ -98,6 +99,7 @@ DOMElement.prototype = {
var childNodes = this.childNodes;
if (childNodes.indexOf(element) === -1) {
childNodes.push(element);
element.parentNode = this;
}
},

Expand All @@ -106,6 +108,7 @@ DOMElement.prototype = {
newNode.childNodes = this.childNodes;
newNode.attributes = this.attributes;
newNode.textContent = this.textContent;
newNode.parentNode = undefined;
return newNode;
},

Expand All @@ -124,6 +127,15 @@ DOMElement.prototype = {

getSerializer: function DOMElement_getSerializer() {
return new DOMElementSerializer(this);
},

remove: function DOMElement_remove() {
if (this.parentNode) {
this.parentNode.childNodes = this.parentNode.childNodes.filter(
node => node != this
);
this.parentNode = undefined;
}
}
}

Expand Down Expand Up @@ -244,6 +256,9 @@ Image.prototype = {
exports.document = document;
exports.Image = Image;

function Blob(blobParts, options) {}
exports.Blob = Blob;

var exported_symbols = Object.keys(exports);

exports.setStubs = function(namespace) {
Expand Down
18 changes: 18 additions & 0 deletions examples/typescript/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## Overview

Example to demonstrate PDF.js library usage with TypeScript.

## Getting started

Build project and install the example dependencies:

$ cd pdf.js
$ npm install
$ gulp dist
$ cd examples/typescript
$ npm install
$ npm test

## Type declaration file

TypeScript type declarations are in `external/types/*.d.ts`. These are copied over to the dist/ directory by `gulp dist`. The copy in the `build/dist/` directory is used by these tests. Rerun `gulp dist` or manually copy the file into `build/dist/` after updating declaration files.
23 changes: 23 additions & 0 deletions examples/typescript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "pdfjstypestest",
"version": "1.0.0",
"description": "command line tool for analyzing pdfs",
"main": "index.js",
"scripts": {
"test": "jasmine-ts \"src/**/*.spec.ts\"",
"build": "tsc",
"watch": "tsc --watch"
},
"author": "",
"engines": {
"node": ">=8.0.0"
},
"dependencies": {
"@types/jasmine": "^2.8.6",
"@types/node": "^8.10.0",
"jasmine-ts": "^0.2.1",
"pdfjs-dist": "../../build/dist",
"typescript": "^2.7.2"
},
"license": "Apache-2.0"
}
88 changes: 88 additions & 0 deletions examples/typescript/src/jasmine_helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/* Copyright 2017 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import "jasmine";

// Helper functions that make Jasmine tests more async/await friendly.

function makeSafeFn(fn?: () => Promise<void> | void) {
return async (done: DoneFn) => {
try {
fn && (await fn());
done();
} catch (err) {
done.fail(err);
}
};
}

export function expectFieldType(obj: object, field: string, type: string) {
expect(`${field}: ${typeof obj[field]}`).toBe(`${field}: ${type}`);
}

type callback = () => Promise<void> | void;

export function test(msg: string, fn?: callback, timeout?: number) {
it(msg, makeSafeFn(fn), timeout);
}

export let asyncTest = test;

export function asyncBeforeAll(action: callback, timeout?: number) {
beforeAll(makeSafeFn(action), timeout);
}

export function asyncAfterAll(action: callback, timeout?: number) {
afterAll(makeSafeFn(action), timeout);
}

export function asyncBeforeEach(action: callback, timeout?: number) {
beforeEach(makeSafeFn(action), timeout);
}

export function asyncAfterEach(action: callback, timeout?: number) {
afterEach(makeSafeFn(action), timeout);
}


// Utility functions to perform type verification for array types.

export function expectArray(a: any[], len?: number) {
expect(a).toEqual(jasmine.any(Array));
if(len) {
expect(a.length).toBe(len);
}
}

export function expectNumberArray(a: number[], len?: number) {
expectArray(a, len);
expect(typeof a[0]).toBe('number');
}

export function expectTypedArray(a: Uint8Array) {
expect(a).toEqual(jasmine.any(Uint8Array));
}

export function expectTransform(m: number[]) {
expectNumberArray(m, 6);
}

export function expectPoint(point: number[]) {
expectNumberArray(point, 2);
}

export function expectRect(rect: number[]) {
expectNumberArray(rect, 4);
}
Loading