Skip to content

Commit

Permalink
added template fn
Browse files Browse the repository at this point in the history
  • Loading branch information
djalmajr committed Feb 8, 2022
1 parent 5ce7703 commit 014b53d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/flow.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export function flow(...fns: Function[]) {
return (x: unknown) => fns.reduce((y, f) => f(y), x);
export function flow<R extends unknown>(...fns: Function[]) {
return (x: unknown) => fns.reduce((y, f) => f(y), x) as R;
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export * from './pick';
export * from './set';
export * from './size';
export * from './sortBy';
export * from './template';
export * from './times';
export * from './uniq';
export * from './uniqBy';
Expand Down
16 changes: 16 additions & 0 deletions src/template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { curry } from './curry';
import { get } from './get';

const regex = /{{(.+?)}}/g;

interface FnTemplate {
<T extends object>(str: string, obj: T): string;
<T extends object>(str: string): (obj: T) => string;
test(str: string): boolean;
}

export const template: FnTemplate = curry((str: string, obj: any) => {
return str.replace(regex, (_, k) => get(k, obj) || '');
}) as FnTemplate;

template.test = (str: string) => regex.test(str);

0 comments on commit 014b53d

Please sign in to comment.