From 29a51de87579b6f308dfdd5059561628fea91ccc Mon Sep 17 00:00:00 2001 From: yoshinorin Date: Thu, 25 Jan 2024 00:41:16 +0900 Subject: [PATCH] refactor: cleanup redundant file, class function naming --- __tests__/unit/utils/time.test.ts | 10 +++++----- src/app/[...slug]/page.tsx | 2 +- src/app/articles/[...slug]/page.tsx | 2 +- src/app/clientLayout.tsx | 4 ++-- src/app/layout.tsx | 4 ++-- src/app/metadata.ts | 6 +++--- src/app/settings/page.tsx | 4 ++-- src/components/archives.tsx | 4 ++-- src/components/articles.tsx | 6 +++--- src/components/content.tsx | 2 +- src/components/cover.tsx | 4 ++-- src/components/headmeta.tsx | 4 ++-- src/components/searchResult.tsx | 4 ++-- src/services/feeds.ts | 8 ++++---- src/services/theme.ts | 2 +- src/utils/{converters.ts => insight.ts} | 0 src/utils/requestContext.ts | 2 +- src/utils/time.ts | 6 +++--- 18 files changed, 37 insertions(+), 37 deletions(-) rename src/utils/{converters.ts => insight.ts} (100%) diff --git a/__tests__/unit/utils/time.test.ts b/__tests__/unit/utils/time.test.ts index 6298597..ef1f393 100644 --- a/__tests__/unit/utils/time.test.ts +++ b/__tests__/unit/utils/time.test.ts @@ -1,17 +1,17 @@ import { expect, test } from 'vitest' import { - convertUnixtimeToDate, - convertUnixTimeToISODateSrting, + toDate, + toISODateSrting, toJaJpDottedDateString } from '../../../src/utils/time'; test('Unixtime should be convert to Date', () => { - expect(convertUnixtimeToDate(1644075206).toISOString()) + expect(toDate(1644075206).toISOString()) .toEqual("2022-02-05T15:33:26.000Z") }) test('Unixtime should be convert to ISODateTime', () => { - expect(convertUnixTimeToISODateSrting(1644075206)) + expect(toISODateSrting(1644075206)) .toEqual("2022-02-05T15:33:26.000Z") }) @@ -20,7 +20,7 @@ This test seems lazy no-need. // https://stackoverflow.com/questions/56261381/how-do-i-set-a-timezone-in-my-jest-config test('Date should be convert to dotted date string (ja-Jp)', () => { - expect(toJaJpDottedDateString(convertUnixtimeToDate(1644075206))) + expect(toJaJpDottedDateString(toDate(1644075206))) .toEqual("2022.2.6") }) */ diff --git a/src/app/[...slug]/page.tsx b/src/app/[...slug]/page.tsx index 658c50b..6d34be5 100644 --- a/src/app/[...slug]/page.tsx +++ b/src/app/[...slug]/page.tsx @@ -4,7 +4,7 @@ import { cache } from 'react' import { permanentRedirect } from "next/navigation"; import { ContentResponse, Content, ContentResponseWithFetchResponse } from '../../models/models'; import { findByPath } from '../../api/content'; -import { asInsight } from '../../utils/converters'; +import { asInsight } from '../../utils/insight'; import { Renderer } from './renderer'; import { runWithHandleErrorIf, throwIfError } from "../handler"; import { sluggize } from '../../utils/slug'; diff --git a/src/app/articles/[...slug]/page.tsx b/src/app/articles/[...slug]/page.tsx index 13793c6..e8fc855 100644 --- a/src/app/articles/[...slug]/page.tsx +++ b/src/app/articles/[...slug]/page.tsx @@ -6,7 +6,7 @@ import { notFound } from "next/navigation"; import { ContentResponse, Content, ContentResponseWithFetchResponse } from '../../../models/models'; import { isIgnoreRequest } from '../../../utils/filterRequests'; import { findByPath } from '../../../api/content'; -import { asInsight } from '../../../utils/converters'; +import { asInsight } from '../../../utils/insight'; import { Renderer } from './renderer'; import { runWithHandleErrorIf, throwIfError } from "../../handler"; import { generateForArticleOrPage } from '../../metadata'; diff --git a/src/app/clientLayout.tsx b/src/app/clientLayout.tsx index 0a99529..0e5fc75 100644 --- a/src/app/clientLayout.tsx +++ b/src/app/clientLayout.tsx @@ -2,7 +2,7 @@ import '../styles/globals.scss'; import React, { useEffect } from 'react'; -import { getThemeSetting } from '../services/theme'; +import { getTheme } from '../services/theme'; import { BackToTopComponent, FooterComponent, @@ -20,7 +20,7 @@ export default function ClientLayout({ children: React.ReactNode }) { useEffect(() => { - let theme = getThemeSetting(); + let theme = getTheme(); const body = document.body; body.setAttribute('data-theme', theme); }); diff --git a/src/app/layout.tsx b/src/app/layout.tsx index 82124d6..b630c56 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -1,7 +1,7 @@ import React from 'react'; import { Metadata } from 'next' import ClientLayout from './clientLayout'; -import { getThemeSetting } from '../services/theme'; +import { getTheme } from '../services/theme'; import { siteName, mainAuthor, @@ -45,7 +45,7 @@ export default function RootLayout({ }: { children: React.ReactNode }) { - const theme = getThemeSetting(); + const theme = getTheme(); // https://github.com/vercel/next.js/discussions/44506#discussioncomment-7901181 return ( diff --git a/src/app/metadata.ts b/src/app/metadata.ts index d5f7bc2..8eab207 100644 --- a/src/app/metadata.ts +++ b/src/app/metadata.ts @@ -1,7 +1,7 @@ import { Metadata } from 'next'; import { Content } from '../models/content'; import { defaultImage, defaultRobotsMeta, locale, siteName } from '../../config'; -import { convertUnixTimeToISODateSrting } from '../utils/time'; +import { toISODateSrting } from '../utils/time'; import { fullUrl } from '../utils/url'; // TODO: write test code @@ -38,8 +38,8 @@ export async function generateForArticleOrPage(url: string, content: Content): P images: fullUrl(defaultImage, false), authors: [content.authorName], tags: tags.length !== 0 ? content.tags.map(t => t.name) : [], - publishedTime: convertUnixTimeToISODateSrting(content.publishedAt), - modifiedTime: convertUnixTimeToISODateSrting(content.updatedAt) + publishedTime: toISODateSrting(content.publishedAt), + modifiedTime: toISODateSrting(content.updatedAt) } }; } diff --git a/src/app/settings/page.tsx b/src/app/settings/page.tsx index a574a58..493812a 100644 --- a/src/app/settings/page.tsx +++ b/src/app/settings/page.tsx @@ -2,13 +2,13 @@ import React, { useState } from 'react'; import { CoverWithNavigationComponent, DropdownComponent } from '../../components/components'; -import { getThemeSetting } from '../../services/theme'; +import { getTheme } from '../../services/theme'; import containerStyles from '../../styles/components/container.module.scss'; import inputStyles from '../../styles/input.module.scss'; export default function Page(){ - const theme = getThemeSetting(); + const theme = getTheme(); const [selectedOption, setSelectedOption] = useState(''); function onChange(event) { diff --git a/src/components/archives.tsx b/src/components/archives.tsx index ad29348..57c5fab 100644 --- a/src/components/archives.tsx +++ b/src/components/archives.tsx @@ -3,7 +3,7 @@ import React, { useState, useEffect } from 'react'; import Link from 'next/link'; import { Archive } from '../models/models'; -import { convertUnixtimeToLocalDateSrting } from '../utils/time'; +import { toLocalDateSrting } from '../utils/time'; import styles from '../styles/archives.module.scss'; import inputStyles from '../styles/input.module.scss'; import containerStyles from '../styles/components/container.module.scss'; @@ -59,7 +59,7 @@ const FilterdItems: React.FunctionComponent<{ archives: Array }> = ({ a return (
  • - {`${convertUnixtimeToLocalDateSrting(archive.publishedAt)}`}:{`${archive.title}`} + {`${toLocalDateSrting(archive.publishedAt)}`}:{`${archive.title}`}
  • ); diff --git a/src/components/articles.tsx b/src/components/articles.tsx index 2646f2d..38d517a 100644 --- a/src/components/articles.tsx +++ b/src/components/articles.tsx @@ -6,7 +6,7 @@ import homeStyles from '../styles/home.module.scss'; import flexStyles from '../styles/components/flex.module.scss'; import containerStyles from '../styles/components/container.module.scss'; import buttonStyles from '../styles/components/button.module.scss'; -import { convertUnixtimeToDate, toJaJpDottedDateString } from '../utils/time'; +import { toDate, toJaJpDottedDateString } from '../utils/time'; export const ArticlesComponent: React.FunctionComponent<{ articles: Array
    }> = ({ articles }) => { return ( @@ -16,7 +16,7 @@ export const ArticlesComponent: React.FunctionComponent<{ articles: Array

    @@ -83,7 +83,7 @@ const ArticlesSection: React.FunctionComponent<{ articles: Array
    }> = (
    {`${article.title}`} diff --git a/src/components/content.tsx b/src/components/content.tsx index 1eaea9d..703d623 100644 --- a/src/components/content.tsx +++ b/src/components/content.tsx @@ -11,7 +11,7 @@ import containerStyles from '../styles/components/container.module.scss'; import contentStyles from '../styles/components/content.module.scss'; import { Accordion } from './accordion'; import { getSystemMetadata } from '../api/metadata'; -import { appendBackendMeta } from '../utils/converters'; +import { appendBackendMeta } from '../utils/insight'; export const ContentComponent: React.FunctionComponent<{ content: Content, insight: Insight | null }> = ({content, insight}) => { diff --git a/src/components/cover.tsx b/src/components/cover.tsx index d80c218..79257a6 100644 --- a/src/components/cover.tsx +++ b/src/components/cover.tsx @@ -3,7 +3,7 @@ import { TagComponent } from './tag'; import { NavigationComponent } from './navigation'; import { ContentCover, Tag } from '../models/models'; import styles from '../styles/components/cover.module.scss'; -import { convertUnixtimeToDate } from '../utils/time'; +import { toDate } from '../utils/time'; import { title, subTitle, coverBottomItems } from '../../config'; export const CoverComponent: React.FunctionComponent<{ contentCover: ContentCover | null }> = ({ contentCover }) => { @@ -23,7 +23,7 @@ export const CoverComponent: React.FunctionComponent<{ contentCover: ContentCove if (contentCover.publishedAt) { return( - { convertUnixtimeToDate(contentCover.publishedAt).toUTCString() } + { toDate(contentCover.publishedAt).toUTCString() } ) } diff --git a/src/components/headmeta.tsx b/src/components/headmeta.tsx index 04517a3..4db1897 100644 --- a/src/components/headmeta.tsx +++ b/src/components/headmeta.tsx @@ -58,8 +58,8 @@ const HeadMetaComponent: React.FunctionComponent<{ - - + + ); } else { diff --git a/src/components/searchResult.tsx b/src/components/searchResult.tsx index 1cc6310..bb1d76f 100644 --- a/src/components/searchResult.tsx +++ b/src/components/searchResult.tsx @@ -1,7 +1,7 @@ import React from 'react'; import Link from 'next/link'; import styles from '../styles/articles.module.scss'; -import { convertUnixtimeToDate, toJaJpDottedDateString } from '../utils/time'; +import { toDate, toJaJpDottedDateString } from '../utils/time'; import { SearchResponse } from '../models/models'; export const SearchResultComponent: React.FunctionComponent<{ @@ -21,7 +21,7 @@ export const SearchResultComponent: React.FunctionComponent<{

    diff --git a/src/services/feeds.ts b/src/services/feeds.ts index 0c25154..aac68b3 100644 --- a/src/services/feeds.ts +++ b/src/services/feeds.ts @@ -1,5 +1,5 @@ import { Feed } from '../models/models'; -import { convertUnixTimeToISODateSrting } from '../utils/time'; +import { toISODateSrting } from '../utils/time'; const FEED_URL = '/feeds/index.xml'; @@ -13,7 +13,7 @@ export async function generateFeedsString(url: string, siteName: string, mainAut ${siteName} - ${convertUnixTimeToISODateSrting(latest.updated)} + ${toISODateSrting(latest.updated)} ${url} ${mainAuthor} @@ -28,8 +28,8 @@ export async function generateFeedsString(url: string, siteName: string, mainAut ${f.title} ${u} - ${convertUnixTimeToISODateSrting(f.published)} - ${convertUnixTimeToISODateSrting(f.updated)} + ${toISODateSrting(f.published)} + ${toISODateSrting(f.updated)} ` }); diff --git a/src/services/theme.ts b/src/services/theme.ts index c3a5861..f5e6403 100644 --- a/src/services/theme.ts +++ b/src/services/theme.ts @@ -1,4 +1,4 @@ -export function getThemeSetting(): string { +export function getTheme(): string { let theme = 'light'; try { theme = localStorage.getItem('theme'); diff --git a/src/utils/converters.ts b/src/utils/insight.ts similarity index 100% rename from src/utils/converters.ts rename to src/utils/insight.ts diff --git a/src/utils/requestContext.ts b/src/utils/requestContext.ts index 6db9edc..7469a3e 100644 --- a/src/utils/requestContext.ts +++ b/src/utils/requestContext.ts @@ -13,7 +13,7 @@ export function getRequestContext(h: Headers = headers()): RequestContext { requestId: uuid4() } as RequestContext } - let xff = h['x-forwarded-for']; + const xff = h['x-forwarded-for']; // @ts-ignore return { // @ts-ignore diff --git a/src/utils/time.ts b/src/utils/time.ts index 5330e08..cc57d83 100644 --- a/src/utils/time.ts +++ b/src/utils/time.ts @@ -1,4 +1,4 @@ -export function convertUnixtimeToDate(unixTime: number): Date { +export function toDate(unixTime: number): Date { try { return new Date(unixTime * 1000); } catch { @@ -6,7 +6,7 @@ export function convertUnixtimeToDate(unixTime: number): Date { } } -export function convertUnixtimeToLocalDateSrting(unixTime: number): String { +export function toLocalDateSrting(unixTime: number): String { try { return new Date(unixTime * 1000).toLocaleDateString(); } catch { @@ -14,7 +14,7 @@ export function convertUnixtimeToLocalDateSrting(unixTime: number): String { } } -export function convertUnixTimeToISODateSrting(unixTime: number): string { +export function toISODateSrting(unixTime: number): string { try { return new Date(unixTime * 1000).toISOString(); } catch {