diff --git a/index.js b/index.js index 2718b44..4f5c034 100644 --- a/index.js +++ b/index.js @@ -81,4 +81,16 @@ Object.defineProperty(ImageData.prototype, 'height', { get () { return heightMap.get(this) } }) +Object.defineProperty(ImageData.prototype, Symbol.for('nodejs.util.inspect.custom'), { + enumerable: false, + configurable: true, + value: function inspectImageData (depth, options) { + if (depth < 0) { + return options.stylize('[ImageData]', 'special') + } + + return Object.assign(new (class ImageData {})(), { data: this.data, width: this.width, height: this.height }, this) + } +}) + module.exports = ImageData diff --git a/test.js b/test.js index 2b4b44c..b379d54 100644 --- a/test.js +++ b/test.js @@ -200,3 +200,52 @@ assert(ImageData.length === 2) assert(img.height === 16) assert(img.data === arr1) } + +{ + const img = new ImageData(1, 1) + + const keys = Object.keys(img) + assert(keys.length === 1) + assert(keys[0] === 'data') + + const ownPropertyNames = Object.getOwnPropertyNames(img) + assert(ownPropertyNames.length === 1) + assert(ownPropertyNames[0] === 'data') + + const enumerableKeys = [] + for (const key in img) enumerableKeys.push(key) + assert(enumerableKeys.length === 3) + assert(enumerableKeys.includes('data')) + assert(enumerableKeys.includes('width')) + assert(enumerableKeys.includes('height')) +} + +// Node.js 10.12.0 and newer +if (typeof process === 'object' && require('util').inspect.custom === Symbol.for('nodejs.util.inspect.custom')) { + const { inspect } = require('util') + const img = new ImageData(1, 1) + + assert(inspect(img, { colors: false, depth: -1 }) === '[ImageData]') + assert(inspect(img, { colors: true, depth: -1 }) === '\u001b[36m[ImageData]\u001b[39m') + + assert(inspect(img, { colors: false, depth: 0 }) === 'ImageData { data: [Uint8ClampedArray], width: 1, height: 1 }') + assert(inspect(img, { colors: true, depth: 0 }) === 'ImageData { data: \u001b[36m[Uint8ClampedArray]\u001b[39m, width: \u001b[33m1\u001b[39m, height: \u001b[33m1\u001b[39m }') + + if (/^v(12|[2-9]\d+)\./.test(process.version)) { + assert(inspect(img, { colors: false, depth: 1 }) === 'ImageData {\n data: Uint8ClampedArray [ 0, 0, 0, 0 ],\n width: 1,\n height: 1\n}') + assert(inspect(img, { colors: true, depth: 1 }) === 'ImageData {\n data: Uint8ClampedArray [ \u001b[33m0\u001b[39m, \u001b[33m0\u001b[39m, \u001b[33m0\u001b[39m, \u001b[33m0\u001b[39m ],\n width: \u001b[33m1\u001b[39m,\n height: \u001b[33m1\u001b[39m\n}') + } else { + assert(inspect(img, { colors: false, depth: 1 }) === 'ImageData { data: Uint8ClampedArray [ 0, 0, 0, 0 ], width: 1, height: 1 }') + assert(inspect(img, { colors: true, depth: 1 }) === 'ImageData { data: Uint8ClampedArray [ \u001b[33m0\u001b[39m, \u001b[33m0\u001b[39m, \u001b[33m0\u001b[39m, \u001b[33m0\u001b[39m ], width: \u001b[33m1\u001b[39m, height: \u001b[33m1\u001b[39m }') + } + + img.test = 'foobar' + + if (/^v(12|[2-9]\d+)\./.test(process.version)) { + assert(inspect(img, { colors: false, depth: 1 }) === 'ImageData {\n data: Uint8ClampedArray [ 0, 0, 0, 0 ],\n width: 1,\n height: 1,\n test: \'foobar\'\n}') + assert(inspect(img, { colors: true, depth: 1 }) === 'ImageData {\n data: Uint8ClampedArray [ \u001b[33m0\u001b[39m, \u001b[33m0\u001b[39m, \u001b[33m0\u001b[39m, \u001b[33m0\u001b[39m ],\n width: \u001b[33m1\u001b[39m,\n height: \u001b[33m1\u001b[39m,\n test: \u001b[32m\'foobar\'\u001b[39m\n}') + } else { + assert(inspect(img, { colors: false, depth: 1 }) === 'ImageData {\n data: Uint8ClampedArray [ 0, 0, 0, 0 ],\n width: 1,\n height: 1,\n test: \'foobar\' }') + assert(inspect(img, { colors: true, depth: 1 }) === 'ImageData {\n data: Uint8ClampedArray [ \u001b[33m0\u001b[39m, \u001b[33m0\u001b[39m, \u001b[33m0\u001b[39m, \u001b[33m0\u001b[39m ],\n width: \u001b[33m1\u001b[39m,\n height: \u001b[33m1\u001b[39m,\n test: \u001b[32m\'foobar\'\u001b[39m }') + } +}