Skip to content

Commit

Permalink
refactor: cleanup redundant file, class function naming
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshinorin committed Jan 24, 2024
1 parent 4439a09 commit 29a51de
Show file tree
Hide file tree
Showing 18 changed files with 37 additions and 37 deletions.
10 changes: 5 additions & 5 deletions __tests__/unit/utils/time.test.ts
Original file line number Diff line number Diff line change
@@ -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")
})

Expand All @@ -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")
})
*/
2 changes: 1 addition & 1 deletion src/app/[...slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion src/app/articles/[...slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
4 changes: 2 additions & 2 deletions src/app/clientLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
});
Expand Down
4 changes: 2 additions & 2 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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 (
<html lang={lang}>
Expand Down
6 changes: 3 additions & 3 deletions src/app/metadata.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
}
};
}
4 changes: 2 additions & 2 deletions src/app/settings/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/components/archives.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -59,7 +59,7 @@ const FilterdItems: React.FunctionComponent<{ archives: Array<Archive> }> = ({ a
return (
<li key={idx}>
<Link href={`${archive.path}`} prefetch={false} target="_blank">
{`${convertUnixtimeToLocalDateSrting(archive.publishedAt)}`}:{`${archive.title}`}
{`${toLocalDateSrting(archive.publishedAt)}`}:{`${archive.title}`}
</Link>
</li>
);
Expand Down
6 changes: 3 additions & 3 deletions src/components/articles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<Article> }> = ({ articles }) => {
return (
Expand All @@ -16,7 +16,7 @@ export const ArticlesComponent: React.FunctionComponent<{ articles: Array<Articl
<article className={styles['article']} key={idx}>
<div className={styles['wrap']}>
<time dateTime={`${article.publishedAt}`} className={`${styles['time']}`}>
{`${toJaJpDottedDateString(convertUnixtimeToDate(article.publishedAt))}`}
{`${toJaJpDottedDateString(toDate(article.publishedAt))}`}
</time>
<Link href={`${article.path}`} prefetch={false} className='unstyled' >
<h2 className={`${styles['header']} ${styles['title']}`}>
Expand Down Expand Up @@ -83,7 +83,7 @@ const ArticlesSection: React.FunctionComponent<{ articles: Array<Article> }> = (
<div className={articlesInSection['wrap']}>
<div className={articlesInSection['title']}>
<time dateTime={`${article.publishedAt}`} className={articlesInSection['time']}>
{`${toJaJpDottedDateString(convertUnixtimeToDate(article.publishedAt))}`} - &nbsp;
{`${toJaJpDottedDateString(toDate(article.publishedAt))}`} - &nbsp;
</time>
<Link href={`${article.path}`} prefetch={false} className='unstyled'>
{`${article.title}`}
Expand Down
2 changes: 1 addition & 1 deletion src/components/content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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}) => {

Expand Down
4 changes: 2 additions & 2 deletions src/components/cover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => {
Expand All @@ -23,7 +23,7 @@ export const CoverComponent: React.FunctionComponent<{ contentCover: ContentCove
if (contentCover.publishedAt) {
return(
<span className={`${styles['content-meta']}`}>
{ convertUnixtimeToDate(contentCover.publishedAt).toUTCString() }
{ toDate(contentCover.publishedAt).toUTCString() }
</span>
)
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/headmeta.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ const HeadMetaComponent: React.FunctionComponent<{
<meta name="description" content={content.description} key="description" />
<meta property="og:description" content={content.description} key="og:description" />
<meta property="og:title" content={content.title} key="og:title" />
<meta property="article:published_time" content={convertUnixTimeToISODateSrting(content.publishedAt)} key="article:published_time" />
<meta property="article:modified_time" content={convertUnixTimeToISODateSrting(content.updatedAt)} key="article:modified_time" />
<meta property="article:published_time" content={toISODateSrting(content.publishedAt)} key="article:published_time" />
<meta property="article:modified_time" content={toISODateSrting(content.updatedAt)} key="article:modified_time" />
</>
);
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/components/searchResult.tsx
Original file line number Diff line number Diff line change
@@ -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<{
Expand All @@ -21,7 +21,7 @@ export const SearchResultComponent: React.FunctionComponent<{
<div className={styles['wrap']}>
<div className={styles['header']}>
<time dateTime={`${content.publishedAt}`} className={styles['time']}>
{`${toJaJpDottedDateString(convertUnixtimeToDate(content.publishedAt))}`}
{`${toJaJpDottedDateString(toDate(content.publishedAt))}`}
</time>
<Link href={`${content.path}`} prefetch={false} className='unstyled' target="_blank">
<h3 className={styles['title']}>
Expand Down
8 changes: 4 additions & 4 deletions src/services/feeds.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -13,7 +13,7 @@ export async function generateFeedsString(url: string, siteName: string, mainAut
<title>${siteName}</title>
<link href="${url}${FEED_URL}" rel="self"/>
<link href="${url}"/>
<updated>${convertUnixTimeToISODateSrting(latest.updated)}</updated>
<updated>${toISODateSrting(latest.updated)}</updated>
<id>${url}</id>
<author>
<name>${mainAuthor}</name>
Expand All @@ -28,8 +28,8 @@ export async function generateFeedsString(url: string, siteName: string, mainAut
<title>${f.title}</title>
<link href="${u}" />
<id>${u}</id>
<published>${convertUnixTimeToISODateSrting(f.published)}</published>
<updated>${convertUnixTimeToISODateSrting(f.updated)}</updated>
<published>${toISODateSrting(f.published)}</published>
<updated>${toISODateSrting(f.updated)}</updated>
</entry>
`
});
Expand Down
2 changes: 1 addition & 1 deletion src/services/theme.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function getThemeSetting(): string {
export function getTheme(): string {
let theme = 'light';
try {
theme = localStorage.getItem('theme');
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/utils/requestContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/utils/time.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
export function convertUnixtimeToDate(unixTime: number): Date {
export function toDate(unixTime: number): Date {
try {
return new Date(unixTime * 1000);
} catch {
return new Date();
}
}

export function convertUnixtimeToLocalDateSrting(unixTime: number): String {
export function toLocalDateSrting(unixTime: number): String {
try {
return new Date(unixTime * 1000).toLocaleDateString();
} catch {
return new Date().toLocaleDateString();
}
}

export function convertUnixTimeToISODateSrting(unixTime: number): string {
export function toISODateSrting(unixTime: number): string {
try {
return new Date(unixTime * 1000).toISOString();
} catch {
Expand Down

0 comments on commit 29a51de

Please sign in to comment.