Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
49 changes: 49 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }')
}
}