Skip to content

Commit

Permalink
Layout.setClassLayout: remove enumerability from synthesized methods
Browse files Browse the repository at this point in the history
The encode method on instances should not be enumerable, and neither
should the decode method on the constructor.
  • Loading branch information
pabigot committed Mar 4, 2016
1 parent 1729248 commit 72f0dd5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## [Unreleased]

* **API** Make the `encode` and `decode` functions from
[setClassLayout][doc:setClassLayout] un-enumerable.

## [0.11.0] - 2016-02-10

* **API** Provide prototypes to use for decoded objects, resolving
Expand Down Expand Up @@ -118,6 +121,7 @@
[doc:Union]: http://pabigot.github.io/buffer-layout/module-Layout-Union.html
[doc:Union.getSourceVariant]: http://pabigot.github.io/buffer-layout/module-Layout-Union.html#getSourceVariant
[doc:UnionDiscriminator]: http://pabigot.github.io/buffer-layout/module-Layout-UnionDiscriminator.html
[doc:setClassLayout]: http://pabigot.github.io/buffer-layout/module-Layout.html#.setClassLayout
[doc:Sequence]: http://pabigot.github.io/buffer-layout/module-Layout-Sequence.html
[doc:Sequence.count]: http://pabigot.github.io/buffer-layout/module-Layout-Sequence.html#count
[doc:Structure]: http://pabigot.github.io/buffer-layout/module-Layout-Structure.html
Expand Down
22 changes: 16 additions & 6 deletions lib/Layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,12 +387,22 @@ Layout.prototype.fromArray = function(values) {
*/
exports.setClassLayout = function(clazz, layout) {
clazz._layout = layout;
clazz.prototype.encode = function(b, offset) {
return layout.encode(this, b, offset);
};
clazz.decode = function(b, offset) {
return layout.decode(b, offset);
};
Object.defineProperty(clazz.prototype, 'encode', {
configurable: false,
enumerable: false,
value: function(b, offset) {
return layout.encode(this, b, offset);
},
writable: false
});
Object.defineProperty(clazz, 'decode', {
configurable: false,
enumerable: false,
value: function(b, offset) {
return layout.decode(b, offset);
},
writable: false
});
};

/**
Expand Down
3 changes: 3 additions & 0 deletions test/LayoutTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -1723,6 +1723,9 @@ suite('Layout', function() {
'sample',
Sample.prototype));

assert(!Sample.prototype.propertyIsEnumerable('encode'));
assert(!Sample.propertyIsEnumerable('decode'));

var p = new Sample(223, 672);
assert(p instanceof Sample);
assert.equal(p.temp_dCel, 223);
Expand Down

0 comments on commit 72f0dd5

Please sign in to comment.