-
-
Notifications
You must be signed in to change notification settings - Fork 34.8k
fs: work with any TypedArrays instead of only Uint8Arrays #22150
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
9851be5
fs: update read to work with any TypedArray or DataView
ececdf8
fs: update write and writeFile to work with TypedArrays and DataViews
8aeab8b
fs: update readSync, writeFile, writeFileSync
d75c103
doc: fix linter issues in fs.md
1b9ae0b
doc: fix yaml indentation for fs.md
4a8cb02
fs: add tests for working with typed arrays
eda78b7
fs: modify existing test case
c9bb952
fs: add tests for working with typed arrays
7084577
fs: modify existing test case
1ca1b02
test: update tests for typed arrays
9087cc9
fs: add tests for working with typed arrays
53472c6
fs: modify existing test case
1b9b245
fs: use isArrayBufferView instead of ArrayBuffer.isView
654944f
test: use console.log instead of util.debuglog
20ab8f6
fs: use isArrayBufferView instead of ArrayBuffer.isView
8f93473
doc: update fs.md with pr-urls
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fs: add tests for working with typed arrays
- Loading branch information
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| 'use strict'; | ||
| const common = require('../common'); | ||
| const assert = require('assert'); | ||
| const fs = require('fs'); | ||
| const join = require('path').join; | ||
| const tmpdir = require('../common/tmpdir'); | ||
| tmpdir.refresh(); | ||
|
|
||
| const filename = join(tmpdir.path, 'test.txt'); | ||
|
|
||
| const s = '南越国是前203年至前111年存在于岭南地区的一个国家,国都位于番禺,疆域包括今天中国的广东、' + | ||
| '广西两省区的大部份地区,福建省、湖南、贵州、云南的一小部份地区和越南的北部。' + | ||
| '南越国是秦朝灭亡后,由南海郡尉赵佗于前203年起兵兼并桂林郡和象郡后建立。' + | ||
| '前196年和前179年,南越国曾先后两次名义上臣属于西汉,成为西汉的“外臣”。前112年,' + | ||
| '南越国末代君主赵建德与西汉发生战争,被汉武帝于前111年所灭。南越国共存在93年,' + | ||
| '历经五代君主。南越国是岭南地区的第一个有记载的政权国家,采用封建制和郡县制并存的制度,' + | ||
| '它的建立保证了秦末乱世岭南地区社会秩序的稳定,有效的改善了岭南地区落后的政治、##济现状。\n'; | ||
|
|
||
| // length must be a multiple of 8? | ||
| const expectStr = s.padEnd(s.length % 8, '-'); | ||
| const expectBuf = Buffer.from(expectStr, 'utf8'); | ||
|
|
||
| const types = [ | ||
| Int8Array, | ||
| Uint8Array, | ||
| Uint8ClampedArray, | ||
| Int16Array, | ||
| Uint16Array, | ||
| Int32Array, | ||
| Uint32Array, | ||
| Float32Array, | ||
| Float64Array, | ||
| DataView | ||
| ]; | ||
|
|
||
| const convertToType = (arrayBufferType, input) => | ||
| arrayBufferType.from(Buffer.from(input)); | ||
| const readInput = (filename) => fs.readFileSync(filename); | ||
|
|
||
|
|
||
| for (const type of types) { | ||
| const expect = convertToType(type, expectBuf); | ||
| fs.writeFileSync(filename, expect); | ||
| assert.strictEqual(convertToType(type, readInput(filename)), expect); | ||
|
|
||
| fs.writeFile(filename, expect, common.mustCall((e) => { | ||
| assert.ifError(e); | ||
|
|
||
| assert.strictEqual(convertToType(type, readInput(filename)), expect); | ||
| })); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@benjamingr this test is currently failing with
What am i doing wrong?