-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: remaining pr search related functions and refactoring
- Loading branch information
1 parent
89fb3aa
commit 601a800
Showing
6 changed files
with
2,411 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
|
||
/** | ||
* Writes json data to a csv file | ||
* @param {Array<Object>} data - The data to be written to the file. | ||
* @param {string} [options.archiveFolder=process.cwd()] - The folder where the file will be saved. Defaults to the current working directory. | ||
* @param {string} [options.archiveFileName='archive-YYYYMMDDHHmmss.csv'] - The name of the file to be written. Defaults to 'archive-YYYYMMDDHHmmss.csv'. | ||
*/ | ||
export function save(data, options = {}) { | ||
if (!data || !Array.isArray(data) || data.length<1) { | ||
console.log("No content to write."); | ||
return; | ||
} | ||
// Prepare content for csv | ||
let allKeys = Array.from(new Set(data.flatMap(Object.keys))); | ||
const headers = allKeys.join(','); | ||
const rows = data.map(obj => formatCSVRow(obj, allKeys)).join("\n"); | ||
const csvContent = headers + "\n" + rows; | ||
writeToFile(csvContent, options); | ||
return csvContent; | ||
} | ||
|
||
export function writeToFile(content, options){ | ||
const ARCHIVE_FOLDER = options.archiveFolder || process.cwd(); | ||
const ARCHIVE_FULL_PATH = path.join(ARCHIVE_FOLDER, options.archiveFileName || `archive-${getFormattedDate()}.csv`); | ||
fs.writeFile(ARCHIVE_FULL_PATH, content, { flag: 'a+' }, err => { | ||
if (err) { | ||
console.error(err); | ||
return; | ||
} | ||
console.log("The file was saved!"); | ||
}); | ||
} | ||
|
||
function formatCSVRow(obj, keys) { | ||
return keys.map(key => formatCSVCell(obj[key])).join(','); | ||
} | ||
|
||
function formatCSVCell(value) { | ||
if (value === undefined || value === null) { | ||
return ''; | ||
} else if (typeof value === 'object') { | ||
// Stringify objects/arrays and escape double quotes | ||
return `"${JSON.stringify(value).replace(/"/g, '""')}"`; | ||
} else if (typeof value === 'string') { | ||
// Check for commas or line breaks and escape double quotes | ||
if (value.includes(',') || value.includes('\n')) { | ||
return `"${value.replace(/"/g, '""')}"`; | ||
} else { | ||
return value; | ||
} | ||
} else { | ||
return value.toString(); | ||
} | ||
} | ||
|
||
export function getFormattedDate() { | ||
const now = new Date(); | ||
return now.toISOString().replace(/[\-\:T\.Z]/g, ''); | ||
} |
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
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,20 @@ | ||
import { expect, assert } from "chai"; | ||
import * as archive from "../archive.js"; | ||
|
||
import * as pullRequestsFixture from './fixtures/pullRequests.fixture.js'; | ||
|
||
describe('archive.js', function() { | ||
|
||
/** Archive test --START-- */ | ||
|
||
describe.skip('#archive(jsonArray);', async function() { | ||
it('should save jsonArray to a csv file', async function() { | ||
this.timeout(100000); | ||
let content = await archive.save(pullRequestsFixture.VALID_PR_SEARCH_RESULT_ITEMS); | ||
assert.isNotNull(content, "Repos not returned"); | ||
expect(content).to.be.an('string'); | ||
expect(content).to.have.lengthOf.greaterThan(2000); | ||
}) | ||
}) | ||
|
||
}) |
Oops, something went wrong.