Improved handling of null values when entering tags frontmatter
This commit is contained in:
@@ -4,7 +4,7 @@ import { TPostListItem, TTagSubPostSet } from "@/types/post-list";
|
||||
import fs from "fs";
|
||||
import { serialize } from "next-mdx-remote/serialize";
|
||||
import path from "path";
|
||||
import { capitalizeFirstLetter, nullifyEmptyString } from "./utils";
|
||||
import { isEmptyString, nullifyEmptyArray, nullifyEmptyString } from "./utils";
|
||||
|
||||
async function getFrontmatters(filepath: string): Promise<TFrontmatter> {
|
||||
const source = fs.readFileSync(filepath, "utf-8");
|
||||
@@ -45,7 +45,10 @@ const sortOutPostLists = async (): Promise<{
|
||||
for (let i = 0; i < postFilePaths.length; i++) {
|
||||
const frontmatter = await getFrontmatters(postFilePaths[i]);
|
||||
const postId = path.parse(postFilePaths[i]).name;
|
||||
const normalizedTags = frontmatter.tags?.map((tagname) => tagname.toUpperCase());
|
||||
|
||||
const normalizedTags = frontmatter.tags
|
||||
?.filter((tagname) => !isEmptyString(tagname))
|
||||
.map((tagname) => tagname.toUpperCase());
|
||||
|
||||
const postListItem: TPostListItem = {
|
||||
id: postId,
|
||||
@@ -53,7 +56,7 @@ const sortOutPostLists = async (): Promise<{
|
||||
title: frontmatter.title,
|
||||
subtitle: nullifyEmptyString(frontmatter.subtitle),
|
||||
coverURL: nullifyEmptyString(frontmatter.coverURL),
|
||||
tags: normalizedTags ?? [],
|
||||
tags: nullifyEmptyArray(normalizedTags),
|
||||
summary: nullifyEmptyString(frontmatter.summary),
|
||||
time: frontmatter.time,
|
||||
pin: frontmatter.pin ?? false,
|
||||
|
||||
46
lib/utils.ts
46
lib/utils.ts
@@ -1,3 +1,4 @@
|
||||
import { NonEmptyArray } from "@/types/utils.type";
|
||||
import { clsx, type ClassValue } from "clsx";
|
||||
import { twMerge } from "tailwind-merge";
|
||||
|
||||
@@ -33,13 +34,10 @@ export function paginateArray<T = any>(array: T[], pageSize: number, pageNumber:
|
||||
* @returns return `null` if the input belongs to "", undefined and null.
|
||||
*/
|
||||
export function nullifyEmptyString(value: string | null | undefined): string | null {
|
||||
if (value == null) {
|
||||
if (isEmptyString(value)) {
|
||||
return null;
|
||||
}
|
||||
if (value.trim() === "") {
|
||||
return null;
|
||||
}
|
||||
return value;
|
||||
return value!;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -48,7 +46,7 @@ export function nullifyEmptyString(value: string | null | undefined): string | n
|
||||
* @returns return `true` if the input belongs to "", undefined and null.
|
||||
*/
|
||||
export function isEmptyString(value: string | null | undefined): boolean {
|
||||
if (value == null) {
|
||||
if (value === null || value === undefined) {
|
||||
return true;
|
||||
}
|
||||
if (value.trim() === "") {
|
||||
@@ -57,6 +55,42 @@ export function isEmptyString(value: string | null | undefined): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes empty values from an array.
|
||||
* @param value - The array to remove empty values from.
|
||||
* @returns The array without empty values.
|
||||
* @template T - The type of the array elements.
|
||||
*/
|
||||
export function removeEmptyValuesFromArray<T = any>(value: any[]): T[] {
|
||||
return value.filter((item) => item != null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if an array is empty.
|
||||
* @param value - The array to check.
|
||||
* @returns True if the array is empty, false otherwise.
|
||||
*/
|
||||
export function isEmptyArray(value: any[] | null | undefined): boolean {
|
||||
if (value === null || value === undefined) {
|
||||
return true;
|
||||
}
|
||||
return removeEmptyValuesFromArray(value).length !== 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Nullifies an empty array.
|
||||
*
|
||||
* @template T - The type of the array elements.
|
||||
* @param value - The array value to be nullified if empty.
|
||||
* @returns The nullified array if it is empty, otherwise returns the original array.
|
||||
*/
|
||||
export function nullifyEmptyArray<T>(value: T[] | null | undefined): NonEmptyArray<T> | null {
|
||||
if (isEmptyArray(value)) {
|
||||
return null;
|
||||
}
|
||||
return value as NonEmptyArray<T>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Capitalizes the first letter of each word in a string.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user