Skip to content

Commit

Permalink
reafactor: better function type
Browse files Browse the repository at this point in the history
  • Loading branch information
Pcrab committed Mar 27, 2023
1 parent 5ae2313 commit 4c99a3d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
6 changes: 5 additions & 1 deletion lib/extend/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ type Option = Partial<{
}[];
}>

type AnyFn = (args: any[]) => any;
interface Args {
_: string[];
[key: string]: string | boolean | string[];
}
type AnyFn = (args: Args) => any;
interface StoreFunction extends AnyFn {
desc?: string;
options?: Option;
Expand Down
2 changes: 1 addition & 1 deletion lib/extend/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface FilterOptions {
}

interface StoreFunction {
(...args: any[]): any;
(data?: any, ...args: any[]): any;
priority?: number;
}

Expand Down
22 changes: 18 additions & 4 deletions lib/extend/generator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
import Promise from 'bluebird';

interface BaseObj {
path: string;
data: any;
layout?: string;
}
type ReturnType = BaseObj | BaseObj[];
type GeneratorReturnType = ReturnType | Promise<ReturnType>;

interface GeneratorFunction {
(locals: object): GeneratorReturnType;
}

type StoreFunctionReturn = Promise<ReturnType>;

interface StoreFunction {
(...args: any[]): any;
(locals: object): StoreFunctionReturn;
}

interface Store {
Expand All @@ -25,9 +39,9 @@ class Generator {
return this.store[name];
}

register(fn: StoreFunction): void
register(name: string, fn: StoreFunction): void
register(name: string | StoreFunction, fn?: StoreFunction) {
register(fn: GeneratorFunction): void
register(name: string, fn: GeneratorFunction): void
register(name: string | GeneratorFunction, fn?: GeneratorFunction) {
if (!fn) {
if (typeof name === 'function') { // fn
fn = name;
Expand Down
2 changes: 1 addition & 1 deletion lib/extend/helper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
interface StoreFunction {
(...args: any[]): any;
(...args: any[]): string;
}

interface Store {
Expand Down
5 changes: 3 additions & 2 deletions lib/extend/processor.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Promise from 'bluebird';
import { Pattern } from 'hexo-util';
import File from '../box/file';

interface StoreFunction {
(args: any): any
(file: File): any
}

type Store = {
Expand Down Expand Up @@ -41,7 +42,7 @@ class Processor {
}

this.store.push({
pattern: new Pattern(pattern),
pattern: new Pattern(pattern as patternType),
process: fn
});
}
Expand Down

0 comments on commit 4c99a3d

Please sign in to comment.