Skip to content

Commit

Permalink
chore: start the process to get it onto jsr
Browse files Browse the repository at this point in the history
  • Loading branch information
maraisr committed May 20, 2024
1 parent 8fa3fdf commit b52293f
Show file tree
Hide file tree
Showing 16 changed files with 343 additions and 411 deletions.
24 changes: 24 additions & 0 deletions src/generic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/// <reference lib="dom" />

import { diary as _diary, type LogEvent, type Reporter, type Diary } from './logger';

function reporter(event: LogEvent) {
let label = '';
const fn = console[event.level === 'fatal' ? 'error' : event.level];

if (event.name) label += `[${event.name}] `;

if (typeof event.messages[0] === 'object') {
return void fn(label, ...event.messages);
} else {
const message = event.messages.shift();
return void fn(label + message, ...event.messages);
}
}

export const diary = (name: string, onEmit: Reporter = reporter): Diary => _diary(name, onEmit);

const { fatal, error, warn, debug, info, log } = diary('', reporter);
export { fatal, error, warn, debug, info, log };
export type { Diary, LogEvent, LogLevels, Reporter } from './logger';
export { enable } from './logger';
2 changes: 0 additions & 2 deletions src/global.d.ts

This file was deleted.

234 changes: 116 additions & 118 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,163 +1,161 @@
import {suite, test} from 'uvu';
import * as assert from 'uvu/assert';
import * as diary from '.';
import { enable } from '.';
import { describe } from '../test/helpers';
import { restoreAll, spy, spyOn } from 'nanospy';
import type { Reporter } from 'diary';

import * as diary from './logger'
import type {Reporter} from './logger'

import * as GENERIC from './generic';
import * as NODE from './node';

const levels = ['fatal', 'error', 'warn', 'debug', 'info', 'log'] as const;

