{"size": 1344, "ext": "js", "max_stars_repo_path": "read-shapes.js", "max_stars_repo_name": "derhuerst/gtfs-utils", "max_stars_repo_stars_event_min_datetime": "2018-02-14T20:18:56.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-11T12:52:50.000Z", "max_issues_repo_path": "read-shapes.js", "max_issues_repo_name": "derhuerst/gtfs-utils", "max_issues_repo_issues_event_min_datetime": "2018-07-02T09:39:09.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-28T14:01:39.000Z", "max_forks_repo_path": "read-shapes.js", "max_forks_repo_name": "derhuerst/gtfs-utils", "max_forks_repo_forks_event_min_datetime": "2019-02-24T19:23:48.000Z", "max_forks_repo_forks_event_max_datetime": "2020-08-20T21:08:19.000Z"}
{"max_stars_count": 21, "max_issues_count": 24, "max_forks_count": 3, "avg_line_length": 23.5789473684, "max_line_length": 63, "alphanum_fraction": 0.6696428571}
'use strict'

const expectSorting = require('./lib/expect-sorting')

const readShapes = async function* (readFile, filters = {}) {
	if ('function' !== typeof readFile) {
		throw new Error('readFile must be a function.')
	}

	const {
		shapesRow: shapesRowFilter,
	} = {
		shapesRow: () => true,
		...filters,
	}
	if ('function' !== typeof shapesRowFilter) {
		throw new Error('filters.shapesRow must be a function.')
	}

	const shapes = await readFile('shapes')
	const checkShapesSorting = expectSorting('shapes', (a, b) => {
		if (a.shape_id < b.shape_id) return -1
		if (a.shape_id > b.shape_id) return 1
		const seqA = parseInt(a.shape_pt_sequence)
		const seqB = parseInt(b.shape_pt_sequence)
		if (seqA === seqB) return 0
		return seqA < seqB ? -1 : 1
	})

	let shape_id = NaN, points = []
	for await (const s of shapes) {
		if (!shapesRowFilter(s)) continue
		checkShapesSorting(s)

		if (s.shape_id !== shape_id) {
			if (points.length > 0) {
				yield [shape_id, points]
				points = []
			}
			shape_id = s.shape_id
		}

		points.push({
			shape_pt_lat: parseFloat(s.shape_pt_lat),
			shape_pt_lon: parseFloat(s.shape_pt_lon),
			shape_pt_sequence: parseInt(s.shape_pt_sequence),
			shape_dist_traveled: parseFloat(s.shape_dist_traveled),
		})
	}

	if (points.length > 0) {
		yield [shape_id, points]
	}
}

module.exports = readShapes
