MCPcopy Create free account
hub / github.com/effect-app/libs / sequenceFromPath

Function sequenceFromPath

repos/effect/packages/effect/test/Pathfinding.test.ts:156–199  ·  view source on GitHub ↗
(
  terrain: Terrain,
  path: Option.Option<Graph.PathResult<number>>
)

Source from the content-addressed store, hash-verified

154}
155
156/**
157 * Derive a sequence of moves from a path.
158 */
159const sequenceFromPath = (
160 terrain: Terrain,
161 path: Option.Option<Graph.PathResult<number>>
162): Sequence | undefined => {
163 if (Option.isNone(path) || path.value.path.length === 0) {
164 return undefined
165 }
166
167 const pathValue = path.value
168
169 const sequence = []
170 const start = terrain.coordinates.get(pathValue.path[0])!
171 let previous = Graph.getNode(terrain.graph, terrain.nodes.get(start)!).pipe(
172 Option.getOrThrowWith(() => new Error(`Start location ${start} not found in terrain`))
173 )
174
175 for (const index of pathValue.path.slice(1)) {
176 const current = Graph.getNode(terrain.graph, index).pipe(
177 Option.getOrThrowWith(() => new Error(`Location ${index} not found in terrain`))
178 )
179
180 if (current.x === previous.x) {
181 if (current.y === previous.y - 1) {
182 sequence.push("↑")
183 } else if (current.y === previous.y + 1) {
184 sequence.push("↓")
185 } else {
186 throw new Error(`Invalid move ${current.x}:${current.y} -> ${previous.x}:${previous.y}`)
187 }
188 } else if (current.y === previous.y) {
189 if (current.x === previous.x - 1) {
190 sequence.push("←")
191 } else if (current.x === previous.x + 1) {
192 sequence.push("→")
193 } else {
194 throw new Error(`Invalid move ${current.x}:${current.y} -> ${previous.x}:${previous.y}`)
195 }
196 }
197
198 previous = current
199 }
200
201 return `${start};${sequence.join("")}`
202}

Callers 1

assertPathFunction · 0.85

Calls 4

pushMethod · 0.80
joinMethod · 0.80
getMethod · 0.65
pipeMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…