-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReReFormat.js
80 lines (69 loc) · 1.77 KB
/
ReReFormat.js
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
// import fs
import * as fs from 'fs/promises';
let data = await fs.readFile('./ReReFormatDataset.json')
// import * as data2 from './dataset.json'
// console.log(data.toString())
let data2 = JSON.parse(data.toString())
// data2.forEach( vid => {
// vid[course_id] = vid[playlistId] && delete vid[playlistId];
// console.log(vid);
// });
const renameKey = (obj, oldKey, newKey) => {
if (obj[oldKey]) {
obj[newKey] = obj[oldKey];
delete obj[oldKey];
}
}
// create a key key and property in a object
const createKey = (obj, key, property) => {
if (!obj[key]) {
obj[key] = property;
}
}
// generate a random number
const random = (min, max) => {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// generate a random date
const randomDate = () => {
const year = random(2000, 2018);
const month = random(1, 12);
const day = random(1, 28);
return new Date(year, month - 1, day);
}
// return a random value from an array
const randomFromArray = (arr) => {
return arr[random(0, arr.length - 1)];
}
const arr = ['Basic', 'Intermediate', 'Advanced'];
data2.Data.forEach( vid => {
let Level = vid.Level
// check if the Level is Expert
if (Level === "Expert"){
// vid.Level with random value from arr
vid.Level = randomFromArray(arr)
}
});
/*
data2.Data.forEach( vid => {
let category = vid.Category
let enter = ``
category.forEach( cat => {
// convert cat to string and concat to enter
enter += `${cat}`
// if the cat is not last give appen comma
if (cat !== category[category.length - 1]) {
enter += `,`
}
})
vid.Category = enter
// console.log(vid.Category);
});
*/
data2 = JSON.stringify(data2)
fs.appendFile(`ReReReFormatDataset.json`, data2, (err) => {
if (err) {
console.log(err);
}
}
);