/** @file utils.js Database utility functions
 */


/** Project a query
 *
 * @a rows are the tuples to operate on
 * @a columns are the attributes to extract
 *
 * @return {Array} of projected tuples
 */
function project (rows, columns) {
	if (columns) {
		const tokens = Array.isArray(columns)
			? columns
			: columns.split(/\s*[,;:\s]\s*/).filter(e => e.length);
		const project = tokens.map(i => i.replace(/^([^.]+)\..*$/, "$1"));
		return rows.map( r =>
			Object.fromEntries(Object.entries(r).filter(entry => project.includes(entry[0])))
		);
	} else {
		return rows;
	}
}

module.exports = {
	project
};
