I agree that it's unfortunate to duplicate this code.  A simple way to avoid it is to define this function alongside ModuleStore, then we can import it into both places that need it, rather than defining it in both places.