Synchronous CSV generation for simple use cases where all data fits in memory and immediate results are needed.
Synchronously generates CSV data and returns the complete result immediately.
/**
* Synchronously generates CSV data with specified options
* @param options - Generation options or number of records to generate
* @returns Generated CSV string (default) or array of objects (if objectMode: true)
*/
function generate<T = any>(options: number | Options): string | Array<T>;Important: The sync API requires the length option to be specified (either as a number parameter or within options object).
Usage Examples:
import { generate } from "csv-generate/sync";
// Generate CSV string with record count
const csvString = generate(5);
console.log(csvString);
// Output: CSV string with 5 records
// Generate with detailed options
const csvData = generate({
length: 3,
columns: 2,
seed: 1,
delimiter: ';'
});
console.log(csvData);
// Output: "OMH;ONKCHhJmjadoA\nD;GeACHiN\nfpeKNmK;ertrjyoM"
// Generate objects instead of CSV string
const records = generate({
length: 3,
columns: ['ascii', 'int'],
objectMode: true,
seed: 1
});
console.log(records);
// Output: [["OMH", 4957], ["D", 1085], ["fpeKNmK", 9644]]
// Custom column functions
const customData = generate({
length: 2,
objectMode: true,
columns: [
() => 'fixed-value',
({ state }) => `record-${state.count_created}`
]
});
console.log(customData);
// Output: [["fixed-value", "record-0"], ["fixed-value", "record-1"]]The sync API performs strict validation on input parameters.
// Valid inputs
generate(10); // Number of records
generate({ length: 10 }); // Options object with length
generate({ length: 5, columns: 3 }); // Full options
// Invalid inputs that throw errors
generate(); // Error: length not defined
generate({}); // Error: length not defined
generate({ columns: 3 }); // Error: length not defined
generate(null); // Error: options must be object or integer
generate("invalid"); // Error: options must be object or integerError Handling:
try {
const result = generate({ columns: 3 }); // Missing length
} catch (error) {
console.error(error.message); // "Invalid Argument: length is not defined"
}
try {
const result = generate("not-a-number");
} catch (error) {
console.error(error.message); // "Invalid Argument: options must be an object or an integer"
}The sync API returns different types based on the objectMode option:
// String mode (default)
const csvString: string = generate({
length: 3,
objectMode: false // or omitted
});
// Object mode
const records: Array<any> = generate({
length: 3,
objectMode: true
});Usage Examples:
// String output (default)
const csvString = generate({
length: 2,
columns: 2
});
console.log(typeof csvString); // "string"
console.log(csvString); // "OMH,ONKCHhJmjadoA\nD,GeACHiN"
// Object output
const objectArray = generate({
length: 2,
columns: 2,
objectMode: true
});
console.log(Array.isArray(objectArray)); // true
console.log(objectArray); // [["OMH", "ONKCHhJmjadoA"], ["D", "GeACHiN"]]