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

chore: remove pluralizer #3586

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Changes from 1 commit
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
Next Next commit
rewrite pluralizer
Uzlopak committed Sep 12, 2024
commit 47d6cc19e18fc4abe25cdab68f4499ad6d17a18a
9 changes: 5 additions & 4 deletions lib/mock/mock-agent.js
Original file line number Diff line number Diff line change
@@ -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())
42 changes: 28 additions & 14 deletions lib/mock/pluralizer.js
Original file line number Diff line number Diff line change
@@ -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