File size: 1,260 Bytes
21dd449
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import type { RepoDesignation, RepoId } from "../types/public";

export function toRepoId(repo: RepoDesignation): RepoId {
	if (typeof repo !== "string") {
		return repo;
	}

	if (repo.startsWith("model/") || repo.startsWith("models/")) {
		throw new TypeError(
			"A repo designation for a model should not start with 'models/', directly specify the model namespace / name"
		);
	}

	if (repo.startsWith("space/")) {
		throw new TypeError("Spaces should start with 'spaces/', plural, not 'space/'");
	}

	if (repo.startsWith("dataset/")) {
		throw new TypeError("Datasets should start with 'dataset/', plural, not 'dataset/'");
	}

	const slashes = repo.split("/").length - 1;

	if (repo.startsWith("spaces/")) {
		if (slashes !== 2) {
			throw new TypeError("Space Id must include namespace and name of the space");
		}

		return {
			type: "space",
			name: repo.slice("spaces/".length),
		};
	}

	if (repo.startsWith("datasets/")) {
		if (slashes > 2) {
			throw new TypeError("Too many slashes in repo designation: " + repo);
		}

		return {
			type: "dataset",
			name: repo.slice("datasets/".length),
		};
	}

	if (slashes > 1) {
		throw new TypeError("Too many slashes in repo designation: " + repo);
	}

	return {
		type: "model",
		name: repo,
	};
}