diff --git a/index.d.ts b/index.d.ts index 2441930..cc8dda3 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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, paperSizeUnit?: PaperSizeUnit): undefined; +export declare function getPaperSize(paperType: PaperType, paperSizeUnit?: Omit): undefined; +export declare function getPaperSize(paperType: Omit, paperSizeUnit?: Omit): undefined; export declare function getLandscapePaperSize(paperType: PaperType, paperSizeUnit?: PaperSizeUnit): PaperSpecifications; export declare function getLandscapePaperSize(paperType: Omit, paperSizeUnit?: PaperSizeUnit): undefined; +export declare function getLandscapePaperSize(paperType: PaperType, paperSizeUnit?: Omit): undefined; +export declare function getLandscapePaperSize(paperType: Omit, paperSizeUnit?: Omit): undefined; export declare function getPaperSizeInInches(paperType: PaperType): PaperSpecifications; export declare function getPaperSizeInInches(paperType: Omit): undefined; export declare function getPaperSizeInMillimetres(paperType: PaperType): PaperSpecifications; diff --git a/index.js b/index.js index 5cc69b4..022e778 100644 --- a/index.js +++ b/index.js @@ -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()]; diff --git a/index.ts b/index.ts index f557a53..528612b 100644 --- a/index.ts +++ b/index.ts @@ -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( @@ -27,6 +24,16 @@ export function getPaperSize( paperSizeUnit?: PaperSizeUnit ): undefined +export function getPaperSize( + paperType: PaperType, + paperSizeUnit?: Omit +): undefined + +export function getPaperSize( + paperType: Omit, + paperSizeUnit?: Omit +): undefined + /** * Retrieves the portrait paper dimensions. * When the paperSizeUnit is undefined, the specified units are returned. @@ -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 || @@ -81,6 +90,16 @@ export function getLandscapePaperSize( paperSizeUnit?: PaperSizeUnit ): undefined +export function getLandscapePaperSize( + paperType: PaperType, + paperSizeUnit?: Omit +): undefined + +export function getLandscapePaperSize( + paperType: Omit, + paperSizeUnit?: Omit +): undefined + /** * Retrieves the landscape paper dimensions. * When the paperSizeUnit is undefined, the specified units are returned. diff --git a/paperSpecifications/iso.d.ts b/paperSpecifications/iso.d.ts index 91fe925..23de1b6 100644 --- a/paperSpecifications/iso.d.ts +++ b/paperSpecifications/iso.d.ts @@ -339,4 +339,4 @@ export declare const isoPaperSpecifications: Readonly>; export type IsoPaperType = keyof typeof isoPaperSpecifications | Lowercase; -export declare function isIsoPaperType(possiblePaperType: string): possiblePaperType is IsoPaperType; +export declare function isIsoPaperType(possiblePaperType?: string): possiblePaperType is IsoPaperType; diff --git a/paperSpecifications/iso.js b/paperSpecifications/iso.js index 1b3dfbc..641c243 100644 --- a/paperSpecifications/iso.js +++ b/paperSpecifications/iso.js @@ -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()); } diff --git a/paperSpecifications/iso.ts b/paperSpecifications/iso.ts index 522bf48..92c4b2e 100644 --- a/paperSpecifications/iso.ts +++ b/paperSpecifications/iso.ts @@ -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()) } diff --git a/paperSpecifications/northAmerica.d.ts b/paperSpecifications/northAmerica.d.ts index d9b01de..261338e 100644 --- a/paperSpecifications/northAmerica.d.ts +++ b/paperSpecifications/northAmerica.d.ts @@ -140,4 +140,4 @@ export declare const northAmericanPaperSpecifications: Readonly>; export type NorthAmericanPaperType = keyof typeof northAmericanPaperSpecifications | Lowercase | Capitalize>; -export declare function isNorthAmericanPaperType(possiblePaperType: string): possiblePaperType is NorthAmericanPaperType; +export declare function isNorthAmericanPaperType(possiblePaperType?: string): possiblePaperType is NorthAmericanPaperType; diff --git a/paperSpecifications/northAmerica.js b/paperSpecifications/northAmerica.js index 5148bbe..e1b5f00 100644 --- a/paperSpecifications/northAmerica.js +++ b/paperSpecifications/northAmerica.js @@ -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()); } diff --git a/paperSpecifications/northAmerica.ts b/paperSpecifications/northAmerica.ts index 0546272..90f2777 100644 --- a/paperSpecifications/northAmerica.ts +++ b/paperSpecifications/northAmerica.ts @@ -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() ) } diff --git a/test/test.js b/test/test.js index e761922..53e123f 100644 --- a/test/test.js +++ b/test/test.js @@ -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); @@ -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); @@ -31,12 +31,12 @@ 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'); @@ -44,10 +44,19 @@ 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); + 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); diff --git a/test/test.ts b/test/test.ts index fea34e2..3940433 100644 --- a/test/test.ts +++ b/test/test.ts @@ -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' @@ -9,6 +12,7 @@ import { getPaperSizeInMillimetres, isIsoPaperType, isNorthAmericanPaperType, + isPaperType, paperSpecifications } from '../index.js' @@ -16,12 +20,13 @@ 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)}`) @@ -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 ) @@ -54,7 +59,7 @@ 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 ) @@ -62,7 +67,7 @@ await describe('@cityssm/paper-sizes', async () => { 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') @@ -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) }) })