refactor: use reduce generic type instead of type cast

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2023-11-30 20:39:35 +01:00
parent e46b987201
commit 20f0a497ce
5 changed files with 29 additions and 33 deletions

View file

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file) * SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file)
* *
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
@ -34,7 +34,7 @@ export class YTextSyncViewPlugin implements PluginValue {
} }
private calculateChanges(event: YTextEvent): ChangeSpec[] { private calculateChanges(event: YTextEvent): ChangeSpec[] {
const [changes] = event.delta.reduce( const [changes] = event.delta.reduce<[ChangeSpec[], number]>(
([changes, position], delta) => { ([changes, position], delta) => {
if (delta.insert !== undefined && typeof delta.insert === 'string') { if (delta.insert !== undefined && typeof delta.insert === 'string') {
changes.push({ from: position, to: position, insert: delta.insert }) changes.push({ from: position, to: position, insert: delta.insert })
@ -48,7 +48,7 @@ export class YTextSyncViewPlugin implements PluginValue {
return [changes, position] return [changes, position]
} }
}, },
[[], 0] as [ChangeSpec[], number] [[], 0]
) )
return changes return changes
} }

View file

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file) * SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file)
* *
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
@ -43,10 +43,10 @@ export const convertClipboardTableToMarkdown = (pasteData: string): string => {
return '' return ''
} }
const tableRows = pasteData.split(/\r?\n/).filter((row) => row.trim() !== '') const tableRows = pasteData.split(/\r?\n/).filter((row) => row.trim() !== '')
const tableCells = tableRows.reduce((cellsInRow, row, index) => { const tableCells = tableRows.reduce<string[][]>((cellsInRow, row, index) => {
cellsInRow[index] = row.split('\t') cellsInRow[index] = row.split('\t')
return cellsInRow return cellsInRow
}, [] as string[][]) }, [])
const arrayMaxRows = createNumberRangeArray(tableCells.length) const arrayMaxRows = createNumberRangeArray(tableCells.length)
const arrayMaxColumns = createNumberRangeArray(Math.max(...tableCells.map((row) => row.length))) const arrayMaxColumns = createNumberRangeArray(Math.max(...tableCells.map((row) => row.length)))

View file

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file) * SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file)
* *
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
@ -12,20 +12,20 @@ interface EmojiEntry {
type ShortCodeMap = { [key: string]: string } type ShortCodeMap = { [key: string]: string }
const shortCodeMap = (emojiData as unknown as EmojiEntry[]).reduce((reduceObject, emoji) => { const shortCodeMap = (emojiData as unknown as EmojiEntry[]).reduce<ShortCodeMap>((reduceObject, emoji) => {
emoji.shortcodes.forEach((shortcode) => { emoji.shortcodes.forEach((shortcode) => {
reduceObject[shortcode] = emoji.emoji reduceObject[shortcode] = emoji.emoji
}) })
return reduceObject return reduceObject
}, {} as ShortCodeMap) }, {})
const emojiSkinToneModifierMap = [1, 2, 3, 4, 5].reduce((reduceObject, modifierValue) => { const emojiSkinToneModifierMap = [1, 2, 3, 4, 5].reduce<ShortCodeMap>((reduceObject, modifierValue) => {
const lightSkinCode = 127995 const lightSkinCode = 127995
const codepoint = lightSkinCode + (modifierValue - 1) const codepoint = lightSkinCode + (modifierValue - 1)
const shortcode = `skin-tone-${modifierValue}` const shortcode = `skin-tone-${modifierValue}`
reduceObject[shortcode] = `&#${codepoint};` reduceObject[shortcode] = `&#${codepoint};`
return reduceObject return reduceObject
}, {} as ShortCodeMap) }, {})
export const combinedEmojiData = { export const combinedEmojiData = {
...shortCodeMap, ...shortCodeMap,

View file

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2020 The HedgeDoc developers (see AUTHORS file) * SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file)
* *
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
@ -17,8 +17,9 @@ export function convertInlineStyleToMap(
return {} return {}
} }
return inlineStyle.split(';').reduce( return inlineStyle
(styleObject, stylePropertyValue) => { .split(';')
.reduce<Record<string, string>>((styleObject, stylePropertyValue) => {
// extract the style property name and value // extract the style property name and value
const [property, value] = stylePropertyValue const [property, value] = stylePropertyValue
.split(/^([^:]+):/) .split(/^([^:]+):/)
@ -44,7 +45,5 @@ export function convertInlineStyleToMap(
styleObject[replacedProperty] = value styleObject[replacedProperty] = value
return styleObject return styleObject
}, }, {})
{} as Record<string, string>
)
} }

View file

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2020 The HedgeDoc developers (see AUTHORS file) * SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file)
* *
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
@ -48,22 +48,19 @@ export function mapHtmlAttributesToReactElementAttributes(
!isEventHandlerAttribute(attribute) && !isEventHandlerAttribute(attribute) &&
isValidTagOrAttributeName(attribute) isValidTagOrAttributeName(attribute)
) )
.reduce( .reduce<Record<string, string>>((mappedAttributes, attribute) => {
(mappedAttributes, attribute) => { // lowercase the attribute name and find it in the react attribute map
// lowercase the attribute name and find it in the react attribute map const lowerCaseAttribute = attribute.toLowerCase()
const lowerCaseAttribute = attribute.toLowerCase()
// format the attribute name // format the attribute name
const name = reactAttributes[lowerCaseAttribute] || attribute const name = reactAttributes[lowerCaseAttribute] || attribute
// add the parsed attribute value to the mapped attributes // add the parsed attribute value to the mapped attributes
mappedAttributes[name] = getParsedAttributeValue( mappedAttributes[name] = getParsedAttributeValue(
name, name,
attributes[attribute] attributes[attribute]
) )
return mappedAttributes return mappedAttributes
}, }, {})
{} as Record<string, string>
)
} }