Skip to content

Commit 4f66e87

Browse files
committed
Add models from metadata-service
1 parent 907477a commit 4f66e87

6 files changed

Lines changed: 302 additions & 0 deletions

File tree

src/models/file.ts

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import { Memoize } from 'typescript-memoize';
2+
import {
3+
Byte,
4+
ByteParser,
5+
Duration,
6+
DurationParser,
7+
NumberParser,
8+
} from '@internetarchive/field-parsers';
9+
10+
/**
11+
* This represents an Internet Archive File
12+
*
13+
* @export
14+
* @class File
15+
*/
16+
export class File {
17+
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
18+
readonly rawValue: Readonly<Record<string, any>>;
19+
20+
get name(): string {
21+
return this.rawValue.name;
22+
}
23+
24+
get source(): string {
25+
return this.rawValue.source;
26+
}
27+
28+
get btih(): string {
29+
return this.rawValue.btih;
30+
}
31+
32+
get md5(): string {
33+
return this.rawValue.md5;
34+
}
35+
36+
get format(): string {
37+
return this.rawValue.format;
38+
}
39+
40+
@Memoize() get mtime(): Date | undefined {
41+
if (this.rawValue.mtime == null) {
42+
return undefined;
43+
}
44+
const numberValue = NumberParser.shared.parseValue(this.rawValue.mtime);
45+
if (numberValue) {
46+
return new Date(numberValue * 1000);
47+
}
48+
}
49+
50+
get crc32(): string {
51+
return this.rawValue.crc32;
52+
}
53+
54+
get sha1(): string {
55+
return this.rawValue.sha1;
56+
}
57+
58+
get original(): string | undefined {
59+
return this.rawValue.original;
60+
}
61+
62+
@Memoize() get size(): Byte | undefined {
63+
return this.rawValue.size != null
64+
? ByteParser.shared.parseValue(this.rawValue.size)
65+
: undefined;
66+
}
67+
68+
get title(): string | undefined {
69+
return this.rawValue.title;
70+
}
71+
72+
@Memoize() get length(): Duration | undefined {
73+
return this.rawValue.length != null
74+
? DurationParser.shared.parseValue(this.rawValue.length)
75+
: undefined;
76+
}
77+
78+
@Memoize() get height(): number | undefined {
79+
return this.rawValue.height != null
80+
? NumberParser.shared.parseValue(this.rawValue.height)
81+
: undefined;
82+
}
83+
84+
@Memoize() get width(): number | undefined {
85+
return this.rawValue.width != null
86+
? NumberParser.shared.parseValue(this.rawValue.width)
87+
: undefined;
88+
}
89+
90+
@Memoize() get track(): number | undefined {
91+
return this.rawValue.track != null
92+
? NumberParser.shared.parseValue(this.rawValue.track)
93+
: undefined;
94+
}
95+
96+
get external_identifier(): string | string[] | undefined {
97+
return this.rawValue.external_identifier;
98+
}
99+
100+
get creator(): string | undefined {
101+
return this.rawValue.creator;
102+
}
103+
104+
get album(): string | undefined {
105+
return this.rawValue.album;
106+
}
107+
108+
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
109+
constructor(json: Record<string, any> = {}) {
110+
this.rawValue = json;
111+
}
112+
}

