-
Notifications
You must be signed in to change notification settings - Fork 0
/
songs.ts
104 lines (95 loc) · 2.58 KB
/
songs.ts
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { createHash, compareHashes } from "../../functions/hash";
import { getDataFromCompare } from "../../functions/hash";
import { HashSchema } from "../../types/hash.types";
const data = {
id: 1,
name: "Imagine Dragons",
genre: "Rock",
description:
"Imagine Dragons is an American rock band from Las Vegas, Nevada.",
songs: [
{
id: 1,
artist_id: 1,
title: "Radioactive",
album: "Night Visions",
year: 2012,
genre: "Rock",
duration: "3:06",
url: "https://www.youtube.com/watch?v=ktvTqknDobU"
},
{
id: 2,
artist_id: 1,
title: "Demons",
album: "Night Visions",
year: 2013,
genre: "Rock",
duration: "3:11",
url: "https://m.youtube.com/watch?v=m_m_m_m_m_m_m_m_m_m"
}
]
};
const newData = { ...data };
const schema: HashSchema<typeof data> = {
hash: true,
id: "id",
songs: {
hash: true,
id: "id",
parentHash: true
}
};
const dataObjectHash = createHash(data, schema);
console.log(dataObjectHash);
// {
// hash: '3f7f8b0b7787fb28666da87a803878587286c531',
// id: 1,
// songsHash: '74e00c297d092cb744bc3c6f90c3a20840d6be19',
// songs: [
// { hash: '3d5d5e3db08d36a5e00623f94b8624d9e80dd2ab', id: 1 },
// { hash: '91b74e916e3a856bc987f066c465298916ae994c', id: 2 }
// ]
// }
newData.songs[1].url = "https://www.youtube.com/watch?v=mWRsgZuwf_8";
const newDataObjectHash = createHash(newData, schema);
console.log(newDataObjectHash);
// {
// hash: '3f7f8b0b7787fb28666da87a803878587286c531',
// id: 1,
// songsHash: '211c8c3369e0b7e823acce56261692b7828724da',
// songs: [
// { hash: '3d5d5e3db08d36a5e00623f94b8624d9e80dd2ab', id: 1 },
// { hash: 'dfc19ec50d6c07acee917fa931f1ed9edacf28f7', id: 2 }
// ]
// }
const compareResponse = compareHashes(newDataObjectHash, dataObjectHash);
console.log(compareResponse);
// {
// id: 1,
// action: 'none',
// songs: [ { id: 1, action: 'none' }, { action: 'update', id: 2 } ]
// }
const dataResponse = getDataFromCompare(newData, compareResponse, schema);
console.dir(dataResponse, { depth: null });
// {
// id: 1,
// action: 'none',
// songs: [
// { id: 1, action: 'none' },
// {
// action: 'update',
// id: 2,
// data: {
// id: 2,
// artist_id: 1,
// title: 'Demons',
// album: 'Night Visions',
// year: 2013,
// genre: 'Rock',
// duration: '3:11',
// url: 'https://www.youtube.com/watch?v=mWRsgZuwf_8'
// }
// }
// ]
// }