Skip to content

Instantly share code, notes, and snippets.

@isaacs
Created February 13, 2021 01:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacs/d5263c46586310a68aafa5246c6694f7 to your computer and use it in GitHub Desktop.
Save isaacs/d5263c46586310a68aafa5246c6694f7 to your computer and use it in GitHub Desktop.
diff --git a/lib/format.js b/lib/format.js
index 79cad68..9c7b9eb 100644
--- a/lib/format.js
+++ b/lib/format.js
@@ -125,8 +125,12 @@ class Format {
case 'symbol':
return this.object.toString()
+ case 'bigint':
+ return this.object.toString() + 'n'
+
case 'string':
return this.string()
+
case 'boolean':
case 'number':
return JSON.stringify(this.object)
diff --git a/lib/match.js b/lib/match.js
index 8695e7e..3ee9e27 100644
--- a/lib/match.js
+++ b/lib/match.js
@@ -27,6 +27,7 @@ class Match extends Has {
: pattern === String ? typeof obj === 'string'
: pattern === Symbol ? typeof obj === 'symbol'
: pattern === Boolean ? typeof obj === 'boolean'
+ : pattern === BigInt ? typeof obj === 'bigint'
: pattern === Map ? this.isMap()
: pattern === Set ? this.isSet()
: pattern === Object ? obj && typeof obj === 'object'
diff --git a/tap-snapshots/test-format.js-TAP.test.js b/tap-snapshots/test-format.js-TAP.test.js
index 6ddbb5c..362346c 100644
--- a/tap-snapshots/test-format.js-TAP.test.js
+++ b/tap-snapshots/test-format.js-TAP.test.js
@@ -279,7 +279,7 @@ exports[`test/format.js TAP gnarly object, many points of view > js 1`] = `
"fancyErr": Object.assign(new Error("fancy pantsy"), {
"fancy": "pantsy",
}),
- "assert": Object.assign(new AssertionError("{\\n a: 1,\\n k: {\\n i: {\\n i: 1,\\n k: [Circular]\\n },\\n k: [Circular]\\n }\\n} == {\\n o: true\\n}"), {
+ "assert": Object.assign(new AssertionError("<ref *1> {\\n a: 1,\\n k: <ref *2> {\\n i: {\\n i: 1,\\n k: [Circular *2]\\n },\\n k: [Circular *1]\\n }\\n} == {\\n o: true\\n}"), {
"generatedMessage": true,
"code": "ERR_ASSERTION",
"actual": &ref_2 {
@@ -617,7 +617,7 @@ Object {
`
exports[`test/format.js TAP gnarly object, many points of view > tight 1`] = `
-&1 {"a":1,"b":2,"extra":true,"c":3,"d":4,"more":false,"e":{"f":{"g":1,},"a":[2,3,4,],"h":"asdf","multilineString":"this is a line\\n"+"this is a line\\n"+"this is a line\\n"+"this is a line\\n"+"\\n","emptyString":"",},"nullObject":{"x":{},"y":{},"z":{"zed":true,},},"p":new Set([{"x":"y","z":true,},{"a":1,},{"b":2,},]),"s":new Set([{"b":2,},{"c":3,},*1,]),"m":new Map([[&2 {"a":1,"k":&3 {"k":*2,"i":{"i":1,"k":*3,},},"f":*1,"c":&4 {"c":{"c":*4,"b":{"b":1,"d":*4,},},"a":&5 [1,*4,*5,],"k":*2,"f":*1,},},1],[{"b":2,},2],[{"c":"d",},{"re":/ef/g,}],[1,&4 {"c":{"c":*4,"b":{"b":1,"d":*4,},},"a":&5 [1,*4,*5,],"k":&2 {"a":1,"k":&3 {"k":*2,"i":{"i":1,"k":*3,},},"f":*1,"c":*4,},"f":*1,}],[*1,&2 {"a":1,"k":&3 {"k":*2,"i":{"i":1,"k":*3,},},"f":*1,"c":&4 {"c":{"c":*4,"b":{"b":1,"d":*4,},},"a":&5 [1,*4,*5,],"k":*2,"f":*1,},}],]),"ao":[{"o":true,},{"o":true,},[{"o":true,},{"o":true,},],],"om":new Map([[{"o":true,},&2 {"a":1,"k":&3 {"k":*2,"i":{"i":1,"k":*3,},},"f":*1,"c":&4 {"c":{"c":*4,"b":{"b":1,"d":*4,},},"a":&5 [1,*4,*5,],"k":*2,"f":*1,},}],[&2 {"a":1,"k":&3 {"k":*2,"i":{"i":1,"k":*3,},},"f":*1,"c":&4 {"c":{"c":*4,"b":{"b":1,"d":*4,},},"a":&5 [1,*4,*5,],"k":*2,"f":*1,},},{"o":true,}],]),"args":[1,2,3,{"o":true,},],"buf":Buffer.from("686f776479", "hex"),"emp":{"b":Buffer.alloc(0),"a":[],"o":{},"m":new Map(),"s":new Set(),},"fns":{"name":function foo () {},"anon":function () {},"arr":() => {},"identity":x => x,"nameless":() => {},},"sym":Symbol(prince),"date":2019-02-14T07:13:44.100Z,"n":null,"undef":undefined,"classy":{},"err":new Error("just an error"),"emptyErr":new Error(""),"fancyErr":Object.assign(new Error("fancy pantsy"), {"fancy":"pantsy",}),"assert":Object.assign(new AssertionError("{\\n a: 1,\\n k: {\\n i: {\\n i: 1,\\n k: [Circular]\\n },\\n k: [Circular]\\n }\\n} == {\\n o: true\\n}"), {"generatedMessage":true,"code":"ERR_ASSERTION","actual":&2 {"a":1,"k":&3 {"k":*2,"i":{"i":1,"k":*3,},},"f":*1,"c":&4 {"c":{"c":*4,"b":{"b":1,"d":*4,},},"a":&5 [1,*4,*5,],"k":*2,"f":*1,},},"expected":{"o":true,},"operator":"==",}),}
+&1 {"a":1,"b":2,"extra":true,"c":3,"d":4,"more":false,"e":{"f":{"g":1,},"a":[2,3,4,],"h":"asdf","multilineString":"this is a line\\n"+"this is a line\\n"+"this is a line\\n"+"this is a line\\n"+"\\n","emptyString":"",},"nullObject":{"x":{},"y":{},"z":{"zed":true,},},"p":new Set([{"x":"y","z":true,},{"a":1,},{"b":2,},]),"s":new Set([{"b":2,},{"c":3,},*1,]),"m":new Map([[&2 {"a":1,"k":&3 {"k":*2,"i":{"i":1,"k":*3,},},"f":*1,"c":&4 {"c":{"c":*4,"b":{"b":1,"d":*4,},},"a":&5 [1,*4,*5,],"k":*2,"f":*1,},},1],[{"b":2,},2],[{"c":"d",},{"re":/ef/g,}],[1,&4 {"c":{"c":*4,"b":{"b":1,"d":*4,},},"a":&5 [1,*4,*5,],"k":&2 {"a":1,"k":&3 {"k":*2,"i":{"i":1,"k":*3,},},"f":*1,"c":*4,},"f":*1,}],[*1,&2 {"a":1,"k":&3 {"k":*2,"i":{"i":1,"k":*3,},},"f":*1,"c":&4 {"c":{"c":*4,"b":{"b":1,"d":*4,},},"a":&5 [1,*4,*5,],"k":*2,"f":*1,},}],]),"ao":[{"o":true,},{"o":true,},[{"o":true,},{"o":true,},],],"om":new Map([[{"o":true,},&2 {"a":1,"k":&3 {"k":*2,"i":{"i":1,"k":*3,},},"f":*1,"c":&4 {"c":{"c":*4,"b":{"b":1,"d":*4,},},"a":&5 [1,*4,*5,],"k":*2,"f":*1,},}],[&2 {"a":1,"k":&3 {"k":*2,"i":{"i":1,"k":*3,},},"f":*1,"c":&4 {"c":{"c":*4,"b":{"b":1,"d":*4,},},"a":&5 [1,*4,*5,],"k":*2,"f":*1,},},{"o":true,}],]),"args":[1,2,3,{"o":true,},],"buf":Buffer.from("686f776479", "hex"),"emp":{"b":Buffer.alloc(0),"a":[],"o":{},"m":new Map(),"s":new Set(),},"fns":{"name":function foo () {},"anon":function () {},"arr":() => {},"identity":x => x,"nameless":() => {},},"sym":Symbol(prince),"date":2019-02-14T07:13:44.100Z,"n":null,"undef":undefined,"classy":{},"err":new Error("just an error"),"emptyErr":new Error(""),"fancyErr":Object.assign(new Error("fancy pantsy"), {"fancy":"pantsy",}),"assert":Object.assign(new AssertionError("<ref *1> {\\n a: 1,\\n k: <ref *2> {\\n i: {\\n i: 1,\\n k: [Circular *2]\\n },\\n k: [Circular *1]\\n }\\n} == {\\n o: true\\n}"), {"generatedMessage":true,"code":"ERR_ASSERTION","actual":&2 {"a":1,"k":&3 {"k":*2,"i":{"i":1,"k":*3,},},"f":*1,"c":&4 {"c":{"c":*4,"b":{"b":1,"d":*4,},},"a":&5 [1,*4,*5,],"k":*2,"f":*1,},},"expected":{"o":true,},"operator":"==",}),}
`
exports[`test/format.js TAP gnarly object, many points of view different points of view > c 1`] = `
diff --git a/tap-snapshots/test-match.js-TAP.test.js b/tap-snapshots/test-match.js-TAP.test.js
index ba8798e..130de07 100644
--- a/tap-snapshots/test-match.js-TAP.test.js
+++ b/tap-snapshots/test-match.js-TAP.test.js
@@ -512,6 +512,48 @@ exports[`test/match.js TAP should handle arguments > must match snapshot 3`] = `
`
+exports[`test/match.js TAP should handle bigints > must match snapshot 1`] = `
+--- expected
++++ actual
+@@ -1,1 +1,1 @@
+-null
++1n
+
+`
+
+exports[`test/match.js TAP should handle bigints > must match snapshot 2`] = `
+--- expected
++++ actual
+@@ -1,1 +1,1 @@
+-undefined
++1n
+
+`
+
+exports[`test/match.js TAP should handle bigints > must match snapshot 3`] = `
+--- expected
++++ actual
+
+`
+
+exports[`test/match.js TAP should handle bigints > must match snapshot 4`] = `
+--- expected
++++ actual
+
+`
+
+exports[`test/match.js TAP should handle bigints > must match snapshot 5`] = `
+--- expected
++++ actual
+
+`
+
+exports[`test/match.js TAP should handle bigints > must match snapshot 6`] = `
+--- expected
++++ actual
+
+`
+
exports[`test/match.js TAP should handle dates > must match snapshot 1`] = `
--- expected
+++ actual
diff --git a/tap-snapshots/test-same.js-TAP.test.js b/tap-snapshots/test-same.js-TAP.test.js
index c5d0c38..c9bce35 100644
--- a/tap-snapshots/test-same.js-TAP.test.js
+++ b/tap-snapshots/test-same.js-TAP.test.js
@@ -920,6 +920,27 @@ exports[`test/same.js TAP should handle arguments > must match snapshot 3`] = `
`
+exports[`test/same.js TAP should handle bigint > must match snapshot 1`] = `
+--- expected
++++ actual
+
+`
+
+exports[`test/same.js TAP should handle bigint > must match snapshot 2`] = `
+--- expected
++++ actual
+
+`
+
+exports[`test/same.js TAP should handle bigint > must match snapshot 3`] = `
+--- expected
++++ actual
+@@ -1,1 +1,1 @@
+-2n
++1n
+
+`
+
exports[`test/same.js TAP should handle dates > must match snapshot 1`] = `
--- expected
+++ actual
diff --git a/test/format.js b/test/format.js
index f52acb6..8369523 100644
--- a/test/format.js
+++ b/test/format.js
@@ -220,3 +220,8 @@ t.test('hidden props and getters', t => {
}).print(), 'all enumerable properties shown')
t.end()
})
+
+t.test('format BigInt', { skip: !global.BigInt ? 'no BigInt here' : false }, t => {
+ t.equal(new Format(BigInt('5')).print(), '5n')
+ t.end()
+})
diff --git a/test/match.js b/test/match.js
index ec2c8e7..02c8477 100644
--- a/test/match.js
+++ b/test/match.js
@@ -51,6 +51,16 @@ t.test('should handle dates', t => {
t.end()
})
+t.test('should handle bigints', t => {
+ t.notOk(match(t, BigInt('1'), null))
+ t.notOk(match(t, BigInt('1'), undefined))
+ t.ok(match(t, BigInt('1'), BigInt('1')))
+ t.ok(match(t, BigInt('1'), BigInt))
+ t.ok(match(t,{ x: BigInt('1') }, { x: BigInt('1') }))
+ t.ok(match(t,{ x: BigInt('1') }, { x: BigInt }))
+ t.end()
+})
+
t.test('should handle RegExps', t => {
t.notOk(match(t,/[b]/, /[a]/))
t.notOk(match(t,/[a]/i, /[a]/g))
diff --git a/test/same.js b/test/same.js
index 1be94fa..3102856 100644
--- a/test/same.js
+++ b/test/same.js
@@ -189,6 +189,13 @@ t.test('should handle arguments', function (t) {
t.end()
})
+t.test('should handle bigint', function (t) {
+ t.ok(same(t, BigInt('0'), BigInt('0')))
+ t.ok(same(t, BigInt('1'), BigInt('1')))
+ t.notOk(same(t, BigInt('1'), BigInt('2')))
+ t.end()
+})
+
t.test('same arrays match', function (t) {
t.ok(same(t,[1, 2, 3], [1, 2, 3]))
t.end()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment