Skip to content

Commit

Permalink
matchStopovers: match next stopover only after previous match
Browse files Browse the repository at this point in the history
  • Loading branch information
derhuerst committed Mar 15, 2023
1 parent 4c64615 commit cf5e2fc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
11 changes: 9 additions & 2 deletions lib/merge-stopovers.js
@@ -1,5 +1,6 @@
'use strict'

const findIndex = require('lodash/findIndex')
// const {plannedDepartureOf, plannedArrivalOf} = require('./helpers')

// const sortChronologically = (stA, stB) => {
Expand All @@ -20,9 +21,15 @@ const createMergeStopovers = (matchStopover, mergeStopover, A, B) => (stopoversA
if (stopoversA.length === 0) return stopoversB

// todo: what about stopovers in B that are not in A?
let prevMatchIdx = -1
return stopoversA.map((stA) => {
const stB = stopoversB.find(matchStopover(stA))
return stB ? mergeStopover(stA, stB) : stA
// start searching after/behind the previous match
const stBIdx = findIndex(stopoversB, matchStopover(stA), prevMatchIdx + 1)
if (stBIdx === -1) return stA
prevMatchIdx = stBIdx

const stB = stopoversB[stBIdx]
return mergeStopover(stA, stB)
})
}

Expand Down
2 changes: 1 addition & 1 deletion license.md
@@ -1,4 +1,4 @@
Copyright (c) 2019, Jannis R
Copyright (c) 2023, Jannis R

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

Expand Down

0 comments on commit cf5e2fc

Please sign in to comment.