Skip to content

Commit

Permalink
perf(rstream-log): Level => const enum
Browse files Browse the repository at this point in the history
- export `__Level` for reverse lookups
  • Loading branch information
postspectacular committed Sep 24, 2018
1 parent 8e4fc90 commit fc6a4d3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
7 changes: 6 additions & 1 deletion packages/rstream-log/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IID } from "@thi.ng/api/api";
import { ISubscribable } from "@thi.ng/rstream/api";

export enum Level {
export const enum Level {
FINE,
DEBUG,
INFO,
Expand All @@ -10,6 +10,11 @@ export enum Level {
NONE,
}

/**
* Reverse lookup for `Level` enums
*/
export const __Level = (<any>exports).Level;

export interface LogEntry extends Array<any> {
[0]: Level;
[1]: string;
Expand Down
6 changes: 3 additions & 3 deletions packages/rstream-log/src/xform/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { Transducer } from "@thi.ng/transducers/api";
import { map } from "@thi.ng/transducers/xform/map";

import {
__Level,
BodyFormat,
DateFormat,
Level,
LogEntry,
LogEntryObj
} from "../api";
Expand All @@ -14,7 +14,7 @@ export function formatString(dtFmt?: DateFormat, bodyFmt?: BodyFormat): Transduc
bodyFmt = bodyFmt || ((x) => x.toString());
return map(
([level, id, time, ...body]) =>
`[${Level[level]}] [${id}] ${dtFmt(time)} ${bodyFmt(body)}`
`[${__Level[level]}] [${id}] ${dtFmt(time)} ${bodyFmt(body)}`
);
}

Expand All @@ -29,7 +29,7 @@ export function formatJSON(dtfmt?: (dt: number) => string): Transducer<LogEntry,
return map(
([level, id, time, ...body]) =>
JSON.stringify({
level: Level[level],
level: __Level[level],
id,
time: dtfmt(time),
body
Expand Down

0 comments on commit fc6a4d3

Please sign in to comment.