Skip to content

Commit

Permalink
fix: Improve code style for upcoming ESLint config update (#286)
Browse files Browse the repository at this point in the history
Updating to `@meyfa/eslint-config` v8 with ESLint v9 requires some
code changes, which this commit addresses. The update itself will be
done in a separate commit.
  • Loading branch information
meyfa authored Oct 13, 2024
1 parent c7c2ae8 commit 2462834
Show file tree
Hide file tree
Showing 21 changed files with 42 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/cookies/request-session-cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function findCookie (headers?: AxiosResponseHeaders | RawAxiosResponseHeaders):
const setCookieArray = Array.isArray(setCookie) ? setCookie : [setCookie]

for (const item of setCookieArray) {
const match = item.toString().match(COOKIE_REGEXP)
const match = COOKIE_REGEXP.exec(item.toString())
if (match != null) {
return match[1]
}
Expand Down
2 changes: 1 addition & 1 deletion src/data/match-line-by-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const LINE_IDS_MAPPING: Map<string, Map<string, string>> = (() => {
const lineMapping = new Map()
for (const line of canteen.lines) {
const allNames = [line.name, ...(line.alternativeNames ?? [])]
allNames.forEach(name => lineMapping.set(normalizeNameForMatching(name), line.id))
allNames.forEach((name) => lineMapping.set(normalizeNameForMatching(name), line.id))
}
mapping.set(canteen.id, lineMapping)
}
Expand Down
4 changes: 2 additions & 2 deletions src/jsonapi/build-canteen-lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export interface MappedCanteen {
* @returns The generated Map.
*/
function toCanteenMap (array: Canteen[]): Map<string, MappedCanteen> {
return new Map(array.map(canteen => {
const lineMap = new Map(canteen.lines.map(line => [line.id, line]))
return new Map(array.map((canteen) => {
const lineMap = new Map(canteen.lines.map((line) => [line.id, line]))
return [canteen.id, {
...canteen,
lines: lineMap
Expand Down
4 changes: 2 additions & 2 deletions src/jsonapi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import { CanteenPlan } from '../types/canteen-plan.js'
* @returns Parsed results.
*/
export async function fetch (options: JsonApiOptions): Promise<CanteenPlan[]> {
const auth = options?.auth
const auth = options.auth
if (auth == null) {
throw new Error('auth option is required')
}
const parallel = options?.parallel ?? false
const parallel = options.parallel ?? false

const metadataPromise = request(auth, METADATA_ENDPOINT)
if (!parallel) await metadataPromise // synchronize
Expand Down
6 changes: 3 additions & 3 deletions src/jsonapi/jsonapi-parse-plans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ function getFormattedPrice (mealData: { price_1?: number, info?: string }): stri
* @returns An array of classifiers for the given meal.
*/
function getClassifiers (mealData: Record<string, any>): string[] {
return Object.keys(CLASSIFIER_MAPPING).filter(classifier => mealData[classifier])
.map(classifier => CLASSIFIER_MAPPING[classifier])
return Object.keys(CLASSIFIER_MAPPING).filter((classifier) => mealData[classifier])
.map((classifier) => CLASSIFIER_MAPPING[classifier])
}

/**
Expand All @@ -115,7 +115,7 @@ function getClassifiers (mealData: Record<string, any>): string[] {
* @returns An array of additives for the given meal.
*/
function getAdditives (mealData: { add: string[] }): string[] {
return mealData.add.filter(str => str != null && str !== '')
return mealData.add.filter((str) => str != null && str !== '')
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/simplesite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { SimpleSiteOptions } from '../types/options.js'
/**
* Array of known canteen ids.
*/
const CANTEEN_IDS = Object.freeze(canteens.map(c => c.id))
const CANTEEN_IDS = Object.freeze(canteens.map((c) => c.id))

/**
* Fetch a single instance of the plan and parse it.
Expand Down
2 changes: 1 addition & 1 deletion src/simplesite/parse-classifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const CLASSIFIERS_REGEXP = /^\s*\[([\w,]+)\]\s*$/
* @returns The classifiers array.
*/
export function parseClassifiers (str: string): string[] {
const match = str.match(CLASSIFIERS_REGEXP)
const match = CLASSIFIERS_REGEXP.exec(str)
if (match != null) {
return match[1].split(/\s*,\s*/)
}
Expand Down
2 changes: 1 addition & 1 deletion src/simplesite/parse-datestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function guessYear (refYear: number, refMonth: number, month: number): number {
* @returns An object containing integers: day, month, year.
*/
export function parseDatestamp (str: string, reference: Date): DateSpec | undefined {
const match = str.match(DATE_REGEXP)
const match = DATE_REGEXP.exec(str)
if (match == null) {
return undefined
}
Expand Down
2 changes: 1 addition & 1 deletion src/simplesite/parse-name-and-additives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const NAME_ADDITIVES_REGEXP = /^\s*([\s\S]+\S)\s+\(\s*(\w{1,3}(?:\s*,\s*\w{1,3})
* @returns The resulting name,additives object.
*/
export function parseNameAndAdditives (str: string): { name: string, additives: string[] } {
const match = str.match(NAME_ADDITIVES_REGEXP)
const match = NAME_ADDITIVES_REGEXP.exec(str)
if (match != null) {
return {
name: mergeWhitespace(match[1]),
Expand Down
2 changes: 1 addition & 1 deletion src/simplesite/simplesite-date-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ export function isDateSupported (date: datelike): boolean {
* @returns Week numbers for the given dates.
*/
export function convertToWeeks (dates: datelike[]): Set<number> {
return new Set(dates.map(d => moment(d).isoWeek()))
return new Set(dates.map((d) => moment(d).isoWeek()))
}
2 changes: 1 addition & 1 deletion src/simplesite/simplesite-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export async function request (canteenId: string, weekId: string | number, sessi
headers,
responseType: 'text',
// to avoid JSON parsing, which, unfortunately, is not done automatically based on responseType
transformResponse: res => res,
transformResponse: (res) => res,
timeout: REQUEST_TIMEOUT,
maxContentLength: REQUEST_MAX_LENGTH
})
Expand Down
4 changes: 2 additions & 2 deletions test/cookies/request-session-cookie.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('cookies/request-session-cookie', function () {
afterEach(() => lazyMock.restore())

it('sends request to https://www.sw-ka.de/*', async function () {
lazyMock.get().onAny().replyOnce(config => {
lazyMock.get().onAny().replyOnce((config) => {
assert.ok(config.url?.startsWith('https://www.sw-ka.de/') === true)
return [200, 'test-response']
})
Expand Down Expand Up @@ -53,7 +53,7 @@ describe('cookies/request-session-cookie', function () {
it('sends a Firefox user agent', async function () {
// this is required because sw-ka does not reliably set cookies in other browsers

lazyMock.get().onAny().replyOnce(config => {
lazyMock.get().onAny().replyOnce((config) => {
assert.ok(config.headers != null)
assert.ok(typeof config.headers['User-Agent'] === 'string')
assert.match(config.headers['User-Agent'], /Mozilla/)
Expand Down
10 changes: 5 additions & 5 deletions test/data/canteens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ describe('data/canteens', function () {
})

it('does not have duplicate canteen ids', function () {
checkDuplicates(canteens, canteen => canteen.id)
checkDuplicates(canteens, (canteen) => canteen.id)
})

it('does not have duplicate canteen names', function () {
checkDuplicates(canteens, canteen => canteen.name)
checkDuplicates(canteens, (canteen) => canteen.name)
})

it('does not have duplicate line ids in canteens', function () {
for (const entry of canteens) {
checkDuplicates(entry.lines, line => line.id)
checkDuplicates(entry.lines, (line) => line.id)
}
})

Expand All @@ -66,10 +66,10 @@ describe('data/canteens', function () {
for (const line of entry.lines) {
allNames.push(line.name.toLocaleLowerCase(['de-DE', 'en-US']))
if (line.alternativeNames != null) {
allNames.push(...line.alternativeNames.map(name => name.toLocaleLowerCase(['de-DE', 'en-US'])))
allNames.push(...line.alternativeNames.map((name) => name.toLocaleLowerCase(['de-DE', 'en-US'])))
}
}
checkDuplicates(allNames, name => name)
checkDuplicates(allNames, (name) => name)
}
})
})
2 changes: 1 addition & 1 deletion test/data/legend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ describe('data/legend', function () {
})

it('does not have duplicate short values', function () {
checkDuplicates(legend, entry => entry.short)
checkDuplicates(legend, (entry) => entry.short)
})
})
4 changes: 2 additions & 2 deletions test/data/match-canteen-by-name.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ describe('data/match-canteen-by-name', function () {
assert.strictEqual(matchCanteenByName('Unbekannte Mensa'), undefined)
})

it("matches 'Mensa Am Adenauerring", function () {
it('matches \'Mensa Am Adenauerring', function () {
assert.strictEqual(matchCanteenByName('Mensa Am Adenauerring'), 'adenauerring')
})

it("matches 'Menseria Moltkestraße 30'", function () {
it('matches \'Menseria Moltkestraße 30\'', function () {
assert.strictEqual(matchCanteenByName('Menseria Moltkestraße 30'), 'x1moltkestrasse')
})

Expand Down
8 changes: 4 additions & 4 deletions test/data/match-line-by-name.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ describe('data/match-line-by-name', function () {
assert.strictEqual(matchLineByName('adenauerring', 'Unbekannte Linie'), undefined)
})

it("matches 'Linie 1' (adenauerring)", function () {
it('matches \'Linie 1\' (adenauerring)', function () {
assert.strictEqual(matchLineByName('adenauerring', 'Linie 1'), 'l1')
})

it("matches '[pizza]werk Salate / Vorspeisen' (adenauerring)", function () {
it('matches \'[pizza]werk Salate / Vorspeisen\' (adenauerring)', function () {
assert.strictEqual(matchLineByName('adenauerring', '[pizza]werk Salate / Vorspeisen'), 'salat_dessert')
})

it("matches '[kœri]werk' (adenauerring)", function () {
it('matches \'[kœri]werk\' (adenauerring)', function () {
assert.strictEqual(matchLineByName('adenauerring', '[kœri]werk'), 'aktion')
})

it("matches 'Gut & Günstig' (x1moltkestrasse)", function () {
it('matches \'Gut & Günstig\' (x1moltkestrasse)', function () {
assert.strictEqual(matchLineByName('x1moltkestrasse', 'Gut & Günstig'), 'gut')
})

Expand Down
1 change: 0 additions & 1 deletion test/jsonapi/jsonapi-parse-plans.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ describe('jsonapi/jsonapi-parse-plans', function () {
})

it('uses empty string for price of 0', function () {
// eslint-disable-next-line camelcase
const data = {
adenauerring: {
1597096800: {
Expand Down
4 changes: 2 additions & 2 deletions test/jsonapi/jsonapi-request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('jsonapi/jsonapi-request', function () {

describe('with #METADATA_ENDPOINT', function () {
it('sends request as expected', async function () {
lazyMock.get().onAny().replyOnce(config => {
lazyMock.get().onAny().replyOnce((config) => {
assert.strictEqual(config.url, 'https://www.sw-ka.de/en/json_interface/general/')
assert.strictEqual(config.method, 'get')
assert.deepStrictEqual(config.auth, {
Expand All @@ -45,7 +45,7 @@ describe('jsonapi/jsonapi-request', function () {

describe('with #PLANS_ENDPOINT', function () {
it('sends request as expected', async function () {
lazyMock.get().onAny().replyOnce(config => {
lazyMock.get().onAny().replyOnce((config) => {
assert.strictEqual(config.url, 'https://www.sw-ka.de/en/json_interface/canteen/')
assert.strictEqual(config.method, 'get')
assert.deepStrictEqual(config.auth, {
Expand Down
4 changes: 2 additions & 2 deletions test/simplesite/parse-classifiers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import assert from 'node:assert'
import { parseClassifiers } from '../../src/simplesite/parse-classifiers.js'

describe('simplesite/parse-classifiers', function () {
it("returns [] for empty input ('')", function () {
it('returns [] for empty input (\'\')', function () {
assert.deepStrictEqual(parseClassifiers(''), [])
})

it("returns [] for empty brackets ('[]')", function () {
it('returns [] for empty brackets (\'[]\')', function () {
assert.deepStrictEqual(parseClassifiers('[]'), [])
})

Expand Down
14 changes: 7 additions & 7 deletions test/simplesite/parse-datestamp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ import assert from 'node:assert'
import { parseDatestamp } from '../../src/simplesite/parse-datestamp.js'

describe('simplesite/parse-datestamp', function () {
it("returns undefined for invalid input ('')", function () {
it('returns undefined for invalid input (\'\')', function () {
const ref = new Date(2019, 10, 30)
assert.strictEqual(parseDatestamp('', ref), undefined)
})

it("returns undefined for implausible input ('Mo 40.05.')", function () {
it('returns undefined for implausible input (\'Mo 40.05.\')', function () {
const ref = new Date(2019, 10, 30)
assert.strictEqual(parseDatestamp('Mo 40.05.', ref), undefined)
})

it("parses 'Mo 02.12.' with reference 2019-10-30", function () {
it('parses \'Mo 02.12.\' with reference 2019-10-30', function () {
const ref = new Date(2019, 10, 30)
assert.deepStrictEqual(parseDatestamp('Mo 02.12.', ref), {
day: 2,
Expand All @@ -21,7 +21,7 @@ describe('simplesite/parse-datestamp', function () {
})
})

it("parses 'Mi 01.01.' with reference 2020-01-01", function () {
it('parses \'Mi 01.01.\' with reference 2020-01-01', function () {
const ref = new Date(2020, 0, 1)
assert.deepStrictEqual(parseDatestamp('Mi 01.01.', ref), {
day: 1,
Expand All @@ -30,7 +30,7 @@ describe('simplesite/parse-datestamp', function () {
})
})

it("parses 'Mo 02.12.' with reference 2020-01-05", function () {
it('parses \'Mo 02.12.\' with reference 2020-01-05', function () {
const ref = new Date(2020, 0, 5)
assert.deepStrictEqual(parseDatestamp('Mo 02.12.', ref), {
day: 2,
Expand All @@ -39,7 +39,7 @@ describe('simplesite/parse-datestamp', function () {
})
})

it("parses 'Mi 01.01.' with reference 2019-12-15", function () {
it('parses \'Mi 01.01.\' with reference 2019-12-15', function () {
const ref = new Date(2019, 11, 15)
assert.deepStrictEqual(parseDatestamp('Mi 01.01.', ref), {
day: 1,
Expand All @@ -48,7 +48,7 @@ describe('simplesite/parse-datestamp', function () {
})
})

it("parses 'Mi 01.07.' with reference 2020-06-30", function () {
it('parses \'Mi 01.07.\' with reference 2020-06-30', function () {
// test for chunk overlap
const ref = new Date(2020, 5, 30)
assert.deepStrictEqual(parseDatestamp('Mi 01.07.', ref), {
Expand Down
4 changes: 2 additions & 2 deletions test/simplesite/simplesite-request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('simplesite/simplesite-request', function () {
afterEach(() => lazyMock.restore())

it('sends request as expected', async function () {
lazyMock.get().onAny().replyOnce(config => {
lazyMock.get().onAny().replyOnce((config) => {
assert.strictEqual(config.url, 'https://www.sw-ka.de/de/hochschulgastronomie/speiseplan/mensa_test-canteen/')
assert.strictEqual(config.method, 'get')
assert.deepStrictEqual(config.params, {
Expand All @@ -30,7 +30,7 @@ describe('simplesite/simplesite-request', function () {
})

it('includes session cookie if provided', async function () {
lazyMock.get().onAny().replyOnce(config => {
lazyMock.get().onAny().replyOnce((config) => {
assert.ok(config.headers != null)
assert.strictEqual(config.headers.Cookie, 'platoCMS=qux42baz')
return [200, 'page content']
Expand Down

0 comments on commit 2462834

Please sign in to comment.