Skip to content

Commit b3331c6

Browse files
authored
feat: stable source uuid with subroot and appendFileCollection (#69)
Refs: #66
1 parent 3d32dec commit b3331c6

3 files changed

Lines changed: 7 additions & 38 deletions

File tree

src/__tests__/file_collection.append_file_collection.test.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -112,22 +112,6 @@ describe('same root', () => {
112112

113113
expect(() => self.appendFileCollection(other)).toThrow(Error);
114114
});
115-
116-
it('should throw error on files without attached source', () => {
117-
const self = new FileCollection();
118-
const other = new FileCollection();
119-
const emptyBlob = new Blob();
120-
other.files.push({
121-
sourceUUID: crypto.randomUUID(),
122-
name: 'test.txt',
123-
relativePath: 'test.txt',
124-
text: emptyBlob.text.bind(emptyBlob),
125-
arrayBuffer: emptyBlob.arrayBuffer.bind(emptyBlob),
126-
stream: emptyBlob.stream.bind(emptyBlob),
127-
});
128-
129-
expect(() => self.appendFileCollection(other)).toThrow(Error);
130-
});
131115
});
132116

133117
describe('at subPath', () => {

src/append/append_file_collection.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { ExtendedSourceItem } from '../ExtendedSourceItem.ts';
21
import { cloneExtendedSourceItem } from '../ExtendedSourceItem.ts';
32
import type { FileCollection } from '../FileCollection.ts';
43
import { cloneFileItem } from '../FileItem.ts';
@@ -10,20 +9,16 @@ export function appendFileCollection(
109
other: FileCollection,
1110
subPath: string,
1211
) {
13-
const sanitizedSubPath = normalizeRelativePath(subPath).replace(/\/$/, '');
14-
const otherSourcesToSelfSources = new Map<
15-
ExtendedSourceItem['uuid'],
16-
ExtendedSourceItem['uuid']
17-
>();
12+
const sanitizedSubPath = normalizeRelativePath(subPath)
13+
// trim ending slash
14+
.replace(/\/$/, '');
1815

1916
for (const otherSource of other.sources) {
2017
const source = cloneExtendedSourceItem(otherSource);
21-
source.uuid = crypto.randomUUID();
22-
source.originalRelativePath = source.relativePath;
18+
source.originalRelativePath ??= source.relativePath;
2319
source.relativePath = normalizeRelativePath(
2420
`${sanitizedSubPath}/${source.relativePath}`,
2521
);
26-
otherSourcesToSelfSources.set(otherSource.uuid, source.uuid);
2722

2823
self.sources.push(source);
2924
}
@@ -38,11 +33,6 @@ export function appendFileCollection(
3833
}
3934
const file = cloneFileItem(otherFile);
4035
file.relativePath = relativePath;
41-
const sourceUUID = otherSourcesToSelfSources.get(file.sourceUUID);
42-
if (!sourceUUID) {
43-
throw new Error('Unreachable: sourceUUID should be defined');
44-
}
45-
file.sourceUUID = sourceUUID;
4636

4737
self.files.push(file);
4838
}

src/subroot.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export function subroot(
1212
const sanitizedSubPath = normalizeRelativePath(subPath).replace(/\/+$/, '');
1313
const pathMatch = `${sanitizedSubPath}/`;
1414
const sources = new Map(top.sources.map((source) => [source.uuid, source]));
15-
const addedSources = new Map<string, string>(); // top source uuid -> sub source uuid
15+
const addedSources = new Set<string>();
1616

1717
for (const topFile of top.files) {
1818
if (!topFile.relativePath.startsWith(pathMatch)) continue;
@@ -22,18 +22,13 @@ export function subroot(
2222
assert(source, 'source');
2323

2424
const subSource = cloneExtendedSourceItem(source);
25-
subSource.uuid = crypto.randomUUID();
26-
subSource.originalRelativePath =
27-
source.originalRelativePath ?? source.relativePath;
25+
subSource.originalRelativePath ??= source.relativePath;
2826
subSource.relativePath = source.relativePath.slice(pathMatch.length);
29-
addedSources.set(topFile.sourceUUID, subSource.uuid);
27+
addedSources.add(subSource.uuid);
3028
sub.sources.push(subSource);
3129
}
3230

3331
const file = cloneFileItem(topFile);
34-
const sourceUUID = addedSources.get(topFile.sourceUUID);
35-
assert(sourceUUID, 'sourceUUID');
36-
file.sourceUUID = sourceUUID;
3732
file.relativePath = file.relativePath.slice(pathMatch.length);
3833
sub.files.push(file);
3934
}

0 commit comments

Comments
 (0)