From 60d27ccbbfff19def8c58f5a2cc0c5f3f9c54bce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20Unneb=C3=A4ck?= Date: Mon, 16 Dec 2019 18:05:18 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=8C=B9=20Add=20test=20for=20enumerabl?= =?UTF-8?q?e=20properties?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test.js b/test.js index 2b4b44c..231dd6b 100644 --- a/test.js +++ b/test.js @@ -200,3 +200,22 @@ 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')) +} From d23d2d2d7d5ad5a335ebe5f3eaa1c8d2b5b809ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20Unneb=C3=A4ck?= Date: Mon, 16 Dec 2019 18:16:55 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=8E=89=20Add=20custom=20inspect=20fun?= =?UTF-8?q?ction=20for=20Node.js=20>=3D=2010.12.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 12 ++++++++++++ test.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) 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 231dd6b..b379d54 100644 --- a/test.js +++ b/test.js @@ -219,3 +219,33 @@ assert(ImageData.length === 2) 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 }') + } +}