Comparing version 4.1.0 to 5.0.0
@@ -22,2 +22,8 @@ class Format { | ||
// how many bytes to show on a line for long buffers | ||
this.bufferChunkSize = +options.bufferChunkSize || | ||
this.style.bufferChunkSize | ||
if (options.style === 'tight') | ||
this.bufferChunkSize = Infinity | ||
// for printing child values of pojos and maps | ||
@@ -82,2 +88,3 @@ this.key = options.key | ||
: this.parent.isMap() ? this.style.mapEntrySep() | ||
: this.parent.isBuffer() ? '' | ||
: this.parent.isArray() ? this.style.arrayEntrySep() | ||
@@ -96,7 +103,13 @@ : this.parent.isSet() ? this.style.setEntrySep() | ||
isKeyless () { | ||
return this.parent.isSet() || | ||
this.parent.isArray() || | ||
this.parent.isString() || | ||
this.isKey | ||
} | ||
printStart () { | ||
const indent = this.isKey ? '' | ||
: new Array(this.level).join(this.indent) | ||
const key = this.parent.isSet() || this.parent.isArray() || this.parent.isString() || this.isKey | ||
? '' | ||
const key = this.isKeyless() ? '' | ||
: this.printKey(this.key) | ||
@@ -131,4 +144,8 @@ | ||
case 'bigint': | ||
return this.object.toString() + 'n' | ||
case 'string': | ||
return this.string() | ||
case 'boolean': | ||
@@ -194,5 +211,5 @@ case 'number': | ||
: this.isMap() ? this.map() | ||
: Buffer.isBuffer(this.object) ? this.buffer() | ||
: this.isBuffer() ? this.buffer() | ||
: this.isArray() ? this.array() | ||
// TODO buffer, streams, JSX | ||
// TODO streams, JSX | ||
: this.pojo() | ||
@@ -209,9 +226,50 @@ | ||
buffer () { | ||
if (this.parent && this.parent.isBuffer()) { | ||
return this.style.bufferKey(this.key) + | ||
this.style.bufferKeySep() + | ||
this.style.bufferLine(this.object, this.bufferChunkSize) | ||
} | ||
if (this.object.length === 0) | ||
return this.style.bufferEmpty() | ||
return this.style.bufferStart() + | ||
this.style.bufferBody(this.object) + | ||
this.style.bufferEnd() | ||
if (this.bufferIsShort()) { | ||
return this.style.bufferStart() + | ||
this.style.bufferBody(this.object) + | ||
this.style.bufferEnd(this.object) | ||
} | ||
const b = this.bufferBody() | ||
return this.bufferHead() + b + this.bufferTail() | ||
} | ||
isBuffer () { | ||
return Buffer.isBuffer(this.object) | ||
} | ||
bufferIsShort () { | ||
return this.object.length < this.bufferChunkSize + 5 | ||
} | ||
bufferHead () { | ||
return this.style.bufferHead() | ||
} | ||
bufferBody () { | ||
const chunk = this.bufferChunkSize | ||
let i | ||
const lines = [] | ||
for (i = 0; i < this.object.length - chunk; i += chunk) { | ||
lines.push(this.bufferLine(i, this.object.slice(i, i + chunk))) | ||
} | ||
lines.push(this.bufferLastLine(i, this.object.slice(i, i + chunk))) | ||
return lines.join('') | ||
} | ||
bufferLine (key, val) { | ||
return this.bufferLastLine(key, val) + this.style.bufferLineSep() | ||
} | ||
bufferLastLine (key, val) { | ||
return this.child(val, { key }).print() | ||
} | ||
bufferTail () { | ||
return this.style.bufferTail(this.indentLevel()) | ||
} | ||
set () { | ||
@@ -464,2 +522,6 @@ if (this.setIsEmpty()) | ||
// can't use buf.toString('ascii') because that unmasks high bytes | ||
const bufToAscii = buf => | ||
buf.map(c => c <= 0x20 || c >= 0x7f ? '.'.charCodeAt(0) : c).toString() | ||
const styles = { | ||
@@ -507,6 +569,26 @@ pretty: { | ||
arrayEntrySep: () => ',\n', | ||
bufferChunkSize: 32, | ||
bufferEmpty: () => 'Buffer <>', | ||
bufferStart: () => 'Buffer <', | ||
bufferBody: buf => buf.toString('hex').replace(/(..)/g, '$1 ').trim(), | ||
bufferEnd: () => '>', | ||
bufferBody: buf => buf.toString('hex').replace(/(....)/g, '$1 ').trim(), | ||
bufferEnd: buf => ' ' + bufToAscii(buf) + '>', | ||
bufferHead: () => 'Buffer <\n', | ||
// show line numbers as offset 0x0000 through 0xffff as zero-padded hex | ||
// this will wrap around if you have more than 64kb buffer, but that's | ||
// (a) highly unusual for the use cases tcompare works in, and (b) fine. | ||
bufferKey: i => (i + 0x10000).toString(16).slice(-4), | ||
bufferLine: (buf, chunkSize) => { | ||
const hex = buf.toString('hex').replace(/(....)/g, '$1 ').trim() | ||
// double for hex, then add 25% for the spaces between every 4 hexits | ||
const l = Math.ceil(chunkSize * 2 * 1.25) | ||
const pad = new Array(l - hex.length).join(' ') + ' ' | ||
return hex + pad + bufToAscii(buf) | ||
}, | ||
bufferLineSep: () => '\n', | ||
bufferTail: indent => `\n${indent}>`, | ||
bufferKeySep: () => ': ', | ||
stringEmpty: () => '""', | ||
@@ -516,4 +598,5 @@ stringOneLine: str => JSON.stringify(str), | ||
stringLineSep: () => '\n', | ||
stringLine: str => JSON.stringify(str.replace(/\n$/, '')).slice(1, -1), | ||
stringTail: (indent) => `\n${indent})`, | ||
stringLine: str => JSON.stringify(str.replace(/\n$/, '')) | ||
.slice(1, -1).replace(/\\"/g, '"'), | ||
stringTail: indent => `\n${indent})`, | ||
}, | ||
@@ -547,6 +630,18 @@ | ||
arrayEntrySep: () => ',\n', | ||
bufferChunkSize: 32, | ||
bufferEmpty: () => 'Buffer.alloc(0)', | ||
bufferStart: () => 'Buffer.from("', | ||
bufferBody: buf => buf.toString('hex'), | ||
bufferEnd: () => '", "hex")', | ||
bufferEnd: buf => '", "hex") /* ' + bufToAscii(buf) + ' */', | ||
bufferHead: () => 'Buffer.from(\n', | ||
bufferKey: () => '', | ||
bufferLine: (buf, chunkSize) => JSON.stringify(buf.toString('hex')) + | ||
(new Array((chunkSize + 1) * 2 - buf.length * 2).join(' ')) + | ||
' /* ' + bufToAscii(buf) + ' */', | ||
bufferTail: indent => `\n${indent}, "hex")`, | ||
bufferLineSep: () => ' +\n', | ||
bufferKeySep: () => '', | ||
stringEmpty: () => '""', | ||
@@ -557,3 +652,3 @@ stringLineSep: () => ' +\n', | ||
stringHead: () => 'String(\n', | ||
stringTail: (indent) => `\n${indent})`, | ||
stringTail: indent => `\n${indent})`, | ||
}, | ||
@@ -588,6 +683,11 @@ | ||
arrayEntrySep: () => ',', | ||
// tight style doesn't need buffer head/tail/body, because it's | ||
// always printed as one base64 line. | ||
bufferChunkSize: Infinity, | ||
bufferEmpty: () => 'Buffer.alloc(0)', | ||
bufferStart: () => 'Buffer.from("', | ||
bufferBody: buf => buf.toString('hex'), | ||
bufferEnd: () => '", "hex")', | ||
bufferBody: buf => buf.toString('base64'), | ||
bufferEnd: () => '","base64")', | ||
stringEmpty: () => '""', | ||
@@ -594,0 +694,0 @@ stringLineSep: () => '+', |
@@ -30,2 +30,3 @@ const Format = require('./format.js') | ||
: pattern === Boolean ? typeof obj === 'boolean' | ||
: pattern === BigInt ? typeof obj === 'bigint' | ||
: pattern === Map ? this.isMap() | ||
@@ -32,0 +33,0 @@ : pattern === Set ? this.isSet() |
{ | ||
"name": "tcompare", | ||
"version": "4.1.0", | ||
"version": "5.0.0", | ||
"description": "A comprehensive comparison library, for use in test frameworks", | ||
@@ -17,3 +17,3 @@ "main": "index.js", | ||
"devDependencies": { | ||
"tap": "^14.9.2" | ||
"tap": "^14.10.2" | ||
}, | ||
@@ -25,3 +25,3 @@ "scripts": { | ||
"postversion": "npm publish", | ||
"postpublish": "git push origin --all; git push origin --tags" | ||
"prepublishOnly": "git push origin --follow-tags" | ||
}, | ||
@@ -32,3 +32,5 @@ "tap": { | ||
}, | ||
"dependencies": {} | ||
"engines": { | ||
"node": ">=10" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
46456
1315