UNPKG

@magnetarjs/core

Version:
23 lines (22 loc) 951 B
import { isCollectionModule } from '@magnetarjs/utils'; /** * Returns a tuple with `[CollectionPath, DocId]` if the `DocId` is `undefined` that means that the `modulePath` passed is a collection! */ export function getCollectionPathDocIdEntry(modulePath) { if (isCollectionModule(modulePath)) return [modulePath, undefined]; const collectionPath = modulePath.split('/').slice(0, -1).join('/'); // prettier-ignore 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); }