Skip to content

Commit

Permalink
increase branch coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
dangowans committed Aug 15, 2024
1 parent 33bc8b4 commit cf0a772
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 45 deletions.
6 changes: 5 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { type PaperType } from './paperSpecifications.js';
import type { PaperSizeUnit, PaperSpecifications } from './types.js';
export declare function isPaperType(possiblePaperType: string): possiblePaperType is PaperType;
export declare function isPaperType(possiblePaperType?: string): possiblePaperType is PaperType;
export declare function getPaperSize(paperType: PaperType, paperSizeUnit?: PaperSizeUnit): PaperSpecifications;
export declare function getPaperSize(paperType: Omit<string, PaperType>, paperSizeUnit?: PaperSizeUnit): undefined;
export declare function getPaperSize(paperType: PaperType, paperSizeUnit?: Omit<string, PaperSizeUnit>): undefined;
export declare function getPaperSize(paperType: Omit<string, PaperType>, paperSizeUnit?: Omit<string, PaperSizeUnit>): undefined;
export declare function getLandscapePaperSize(paperType: PaperType, paperSizeUnit?: PaperSizeUnit): PaperSpecifications;
export declare function getLandscapePaperSize(paperType: Omit<string, PaperType>, paperSizeUnit?: PaperSizeUnit): undefined;
export declare function getLandscapePaperSize(paperType: PaperType, paperSizeUnit?: Omit<string, PaperSizeUnit>): undefined;
export declare function getLandscapePaperSize(paperType: Omit<string, PaperType>, paperSizeUnit?: Omit<string, PaperSizeUnit>): undefined;
export declare function getPaperSizeInInches(paperType: PaperType): PaperSpecifications;
export declare function getPaperSizeInInches(paperType: Omit<string, PaperType>): undefined;
export declare function getPaperSizeInMillimetres(paperType: PaperType): PaperSpecifications;
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { paperSpecifications } from './paperSpecifications.js';
const mmToInches = 25.4;
export function isPaperType(possiblePaperType) {
return Object.hasOwn(paperSpecifications, (possiblePaperType ?? '').toUpperCase());
export function isPaperType(possiblePaperType = '') {
return Object.hasOwn(paperSpecifications, possiblePaperType.toUpperCase());
}
export function getPaperSize(paperType, paperSizeUnit) {
const specifications = paperSpecifications[(paperType ?? '').toUpperCase()];
Expand Down
31 changes: 25 additions & 6 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@ const mmToInches = 25.4
* @returns True if the string represents a known paper type.
*/
export function isPaperType(
possiblePaperType: string
possiblePaperType = ''
): possiblePaperType is PaperType {
return Object.hasOwn(
paperSpecifications,
(possiblePaperType ?? '').toUpperCase()
)
return Object.hasOwn(paperSpecifications, possiblePaperType.toUpperCase())
}

export function getPaperSize(
Expand All @@ -27,6 +24,16 @@ export function getPaperSize(
paperSizeUnit?: PaperSizeUnit
): undefined

export function getPaperSize(
paperType: PaperType,
paperSizeUnit?: Omit<string, PaperSizeUnit>
): undefined

export function getPaperSize(
paperType: Omit<string, PaperType>,
paperSizeUnit?: Omit<string, PaperSizeUnit>
): undefined

/**
* Retrieves the portrait paper dimensions.
* When the paperSizeUnit is undefined, the specified units are returned.
Expand All @@ -40,7 +47,9 @@ export function getPaperSize(
paperType: string,
paperSizeUnit?: PaperSizeUnit
): PaperSpecifications | undefined {
const specifications = paperSpecifications[(paperType ?? '').toUpperCase()]
const specifications = paperSpecifications[
(paperType ?? '').toUpperCase()
] as PaperSpecifications | undefined

if (
specifications === undefined ||
Expand Down Expand Up @@ -81,6 +90,16 @@ export function getLandscapePaperSize(
paperSizeUnit?: PaperSizeUnit
): undefined

export function getLandscapePaperSize(
paperType: PaperType,
paperSizeUnit?: Omit<string, PaperSizeUnit>
): undefined

export function getLandscapePaperSize(
paperType: Omit<string, PaperType>,
paperSizeUnit?: Omit<string, PaperSizeUnit>
): undefined

/**
* Retrieves the landscape paper dimensions.
* When the paperSizeUnit is undefined, the specified units are returned.
Expand Down
2 changes: 1 addition & 1 deletion paperSpecifications/iso.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,4 +339,4 @@ export declare const isoPaperSpecifications: Readonly<Readonly<{
};
}>>;
export type IsoPaperType = keyof typeof isoPaperSpecifications | Lowercase<keyof typeof isoPaperSpecifications>;
export declare function isIsoPaperType(possiblePaperType: string): possiblePaperType is IsoPaperType;
export declare function isIsoPaperType(possiblePaperType?: string): possiblePaperType is IsoPaperType;
4 changes: 2 additions & 2 deletions paperSpecifications/iso.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ export const cSeriesPaperSpecifications = Object.freeze({
C10: { width: 28, height: 40, unit: 'mm' }
});
export const isoPaperSpecifications = Object.freeze(Object.assign({}, aSeriesPaperSpecifications, bSeriesPaperSpecifications, cSeriesPaperSpecifications));
export function isIsoPaperType(possiblePaperType) {
return Object.hasOwn(isoPaperSpecifications, (possiblePaperType ?? '').toUpperCase());
export function isIsoPaperType(possiblePaperType = '') {
return Object.hasOwn(isoPaperSpecifications, possiblePaperType.toUpperCase());
}
7 changes: 2 additions & 5 deletions paperSpecifications/iso.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@ export type IsoPaperType =
* @returns True if the string represents a known ISO paper type.
*/
export function isIsoPaperType(
possiblePaperType: string
possiblePaperType = ''
): possiblePaperType is IsoPaperType {
return Object.hasOwn(
isoPaperSpecifications,
(possiblePaperType ?? '').toUpperCase()
)
return Object.hasOwn(isoPaperSpecifications, possiblePaperType.toUpperCase())
}
2 changes: 1 addition & 1 deletion paperSpecifications/northAmerica.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,4 @@ export declare const northAmericanPaperSpecifications: Readonly<Readonly<{
};
}>>;
export type NorthAmericanPaperType = keyof typeof northAmericanPaperSpecifications | Lowercase<keyof typeof northAmericanPaperSpecifications> | Capitalize<Lowercase<keyof typeof northAmericanPaperSpecifications>>;
export declare function isNorthAmericanPaperType(possiblePaperType: string): possiblePaperType is NorthAmericanPaperType;
export declare function isNorthAmericanPaperType(possiblePaperType?: string): possiblePaperType is NorthAmericanPaperType;
4 changes: 2 additions & 2 deletions paperSpecifications/northAmerica.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ export const archPaperSpecifications = Object.freeze({
'ARCH E3': { width: 27, height: 39, unit: 'in' }
});
export const northAmericanPaperSpecifications = Object.freeze(Object.assign({}, commonNorthAmericanPaperSpecifications, ansiPaperSpecifications, archPaperSpecifications));
export function isNorthAmericanPaperType(possiblePaperType) {
return Object.hasOwn(northAmericanPaperSpecifications, (possiblePaperType ?? '').toUpperCase());
export function isNorthAmericanPaperType(possiblePaperType = '') {
return Object.hasOwn(northAmericanPaperSpecifications, possiblePaperType.toUpperCase());
}
4 changes: 2 additions & 2 deletions paperSpecifications/northAmerica.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ export type NorthAmericanPaperType =
* @returns True if the string represents a known ISO paper type.
*/
export function isNorthAmericanPaperType(
possiblePaperType: string
possiblePaperType = ''
): possiblePaperType is NorthAmericanPaperType {
return Object.hasOwn(
northAmericanPaperSpecifications,
(possiblePaperType ?? '').toUpperCase()
possiblePaperType.toUpperCase()
)
}
27 changes: 18 additions & 9 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import assert from 'node:assert';
import { describe, it } from 'node:test';
import { getLandscapePaperSize, getPaperSize, getPaperSizeInInches, getPaperSizeInMillimetres, isIsoPaperType, isNorthAmericanPaperType, paperSpecifications } from '../index.js';
import { getLandscapePaperSize, getPaperSize, getPaperSizeInInches, getPaperSizeInMillimetres, isIsoPaperType, isNorthAmericanPaperType, isPaperType, paperSpecifications } from '../index.js';
await describe('@cityssm/paper-sizes', async () => {
const paperTypes = Object.keys(paperSpecifications);
for (const paperType of paperTypes) {
await describe(`Paper type: ${paperType}`, async () => {
await it('Defined with an upper case key', async () => {
await it('Defined with an upper case key', () => {
assert.strictEqual(paperType, paperType.toUpperCase());
});
await it('Returns portrait paper dimensions', async () => {
await it('Returns portrait paper dimensions', () => {
const portrait = getPaperSize(paperType);
console.log(`${paperType}: ${JSON.stringify(portrait)}`);
assert(portrait);
Expand All @@ -20,7 +20,7 @@ await describe('@cityssm/paper-sizes', async () => {
assert.strictEqual(portrait.unit, 'in');
}
});
await it('Returns landscape paper dimensions', async () => {
await it('Returns landscape paper dimensions', () => {
const landscape = getLandscapePaperSize(paperType.toUpperCase());
assert(landscape);
assert(landscape.width >= landscape.height);
Expand All @@ -31,23 +31,32 @@ await describe('@cityssm/paper-sizes', async () => {
assert.strictEqual(landscape.unit, 'in');
}
});
await it('Returns paper dimensions in inches', async () => {
await it('Returns paper dimensions in inches', () => {
const portrait = getPaperSizeInInches(paperType.toLowerCase());
assert(portrait);
assert.strictEqual(portrait.unit, 'in');
});
await it('Returns paper dimensions in millimetres', async () => {
await it('Returns paper dimensions in millimetres', () => {
const portrait = getPaperSizeInMillimetres(paperType);
assert(portrait);
assert.strictEqual(portrait.unit, 'mm');
});
});
}
await describe('Error handling', async () => {
await it('Returns undefined when the size unit is unknown', async () => {
assert.strictEqual(getPaperSize('Letter', 'bananas'), undefined);
await it('Returns false for unknown paper types', () => {
const invalidPaperType = 'invalidPaperType';
assert.strictEqual(isPaperType(invalidPaperType), false);
assert.strictEqual(isNorthAmericanPaperType(invalidPaperType), false);
assert.strictEqual(isIsoPaperType(invalidPaperType), false);
assert.strictEqual(isPaperType(undefined), false);
});
await it('Returns undefined when the paper type is unknown', async () => {
await it('Returns undefined when the size unit is unknown', () => {
const invalidUnit = 'bananas';
assert.strictEqual(getPaperSize('Letter', invalidUnit), undefined);
assert.strictEqual(getLandscapePaperSize('Letter', invalidUnit), undefined);
});
await it('Returns undefined when the paper type is unknown', () => {
const unknownPaperType = 'unknown';
assert.strictEqual(getPaperSize(unknownPaperType), undefined);
assert.strictEqual(getLandscapePaperSize(unknownPaperType), undefined);
Expand Down
44 changes: 30 additions & 14 deletions test/test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// eslint-disable-next-line @eslint-community/eslint-comments/disable-enable-pair
/* eslint-disable @typescript-eslint/no-confusing-void-expression */

import assert from 'node:assert'
import { describe, it } from 'node:test'

Expand All @@ -9,19 +12,21 @@ import {
getPaperSizeInMillimetres,
isIsoPaperType,
isNorthAmericanPaperType,
isPaperType,
paperSpecifications
} from '../index.js'

await describe('@cityssm/paper-sizes', async () => {
const paperTypes = Object.keys(paperSpecifications) as PaperType[]

for (const paperType of paperTypes) {
// eslint-disable-next-line @typescript-eslint/no-loop-func
await describe(`Paper type: ${paperType}`, async () => {
await it('Defined with an upper case key', async () => {
await it('Defined with an upper case key', () => {
assert.strictEqual(paperType, paperType.toUpperCase())
})

await it('Returns portrait paper dimensions', async () => {
await it('Returns portrait paper dimensions', () => {
const portrait = getPaperSize(paperType)
console.log(`${paperType}: ${JSON.stringify(portrait)}`)

Expand All @@ -37,7 +42,7 @@ await describe('@cityssm/paper-sizes', async () => {
}
})

await it('Returns landscape paper dimensions', async () => {
await it('Returns landscape paper dimensions', () => {
const landscape = getLandscapePaperSize(
paperType.toUpperCase() as PaperType
)
Expand All @@ -54,15 +59,15 @@ await describe('@cityssm/paper-sizes', async () => {
}
})

await it('Returns paper dimensions in inches', async () => {
await it('Returns paper dimensions in inches', () => {
const portrait = getPaperSizeInInches(
paperType.toLowerCase() as PaperType
)
assert(portrait)
assert.strictEqual(portrait.unit, 'in')
})

await it('Returns paper dimensions in millimetres', async () => {
await it('Returns paper dimensions in millimetres', () => {
const portrait = getPaperSizeInMillimetres(paperType)
assert(portrait)
assert.strictEqual(portrait.unit, 'mm')
Expand All @@ -71,23 +76,34 @@ await describe('@cityssm/paper-sizes', async () => {
}

await describe('Error handling', async () => {
await it('Returns undefined when the size unit is unknown', async() => {
assert.strictEqual(getPaperSize('Letter', 'bananas'), undefined)
await it('Returns false for unknown paper types', () => {
const invalidPaperType = 'invalidPaperType'

assert.strictEqual(isPaperType(invalidPaperType), false)
assert.strictEqual(isNorthAmericanPaperType(invalidPaperType), false)
assert.strictEqual(isIsoPaperType(invalidPaperType), false)

// eslint-disable-next-line unicorn/no-useless-undefined
assert.strictEqual(isPaperType(undefined), false)
})

await it('Returns undefined when the size unit is unknown', () => {
const invalidUnit = 'bananas'

assert.strictEqual(getPaperSize('Letter', invalidUnit), undefined)
assert.strictEqual(
getLandscapePaperSize('Letter', invalidUnit),
undefined
)
})

await it('Returns undefined when the paper type is unknown', async () => {
await it('Returns undefined when the paper type is unknown', () => {
const unknownPaperType = 'unknown'

// eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
assert.strictEqual(getPaperSize(unknownPaperType), undefined)

// eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
assert.strictEqual(getLandscapePaperSize(unknownPaperType), undefined)

// eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
assert.strictEqual(getPaperSizeInInches(unknownPaperType), undefined)

// eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
assert.strictEqual(getPaperSizeInMillimetres(unknownPaperType), undefined)
})
})
Expand Down

0 comments on commit cf0a772

Please sign in to comment.