-
-
Notifications
You must be signed in to change notification settings - Fork 234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement Buffer.copyBytesFrom #348
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,8 @@ | |
var Buffer = require('../../').Buffer; | ||
|
||
const common = require('./common'); | ||
const { deepStrictEqual, throws } = require('assert'); | ||
const assert = require('assert'); | ||
const { deepStrictEqual, throws } = assert; | ||
const { runInNewContext } = require('vm'); | ||
|
||
const checkString = 'test'; | ||
|
@@ -66,3 +67,73 @@ deepStrictEqual( | |
throws(() => Buffer.from(input), errMsg); | ||
}); | ||
|
||
{ | ||
const u16 = new Uint16Array([0xffff]); | ||
const b16 = Buffer.copyBytesFrom(u16); | ||
u16[0] = 0; | ||
assert.strictEqual(b16.length, 2); | ||
assert.strictEqual(b16[0], 255); | ||
assert.strictEqual(b16[1], 255); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How are semi's here? 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (no changes required, we can resolve this in another pull request) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's because I pulled these directly from the node.js tests. Maybe we don't lint the node tests? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If these are directly from the node tests, can you add an please add a reference? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is that not already accounted for? As far as I can tell, all of the tests in edit: Okay, after looking, I guess it isn't accounted for. Should I add a the node.js LICENSE file inside the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for hunting that down @chjj, everything should be MIT, so hopefully any effort here is simply about proper attribution. |
||
} | ||
|
||
{ | ||
const u16 = new Uint16Array([0, 0xffff]); | ||
const b16 = Buffer.copyBytesFrom(u16, 1, 5); | ||
u16[0] = 0xffff; | ||
u16[1] = 0; | ||
assert.strictEqual(b16.length, 2); | ||
assert.strictEqual(b16[0], 255); | ||
assert.strictEqual(b16[1], 255); | ||
} | ||
|
||
{ | ||
const u32 = new Uint32Array([0xffffffff]); | ||
const b32 = Buffer.copyBytesFrom(u32); | ||
u32[0] = 0; | ||
assert.strictEqual(b32.length, 4); | ||
assert.strictEqual(b32[0], 255); | ||
assert.strictEqual(b32[1], 255); | ||
assert.strictEqual(b32[2], 255); | ||
assert.strictEqual(b32[3], 255); | ||
} | ||
|
||
assert.throws(() => { | ||
Buffer.copyBytesFrom(); | ||
}, TypeError); | ||
|
||
{ | ||
const dv = new DataView(new ArrayBuffer(1)); | ||
assert.throws(() => { | ||
Buffer.copyBytesFrom(dv); | ||
}, TypeError); | ||
} | ||
|
||
['', Symbol(), true, false, {}, [], () => {}, 1, 1n, null, undefined].forEach( | ||
notTypedArray => assert.throws(() => { | ||
Buffer.copyBytesFrom(notTypedArray); | ||
}, TypeError) | ||
); | ||
|
||
['', Symbol(), true, false, {}, [], () => {}, 1n].forEach(notANumber => | ||
assert.throws(() => { | ||
Buffer.copyBytesFrom(new Uint8Array(1), notANumber); | ||
}, TypeError) | ||
); | ||
|
||
[-1, NaN, 1.1, -Infinity].forEach(outOfRange => | ||
assert.throws(() => { | ||
Buffer.copyBytesFrom(new Uint8Array(1), outOfRange); | ||
}, RangeError) | ||
); | ||
|
||
['', Symbol(), true, false, {}, [], () => {}, 1n].forEach(notANumber => | ||
assert.throws(() => { | ||
Buffer.copyBytesFrom(new Uint8Array(1), 0, notANumber); | ||
}, TypeError) | ||
); | ||
|
||
[-1, NaN, 1.1, -Infinity].forEach(outOfRange => | ||
assert.throws(() => { | ||
Buffer.copyBytesFrom(new Uint8Array(1), 0, outOfRange); | ||
}, RangeError) | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally, a preference for flat (preferably functional) style, even if the actual number of resulting branches is the same
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code was ported from the node.js code as faithfully as possible to ensure accurate behavior. I can change it though.
While we're on the subject: what is the minimum ES version we support? Since the
??
operator is an ES2020 feature, it would be good to have clarity on that. BigInt is also an ES2020 feature, but we include graceful degradation for the bigint methods.