From 2acaad97ef2ba1a82df484d1ae1589a7b7c69d90 Mon Sep 17 00:00:00 2001 From: chad Date: Fri, 9 Jun 2023 16:28:49 -0500 Subject: [PATCH 1/2] feat: added bigint formatting --- src/index.ts | 5 +++++ test/index.spec.ts | 15 +++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 26bca95..431d0bc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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.toString() +} + export interface Logger { (formatter: any, ...args: any[]): void error: (formatter: any, ...args: any[]) => void diff --git a/test/index.spec.ts b/test/index.spec.ts index 9ad39cc..c570904 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -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') @@ -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.toString()}`) + }) + it('test ma formatter', () => { const ma = multiaddr('/ip4/127.0.0.1/tcp/4001') From e42569b2a77081fa0c8bf5125f1e71e3314cae48 Mon Sep 17 00:00:00 2001 From: chad Date: Thu, 15 Jun 2023 11:14:21 -0500 Subject: [PATCH 2/2] feat: updated big int to be formatted as locale string --- src/index.ts | 2 +- test/index.spec.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 431d0bc..1c52fa9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -44,7 +44,7 @@ debug.formatters.a = (v?: Multiaddr): string => { // Add a formatter for stringifying BigInts debug.formatters.i = (v?: bigint): string => { - return v == null ? 'undefined' : v.toString() + return v == null ? 'undefined' : v.toLocaleString() } export interface Logger { diff --git a/test/index.spec.ts b/test/index.spec.ts index c570904..7ee2580 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -95,7 +95,7 @@ describe('logger', () => { log('big int %i', bi) - expect(debugSpy.secondCall.args[0], 'Big int formatting not included').to.include(`big int ${bi.toString()}`) + expect(debugSpy.secondCall.args[0], 'Big int formatting not included').to.include(`big int ${bi.toLocaleString()}`) }) it('test ma formatter', () => {