describe('api', (it) => {
it.after.each(() => {
restoreAll();
});
function before() {
diary.enable('*');
restoreAll();
}

test.before.each(before);

it('exports', () => {
Object.entries({ generic: GENERIC, node: NODE }).forEach(([name, mod]) => {
const s = suite(`mod :: ${name}`);
s.before.each(before);

s('exports', () => {
[...levels, 'diary'].forEach((verb) => {
assert.type(
// @ts-ignore
diary[verb],
mod[verb],
'function',
`Expected diary to have #${verb} function`,
);
});
});

it('default diary should not be named', () => {
const log_output = spyOn(console, 'info', () => {});
s.run();
});

diary.info('info');
assert.equal(log_output.calls[0].join(''), 'ℹ info info');
diary.diary('named').info('info');
assert.equal(log_output.calls[1].join(''), 'ℹ info [named] info');
});
test('should allow object logging', () => {
const reporter = spy<Reporter>();
const scope = diary.diary('error', reporter);

it('#error persist', () => {
const reporter = spy<Reporter>();
const scope = diary.diary('error', reporter);
scope.info('info');
assert.equal(reporter.callCount, 1);
assert.equal(reporter.calls[0][0], 'ℹ info info');
scope.info({ foo: 'bar' });

scope.error(new Error('some error'));
assert.equal(reporter.calls[1][0], "ℹ info { foo: 'bar' }");
});

assert.equal(reporter.callCount, 1);
assert.equal(reporter.calls[0][0].messages[0].message, 'some error');
assert.instance(reporter.calls[0][0].messages[0], Error);
});
const allows = suite('allows');
allows.before.each(before);

it('should allow object logging', () => {
const log_output = spyOn(console, 'info', () => {});
allows('should only allow some scopes', () => {
const reporter = spy<Reporter>();
const scopeA = diary.diary('scope:a', reporter);
const scopeB = diary.diary('scope:b', reporter);

diary.info('info');
assert.equal(log_output.callCount, 1);
assert.equal(log_output.calls[0][0], 'ℹ info info');
diary.info({ foo: 'bar' });
diary.enable('scope:a');

assert.equal(log_output.calls[1][0], "ℹ info { foo: 'bar' }");
});
scopeA.info('info a');
scopeB.info('info b');
scopeB.info('info b');
scopeA.info('info a');

assert.equal(
reporter.calls.flatMap((i) => i[0].messages),
['info a', 'info a'],
);
});

describe('allows', (it) => {
it('should only allow some scopes', () => {
const reporter = spy<Reporter>();
const scopeA = diary.diary('scope:a', reporter);
const scopeB = diary.diary('scope:b', reporter);
allows('should allow nested scopes', () => {
const reporter = spy<Reporter>();
const scopeA = diary.diary('scope:a', reporter);
const scopeB = diary.diary('scope:b', reporter);

enable('scope:a');
diary.enable('scope:*');

scopeA.info('info a');
scopeB.info('info b');
scopeB.info('info b');
scopeA.info('info a');
scopeA.info('info a');
scopeB.info('info b');

assert.equal(
reporter.calls.flatMap((i) => i[0].messages),
['info a', 'info a'],
);
});
assert.equal(
reporter.calls.flatMap((i) => i[0].messages),
['info a', 'info b'],
);
});

it('should allow nested scopes', () => {
const reporter = spy<Reporter>();
const scopeA = diary.diary('scope:a', reporter);
const scopeB = diary.diary('scope:b', reporter);
allows('should allow multiple allows per enable', () => {
const reporter = spy<Reporter>();

enable('scope:*');
const scopeA = diary.diary('scope:a', reporter);
const scopeB = diary.diary('scope:b', reporter);

scopeA.info('info a');
scopeB.info('info b');
diary.enable('scope:a,blah');

assert.equal(
reporter.calls.flatMap((i) => i[0].messages),
['info a', 'info b'],
);
});
scopeA.info('info a');
scopeB.info('info b');

it('should allow multiple allows per enable', () => {
const reporter = spy<Reporter>();
diary.enable('blah,scope:a');

scopeA.info('info a');
scopeB.info('info b');
scopeB.info('info b');
scopeA.info('info a');

diary.enable('foo,bar:*,scope:,scope:*');

const scopeA = diary.diary('scope:a', reporter);
const scopeB = diary.diary('scope:b', reporter);
scopeA.info('info a');
scopeB.info('info b');

enable('scope:a,blah');
assert.equal(
reporter.calls.flatMap((i) => i[0].messages),
['info a', 'info a', 'info a', 'info a', 'info b'],
);
});

scopeA.info('info a');
scopeB.info('info b');
allows.run();

enable('blah,scope:a');
levels.forEach((level) => {
const l = suite(`level :: ${level}`);
l.before.each(before);

scopeA.info('info a');
scopeB.info('info b');
scopeB.info('info b');
scopeA.info('info a');
l('should log something', () => {
const reporter = spy<Reporter>();
const scope = diary.diary(level, reporter);

enable('foo,bar:*,scope:,scope:*');
scope[level]('something');
scope[level]('something else');
scope[level]('object else', { foo: 'bar' });
scope[level]({ foo: 'bar' });

scopeA.info('info a');
scopeB.info('info b');
assert.equal(reporter.callCount, 4);

assert.equal(
reporter.calls.flatMap((i) => i[0].messages),
['info a', 'info a', 'info a', 'info a', 'info b'],
reporter.calls.map((i) => i[0]),
[
{
name: level,
level: level,
messages: ['something'],
},
{
name: level,
level: level,
messages: ['something else'],
},
{
name: level,
level: level,
messages: ['object else', { foo: 'bar' }],
},
{
name: level,
level: level,
messages: [{ foo: 'bar' }],
},
],
);
});
});

levels.forEach((level) => {
describe(`level :: ${level}`, (it) => {
it('should log something', () => {
const reporter = spy<Reporter>();
const scope = diary.diary(level, reporter);

scope[level]('something');
scope[level]('something else');
scope[level]('object else', { foo: 'bar' });
scope[level]({ foo: 'bar' });

assert.equal(reporter.callCount, 4);

assert.equal(
reporter.calls.map((i) => i[0]),
[
{
name: level,
level: level,
messages: ['something'],
},
{
name: level,
level: level,
messages: ['something else'],
},
{
name: level,
level: level,
messages: ['object else', { foo: 'bar' }],
},
{
name: level,
level: level,
messages: [{ foo: 'bar' }],
},
],
);
});
});
l.run();
});
88 changes: 0 additions & 88 deletions src/index.ts

This file was deleted.

Loading

0 comments on commit b52293f

Please sign in to comment.