Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

feat: added bigint formatting #37

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ debug.formatters.a = (v?: Multiaddr): string => {
return v == null ? 'undefined' : v.toString()
}

// Add a formatter for stringifying BigInts
debug.formatters.i = (v?: bigint): string => {
return v == null ? 'undefined' : v.toLocaleString()
}

export interface Logger {
(formatter: any, ...args: any[]): void
error: (formatter: any, ...args: any[]) => void
Expand Down
15 changes: 13 additions & 2 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { Key } from 'interface-datastore'
import sinon from 'sinon'

describe('logger', () => {
const debugSpy = sinon.spy(debug, 'log')

it('creates a logger', () => {
const log = logger('hello')

Expand Down Expand Up @@ -80,13 +82,22 @@ describe('logger', () => {

const ma = multiaddr('/ip4/127.0.0.1/tcp/4001')

const debugSpy = sinon.spy(debug, 'log')

log('multiaddr %a', ma)

expect(debugSpy.firstCall.args[0], 'Multiaddr formatting not included').to.include(`multiaddr ${ma.toString()}`)
})

it('test printf style formatting for big int', () => {
const log = logger('printf-style')
debug.enable('printf-style')

const bi = BigInt(12345678901234567168)

log('big int %i', bi)

expect(debugSpy.secondCall.args[0], 'Big int formatting not included').to.include(`big int ${bi.toLocaleString()}`)
})

it('test ma formatter', () => {
const ma = multiaddr('/ip4/127.0.0.1/tcp/4001')

Expand Down