Skip to content

Commit

Permalink
🚧 WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
maraisr committed May 22, 2024
1 parent 07a0b8a commit ef2e39d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 28 deletions.
44 changes: 22 additions & 22 deletions migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,33 @@ let v4Events: any[] = [];
let v5Events: any[] = [];

{
v4.enable('*');
const log = v4.diary("v0.4", event => {
v4Events.push(event);
});
v4.enable('*');
const log = v4.diary('v0.4', (event) => {
v4Events.push(event);
});

log.log("hello %s", "world", "extra", "props");
log.debug("hello %s", "world", "extra", "props");
log.info("hello %s", "world", "extra", "props");
log.warn("hello %s", "world", "extra", "props");
log.error("hello %s", "world", "extra", "props");
log.fatal("hello %s", "world", "extra", "props");
log.log('hello %s', 'world', 'extra', 'props');
log.debug('hello %s', 'world', 'extra', 'props');
log.info('hello %s', 'world', 'extra', 'props');
log.warn('hello %s', 'world', 'extra', 'props');
log.error('hello %s', 'world', 'extra', 'props');
log.fatal('hello %s', 'world', 'extra', 'props');
}

{
const log = v5.diary("v0.5", (...event: any[]) => {
v5Events.push(event);
});
const log = v5.diary('v0.5', (name, level, event, props) => {
v5Events.push({ name, level, messages: [event, props] });
});

log("log", "hello {phrase}", {phrase: "world", extra: ["extra", "props"]});
log("debug", "hello {phrase}", {phrase: "world", extra: ["extra", "props"]});
log("info", "hello {phrase}", {phrase: "world", extra: ["extra", "props"]});
log("warn", "hello {phrase}", {phrase: "world", extra: ["extra", "props"]});
log("error", "hello {phrase}", {phrase: "world", extra: ["extra", "props"]});
log("fatal", "hello {phrase}", {phrase: "world", extra: ["extra", "props"]});
log('log', 'hello {phrase}', { phrase: 'world', extra: ['extra', 'props'] });
log('debug', 'hello {phrase}', { phrase: 'world', extra: ['extra', 'props'] });
log('info', 'hello {phrase}', { phrase: 'world', extra: ['extra', 'props'] });
log('warn', 'hello {phrase}', { phrase: 'world', extra: ['extra', 'props'] });
log('error', 'hello {phrase}', { phrase: 'world', extra: ['extra', 'props'] });
log('fatal', 'hello {phrase}', { phrase: 'world', extra: ['extra', 'props'] });
}

console.log({
v4Events,
v5Events
})
v4Events,
v5Events,
});
1 change: 1 addition & 0 deletions src/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface OnEmitFn {
}

export interface LogFn {
// TODO: how can we require props if there is a template in the event?
<const T extends string, P extends Props<T>>(level: Level, event: T, props: P): void;
}

Expand Down
16 changes: 10 additions & 6 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
export type Props<T> = T extends `${string}{${infer U}}${infer Rest}`
? { [K in U]: unknown } & Props<Rest>
: Record<string, unknown>;
type Evaluate<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;

export type Props<T> = Evaluate<
T extends `${string}{${infer U extends string}}${infer Rest extends string}`
? { [K in U]: unknown } & Props<Rest>
: unknown
>;

export function interpolate<const T extends string, O extends Props<T>>(
event: T,
props: O,
to_string: (v: keyof O) => string = String,
to_string: (v: O[keyof O], key: keyof O) => string = String,
): string {
return event.replaceAll(/\{([^}]+)\}/g, (_, key) => {
if (!hasOwn(props, key)) return `{${key}}`;
return to_string(props[key] as keyof O);
return to_string(props[key], key);
});
}

function hasOwn(obj: unknown, key: string) {
function hasOwn<T extends Record<string, unknown>>(obj: T, key: PropertyKey): key is keyof T {
return Object.prototype.hasOwnProperty.call(obj, key);
}

0 comments on commit ef2e39d

Please sign in to comment.