Split the flag generator JS into tighter units to improve testability