import { isOdd , isEven , countCharacter } from './countHelpers' ; import type { WriteLock } from '../Magnetar' ; export function isDocModule ( path ) { return isOdd ( countCharacter ( path , /\//g ) ) ; } export function isCollectionModule ( path ) { return isEven ( countCharacter ( path , /\//g ) ) ; } type CollectionPath = string ; type DocId = string | undefined ; param { string } modulePath returns { [ CollectionPath , DocId ] } export function getCollectionPathDocIdEntry ( modulePath ) { if ( isCollectionModule ( modulePath ) ) return [ modulePath , undefined ] ; const collectionPath = modulePath . split ( '/' ) . slice ( 0 , - 1 ) . join ( '/' ) ; const docId = modulePath . split ( '/' ) . slice ( - 1 ) [ 0 ] ; return [ collectionPath , docId ] ; } /**
 * Gets all WriteLock objects of a certain `collectionPath` from the `WriteLockMap`
 */ export function getCollectionWriteLocks ( collectionPath , writeLockMap ) { return [ ... writeLockMap . entries ( ) ] . filter ( ( [ modulePath ] ) => { const [ _collectionPath ] = getCollectionPathDocIdEntry ( modulePath ) ; return _collectionPath === collectionPath ; } ) . map ( ( [ modulePath , writeLock ] ) => writeLock ) ; }