Skip to content

Commit

Permalink
Fix faulty TypeScript typings
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiasw committed Mar 17, 2023
1 parent 45b0fb9 commit 0484def
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 9 deletions.
1 change: 1 addition & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ jobs:
- run: npm ci
- run: npm run lint
- run: npm run test
- run: npm run test:types
- run: npm run test:build
env:
CI: true
Expand Down
16 changes: 11 additions & 5 deletions exif-reader.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,22 @@ interface PngFileTags {
}
}

interface PngPhysTags {
'Pixels Per Unit X'?: NumberFileTag,
'Pixels Per Unit Y'?: NumberFileTag,
'Pixel Units'?: {
value: number,
description: 'meters' | 'Unknown'
},
'Modify Date'?: NumberArrayFileTag
}

interface PngTag {
description: string,
value: string | number
}

interface PngTags extends PngFileTags {
'Pixels Per Unit X'?: NumberFileTag
'Pixels Per Unit Y'?: NumberFileTag
'Pixel Units'?: 'meters' | 'Unknown'
'Modify Date'?: NumberArrayFileTag
type PngTags = PngFileTags & PngPhysTags & {
[name: string]: PngTag
}

Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"rimraf": "^3.0.2",
"string-replace-loader": "^3.0.3",
"terser-webpack-plugin": "^5.2.4",
"typescript": "^5.0.2",
"webpack": "^5.74.0",
"webpack-cli": "^4.10.0",
"webpack-dev-server": "^4.11.1"
Expand All @@ -60,21 +61,22 @@
"test:build:custom": "mocha --bail test/build/test-custom.js",
"test:build:update": "node test/build/update.js",
"test:e2e": "cypress run",
"test:all": "npm-run-all lint coverage test:e2e test:build test:build:custom",
"test:types": "tsc --noEmit --project test/types",
"test:all": "npm-run-all lint coverage test:e2e test:build test:build:custom test:types",
"postinstall": "node bin/build.js --only-with-config"
},
"nyc": {
"check-coverage": true,
"statements": 95,
"branches": 93,
"branches": 92,
"functions": 98,
"lines": 96,
"lines": 95,
"reporter": [
"lcov",
"text"
],
"exclude": [
"test/**/*.js",
"test/*",
"**/node_modules/*"
]
},
Expand Down
21 changes: 21 additions & 0 deletions test/types/exif-reader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {load} from '../../exif-reader.js';

load('', {includeUnknown: true});
load('', {length: 1024});
load('', {expanded: false, includeUnknown: true, length: 1024});

const tags = await load('');
const expandedTags = await load('', {expanded: true});

////////
// PNG
tags['Color Type']?.description === 'Grayscale';

expandedTags.png?.['Color Type']?.description === 'Grayscale';
expandedTags.png?.['Pixel Units']?.value === 1;
// @ts-expect-error
expandedTags.png?.['Pixel Units']?.value === 'a string';
expandedTags.png?.['Pixel Units']?.description === 'meters';
// @ts-expect-error
expandedTags.png?.['Color Type']?.description === 'A non-color type value';
expandedTags.png?.['Custom Tag Name']?.description === 'Should work';
8 changes: 8 additions & 0 deletions test/types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"target": "es2017",
"module": "es2022",
"strict": true
},
"include": ["./*.ts"]
}
10 changes: 10 additions & 0 deletions test/unit/png-text-tags-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,14 @@ describe('png-text-tags', () => {
description: 'My other value.'
});
});

it('should ignore tags that use compression', () => {
const tagDataiTXt = 'MyTag1\x00\x01\x00fr\x00MyFrTag1\x00Compressed value.';
const dataView = getDataView(tagDataiTXt);
const chunks = [{type: 'iTXt', offset: 0, length: tagDataiTXt.length}];

const tags = PngTextTags.read(dataView, chunks);

expect(tags).to.deep.equal({});
});
});

0 comments on commit 0484def

Please sign in to comment.