From 47d6cc19e18fc4abe25cdab68f4499ad6d17a18a Mon Sep 17 00:00:00 2001 From: uzlopak <aras.abbasi@googlemail.com> Date: Thu, 12 Sep 2024 04:10:22 +0200 Subject: [PATCH 1/2] rewrite pluralizer --- lib/mock/mock-agent.js | 9 +++++---- lib/mock/pluralizer.js | 42 ++++++++++++++++++++++++++++-------------- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/lib/mock/mock-agent.js b/lib/mock/mock-agent.js index c02ee375e25..72aa40ea188 100644 --- a/lib/mock/mock-agent.js +++ b/lib/mock/mock-agent.js @@ -18,9 +18,11 @@ const MockPool = require('./mock-pool') const { matchValue, buildMockOptions } = require('./mock-utils') const { InvalidArgumentError, UndiciError } = require('../core/errors') const Dispatcher = require('../dispatcher/dispatcher') -const Pluralizer = require('./pluralizer') +const pluralizer = require('./pluralizer') const PendingInterceptorsFormatter = require('./pending-interceptors-formatter') +const interceptorPluralizer = pluralizer('interceptor', 'interceptors') + class MockAgent extends Dispatcher { constructor (opts) { super(opts) @@ -147,10 +149,9 @@ class MockAgent extends Dispatcher { return } - const pluralizer = new Pluralizer('interceptor', 'interceptors').pluralize(pending.length) + const pluralized = interceptorPluralizer(pending.length) - throw new UndiciError(` -${pluralizer.count} ${pluralizer.noun} ${pluralizer.is} pending: + throw new UndiciError(`${pluralized.count} ${pluralized.noun} ${pluralized.is} pending: ${pendingInterceptorsFormatter.format(pending)} `.trim()) diff --git a/lib/mock/pluralizer.js b/lib/mock/pluralizer.js index 47f150bc27a..0586f6e544b 100644 --- a/lib/mock/pluralizer.js +++ b/lib/mock/pluralizer.js @@ -1,29 +1,43 @@ 'use strict' -const singulars = { +const singulars = /** @type {const} */ ({ pronoun: 'it', is: 'is', was: 'was', this: 'this' -} +}) -const plurals = { +const plurals = /** @type {const} */ ({ pronoun: 'they', is: 'are', was: 'were', this: 'these' -} +}) -module.exports = class Pluralizer { - constructor (singular, plural) { - this.singular = singular - this.plural = plural - } +/** + * @template S, P + * @template {number|1} [C=number] + * @typedef {((count: 1) => Readonly<typeof singulars & { noun: S, count: 1 }>) & ((count: C) => Readonly<typeof plurals & { noun: P, count: C }>)} PluralizerFunction + */ - pluralize (count) { - const one = count === 1 - const keys = one ? singulars : plurals - const noun = one ? this.singular : this.plural - return { ...keys, count, noun } +/** + * Generates a pluralizer function for the given singular and plural forms. + * + * @template {string} S + * @template {string} P + * @param {S} singular - The singular form of the word. + * @param {P} plural - The plural form of the word. + * @returns {PluralizerFunction<S, P>} - A function that pluralizes the word based on the provided count. + */ +function pluralizer (singular, plural) { + const singularResult = { ...singulars, noun: singular } + const pluralResult = { ...plurals, noun: plural } + + return (count) => { + return count === 1 + ? { ...singularResult, count } + : { ...pluralResult, count } } } + +module.exports = pluralizer From 0041c803aed8d3aba26f14a47b759ca82867fcfd Mon Sep 17 00:00:00 2001 From: uzlopak <aras.abbasi@googlemail.com> Date: Thu, 12 Sep 2024 04:21:05 +0200 Subject: [PATCH 2/2] remove pluralizer --- lib/mock/mock-agent.js | 14 +++++--------- lib/mock/pluralizer.js | 43 ------------------------------------------ 2 files changed, 5 insertions(+), 52 deletions(-) delete mode 100644 lib/mock/pluralizer.js diff --git a/lib/mock/mock-agent.js b/lib/mock/mock-agent.js index 72aa40ea188..6ee68570539 100644 --- a/lib/mock/mock-agent.js +++ b/lib/mock/mock-agent.js @@ -18,11 +18,8 @@ const MockPool = require('./mock-pool') const { matchValue, buildMockOptions } = require('./mock-utils') const { InvalidArgumentError, UndiciError } = require('../core/errors') const Dispatcher = require('../dispatcher/dispatcher') -const pluralizer = require('./pluralizer') const PendingInterceptorsFormatter = require('./pending-interceptors-formatter') -const interceptorPluralizer = pluralizer('interceptor', 'interceptors') - class MockAgent extends Dispatcher { constructor (opts) { super(opts) @@ -149,12 +146,11 @@ class MockAgent extends Dispatcher { return } - const pluralized = interceptorPluralizer(pending.length) - - throw new UndiciError(`${pluralized.count} ${pluralized.noun} ${pluralized.is} pending: - -${pendingInterceptorsFormatter.format(pending)} -`.trim()) + throw new UndiciError( + pending.length === 1 + ? `1 interceptor is pending:\n\n${pendingInterceptorsFormatter.format(pending)}`.trim() + : `${pending.length} interceptors are pending:\n\n${pendingInterceptorsFormatter.format(pending)}`.trim() + ) } } diff --git a/lib/mock/pluralizer.js b/lib/mock/pluralizer.js deleted file mode 100644 index 0586f6e544b..00000000000 --- a/lib/mock/pluralizer.js +++ /dev/null @@ -1,43 +0,0 @@ -'use strict' - -const singulars = /** @type {const} */ ({ - pronoun: 'it', - is: 'is', - was: 'was', - this: 'this' -}) - -const plurals = /** @type {const} */ ({ - pronoun: 'they', - is: 'are', - was: 'were', - this: 'these' -}) - -/** - * @template S, P - * @template {number|1} [C=number] - * @typedef {((count: 1) => Readonly<typeof singulars & { noun: S, count: 1 }>) & ((count: C) => Readonly<typeof plurals & { noun: P, count: C }>)} PluralizerFunction - */ - -/** - * Generates a pluralizer function for the given singular and plural forms. - * - * @template {string} S - * @template {string} P - * @param {S} singular - The singular form of the word. - * @param {P} plural - The plural form of the word. - * @returns {PluralizerFunction<S, P>} - A function that pluralizes the word based on the provided count. - */ -function pluralizer (singular, plural) { - const singularResult = { ...singulars, noun: singular } - const pluralResult = { ...plurals, noun: plural } - - return (count) => { - return count === 1 - ? { ...singularResult, count } - : { ...pluralResult, count } - } -} - -module.exports = pluralizer