@typed/content-hash
Version:
Content hash a directory of HTML/JS/CSS files and other static assets
37 lines (30 loc) • 919 B
text/typescript
import { Eq } from 'fp-ts/Eq'
import { pipe } from 'fp-ts/function'
import { getOrElse } from 'fp-ts/Option'
import * as RA from 'fp-ts/ReadonlyArray'
import * as RM from 'fp-ts/ReadonlyMap'
import { DiGraph, Edge } from './DiGraph'
export type DependencyMap<A> = ReadonlyMap<A, ReadonlyArray<A>>
export function toDependencyMap<A>(graph: DiGraph<A>, eq: Eq<A> = graph): DependencyMap<A> {
return pipe(graph.edges, RA.reduce(new Map<A, ReadonlyArray<A>>(), applyEdge(eq)))
}
function applyEdge<A>(eq: Eq<A>) {
const lookup = RM.lookup(eq)
const upsertAt = RM.upsertAt(eq)
return (map: DependencyMap<A>, edge: Edge<A>): DependencyMap<A> => {
const [from, to] = edge
return pipe(
map,
upsertAt(
from,
pipe(
map,
lookup(from),
getOrElse((): readonly A[] => []),
RA.append(to),
RA.uniq(eq),
),
),
)
}
}