-
Notifications
You must be signed in to change notification settings - Fork 361
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1 parent
24a7893
commit b4b3157
Showing
4 changed files
with
120 additions
and
44 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,67 @@ | ||
/* | ||
Function to convert tests using the async API to use the streamming API. | ||
Please review the conversion to fix any indentation issue or any other | ||
unexpected behaviour. | ||
Example: | ||
const func = ` test('should handle empty object', (t) => { | ||
const opts = { | ||
fields: ['carModel', 'price', 'color'] | ||
}; | ||
const csv = json2csv(jsonFixtures.default, opts); | ||
t.equal(csv, '"carModel","price","color"'); | ||
t.end(); | ||
});` | ||
will output | ||
test('should handle empty object', (t) => { | ||
const opts = { | ||
fields: ['carModel', 'price', 'color'] | ||
}; | ||
const transform = new Json2csvTransform(opts); | ||
const processor = jsonFixturesStreams.default.pipe(transform); | ||
let csv = ''; | ||
processor | ||
.on('data', chunk => (csv += chunk.toString())) | ||
.on('end', () => { | ||
t.equal(csv, '"carModel","price","color"'); | ||
t.end(); | ||
}) | ||
.on('error', err => t.notOk(err)); | ||
}); | ||
*/ | ||
|
||
const func = String.raw`PUT_YOU_FUNCTION_HERE` | ||
|
||
const re = /const csv = json2csv\((.*?)(, opts)?\);\n\n((.|\n)*?)}\);/g | ||
|
||
const processedFunc = func | ||
.replace(re, | ||
`const transform = new Json2csvTransform($2); | ||
const processor = $1.pipe(transform); | ||
let csv = ''; | ||
processor | ||
.on('data', chunk => (csv += chunk.toString())) | ||
.on('end', () => { | ||
$3 | ||
}) | ||
.on('error', err => t.notOk(err)); | ||
});`) | ||
.replace(/const input = ((.|\n)*?);/g, `const input = new Readable(); | ||
input._read = () => {}; | ||
input.push(JSON.stringify($1)); | ||
input.push(null);`) | ||
.replace(/jsonFixtures\./g, 'jsonFixturesStreams.') | ||
.replace(/ t\./g, ' t.') | ||
.replace(/;\n \n }/g, ';\n }') | ||
.replace(/, opts/g, 'opts'); | ||
|
||
console.log(processedFunc); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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