Skip to content

Commit

Permalink
chore: separate result type & options as optional
Browse files Browse the repository at this point in the history
optional options by adding default options empty object
prevent object assign to undefined options variable

fix: TS2345

Argument of type 'string' is not assignable to parameter of type 'never'
  • Loading branch information
dimaslanjaka committed May 13, 2023
1 parent f8fcdb6 commit b7050e8
Showing 1 changed file with 73 additions and 27 deletions.
100 changes: 73 additions & 27 deletions lib/front_matter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,61 @@ function split(str: string) {
return { content: str };
}

function parse(str: string, options?: yaml.LoadOptions) {
/**
* Hexo Front Matter Parse Result
* * required while used in hexo plugins or themes
* * to identify page metadata properties
*/
export type HexoFMResult = Partial<{

/**
* title of page
*/
title: string;

/**
* description of page
*/
description: string;

/**
* thumbnail of page
*/
thumbnail: string;

/**
* page created date
*/
date: any;

/**
* page modified date
*/
updated: any;

/**
* page permalink
*/
permalink: string;
}>;
export type ParseResult = Record<string, any> & {
_content: string;
} & HexoFMResult;

function parse(str: string, options: yaml.LoadOptions = {}) {
if (typeof str !== 'string') throw new TypeError('str is required!');

const splitData = split(str);
const raw = splitData.data;

if (!raw) return { _content: str };

let data;
let data: ParseResult;

if (splitData.separator.startsWith(';')) {
data = parseJSON(raw);
} else {
data = parseYAML(raw, options);
data = parseYAML(raw, options) as any;
}

if (!data) return { _content: str };
Expand All @@ -55,26 +96,28 @@ function parse(str: string, options?: yaml.LoadOptions) {
const item = data[key];

if (item instanceof Date) {
data[key] = new Date(item.getTime() + (item.getTimezoneOffset() * 60 * 1000));
data[key] = new Date(
item.getTime() + (item.getTimezoneOffset() * 60 * 1000)
);
}
});

data._content = splitData.content;
return data;
}

function parseYAML(str, options: yaml.LoadOptions) {
function parseYAML(str: string, options: yaml.LoadOptions) {
const result = yaml.load(escapeYAML(str), options);
if (typeof result !== 'object') return;

return result;
}

function parseJSON(str) {
function parseJSON(str: string) {
try {
return JSON.parse(`{${str}}`);
} catch (err) {
return; // eslint-disable-line
return; // eslint-disable-line
}
}

Expand All @@ -93,12 +136,12 @@ function escapeYAML(str: string) {
}

interface Options {
mode?: 'json' | '',
prefixSeparator?: boolean,
separator?: string
mode?: 'json' | '';
prefixSeparator?: boolean;
separator?: string;
}

function stringify(obj, options: Options = {}) {
function stringify(obj: Record<string, any>, options: Options = {}): string {
if (!obj) throw new TypeError('obj is required!');

const { _content: content = '' } = obj;
Expand All @@ -123,12 +166,14 @@ function stringify(obj, options: Options = {}) {
return result;
}

function stringifyYAML(obj, options) {
type YamlMergedOpts = Options & yaml.DumpOptions;

function stringifyYAML(obj: Record<string, any>, options: YamlMergedOpts) {
const keys = Object.keys(obj);
const data = {};
const nullKeys = [];
const dateKeys = [];
let key, value, i, len;
const nullKeys: string[] = [];
const dateKeys: string[] = [];
let key: string, value: any, i: number, len: number;

for (i = 0, len = keys.length; i < len; i++) {
key = keys[i];
Expand Down Expand Up @@ -162,24 +207,25 @@ function stringifyYAML(obj, options) {
}

function stringifyJSON(obj) {
return JSON.stringify(obj, null, ' ')
return (
JSON.stringify(obj, null, ' ')
// Remove indention
.replace(/\n {2}/g, () => '\n')
.replace(/\n {2}/g, () => '\n')
// Remove prefixing and trailing braces
.replace(/^{\n|}$/g, '');
.replace(/^{\n|}$/g, '')
);
}

function doubleDigit(num) {
function doubleDigit(num: number) {
return num.toString().padStart(2, '0');
}

function formatDate(date) {
return `${date.getFullYear()}-${doubleDigit(date.getMonth() + 1)}-${doubleDigit(date.getDate())} ${doubleDigit(date.getHours())}:${doubleDigit(date.getMinutes())}:${doubleDigit(date.getSeconds())}`;
function formatDate(date: Date) {
return `${date.getFullYear()}-${doubleDigit(
date.getMonth() + 1
)}-${doubleDigit(date.getDate())} ${doubleDigit(
date.getHours()
)}:${doubleDigit(date.getMinutes())}:${doubleDigit(date.getSeconds())}`;
}

export {
parse,
split,
escapeYAML as escape,
stringify
};
export { parse, split, escapeYAML as escape, stringify };

0 comments on commit b7050e8

Please sign in to comment.