src/models/review.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Memoize } from 'typescript-memoize';
2+
import { DateParser, NumberParser } from '@internetarchive/field-parsers';
3+
4+
export class Review {
5+
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
6+
readonly rawValue: Readonly<Record<string, any>>;
7+
8+
get reviewbody(): string | undefined {
9+
return this.rawValue.reviewbody;
10+
}
11+
12+
get reviewtitle(): string | undefined {
13+
return this.rawValue.reviewtitle;
14+
}
15+
16+
get reviewer(): string | undefined {
17+
return this.rawValue.reviewer;
18+
}
19+
20+
get reviewer_itemname(): string | undefined {
21+
return this.rawValue.reviewer_itemname;
22+
}
23+
24+
@Memoize() get reviewdate(): Date | undefined {
25+
return this.rawValue.reviewdate != null
26+
? DateParser.shared.parseValue(this.rawValue.reviewdate)
27+
: undefined;
28+
}
29+
30+
@Memoize() get createdate(): Date | undefined {
31+
return this.rawValue.createdate != null
32+
? DateParser.shared.parseValue(this.rawValue.createdate)
33+
: undefined;
34+
}
35+
36+
@Memoize() get stars(): number | undefined {
37+
return this.rawValue.stars != null
38+
? NumberParser.shared.parseValue(this.rawValue.stars)
39+
: undefined;
40+
}
41+
42+
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
43+
constructor(json: Record<string, any> = {}) {
44+
this.rawValue = json;
45+
}
46+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* This is the format used for radio transcripts
3+
*/
4+
export type SpeechMusicASREntry = {
5+
readonly end: number;
6+
readonly id: number;
7+
readonly is_music: boolean;
8+
readonly start: number;
9+
readonly text: string;
10+
};

src/models/task.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export type TaskColor = 'red' | 'green' | 'blue' | 'purple' | 'brown';
2+
3+
export type TaskStatus = 'error' | 'queued' | 'running' | 'passed' | 'paused';
4+
5+
export type Task = {
6+
task_id: number;
7+
cmd: string;
8+
priority: number;
9+
wait_admin: number;
10+
args: Record<string, string>;
11+
color: TaskColor;
12+
status: TaskStatus;
13+
};

test/models/file.test.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { expect } from '@open-wc/testing';
2+
3+
import { File } from '../../src/models/file';
4+
5+
describe('File', () => {
6+
it('can be instantiated with an object', async () => {
7+
const file = new File({ name: 'foo.jpg' });
8+
expect(file.name).to.equal('foo.jpg');
9+
});
10+
11+
// some fields like size and length get converted to their proper types
12+
it('properly instantiates modeled fields', async () => {
13+
const file = new File({
14+
name: 'foo.jpg',
15+
size: '1234',
16+
length: '1:23',
17+
height: '1080',
18+
width: '1920',
19+
track: '1',
20+
});
21+
expect(file.size).to.equal(1234);
22+
expect(file.length).to.equal(83);
23+
expect(file.height).to.equal(1080);
24+
expect(file.width).to.equal(1920);
25+
expect(file.track).to.equal(1);
26+
});
27+
28+
it('external_identifier can be a single value', async () => {
29+
const file = new File({ name: 'foo.jpg', external_identifier: 'bar' });
30+
expect(file.external_identifier).to.equal('bar');
31+
});
32+
33+
it('external_identifier can be an array', async () => {
34+
const file = new File({
35+
name: 'foo.jpg',
36+
external_identifier: ['foo', 'bar'],
37+
});
38+
expect(file.external_identifier).to.deep.equal(['foo', 'bar']);
39+
});
40+
41+
it('handles falsy values properly', async () => {
42+
const file = new File({
43+
name: 'foo.jpg',
44+
size: 0,
45+
track: 0,
46+
});
47+
expect(file.size).to.not.be.undefined;
48+
expect(file.size).to.equal(0);
49+
expect(file.track).to.not.be.undefined;
50+
expect(file.track).to.equal(0);
51+
});
52+
53+
it('parses mtime properly', async () => {
54+
const file = new File({
55+
name: 'foo.jpg',
56+
mtime: '1639591034',
57+
});
58+
expect(file.mtime).to.not.be.undefined;
59+
expect(file.mtime instanceof Date).to.be.true;
60+
expect(file.mtime?.getTime()).to.equal(1639591034000);
61+
});
62+
});

test/models/review.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { expect } from '@open-wc/testing';
2+
3+
import { Review } from '../../src/models/review';
4+
5+
describe('Review', () => {
6+
it('can be instantiated with a title', async () => {
7+
const review = new Review({
8+
reviewtitle: 'It was awesome!',
9+
});
10+
expect(review.reviewtitle).to.equal('It was awesome!');
11+
});
12+
13+
it('stars get converted to a number', async () => {
14+
const review = new Review({
15+
stars: '5',
16+
});
17+
expect(review.stars).to.equal(5);
18+
});
19+
20+
it('reviewdate get converted to a date', async () => {
21+
const review = new Review({
22+
reviewdate: '2014-05-09 09:47:15',
23+
});
24+
25+
const expected = new Date();
26+
expected.setHours(9);
27+
expected.setMinutes(47);
28+
expected.setSeconds(15);
29+
expected.setMilliseconds(0);
30+
expected.setMonth(4);
31+
expected.setDate(9);
32+
expected.setFullYear(2014);
33+
34+
expect(review.reviewdate?.getTime()).to.equal(expected.getTime());
35+
});
36+
37+
it('handles falsy values properly', async () => {
38+
const review = new Review({
39+
reviewtitle: 'yay',
40+
reviewdate: '0',
41+
stars: 0,
42+
});
43+
44+
const expected = new Date();
45+
expected.setHours(0);
46+
expected.setMinutes(0);
47+
expected.setSeconds(0);
48+
expected.setMilliseconds(0);
49+
expected.setMonth(0);
50+
expected.setDate(1);
51+
expected.setFullYear(2000);
52+
53+
expect(review.reviewtitle).to.equal('yay');
54+
expect(review.reviewdate).to.not.be.undefined;
55+
expect(review.reviewdate?.getTime()).to.equal(expected.getTime());
56+
expect(review.stars).to.not.be.undefined;
57+
expect(review.stars).to.equal(0);
58+
});
59+
});

0 commit comments

Comments
 (0)