Skip to content

Instantly share code, notes, and snippets.

@ZhangYiJiang
Last active December 25, 2017 07:47
Show Gist options
  • Save ZhangYiJiang/69a47a58b06c25615793b38eaec6d9d1 to your computer and use it in GitHub Desktop.
Save ZhangYiJiang/69a47a58b06c25615793b38eaec6d9d1 to your computer and use it in GitHub Desktop.
v2 -> v3 data migration test scripts
// NUSMods v2 timetable data extractor
var loaderTemplate = data => `
var s = document.createElement('script');
s.src = 'https://cdnjs.cloudflare.com/ajax/libs/localforage/1.5.5/localforage.min.js';
s.onload = function() {
const data = ${data};
let count = 0;
data.forEach(([key, queryString]) => {
localforage
.setItem(key, queryString)
.then(() => {
count++;
if (count === data.length) {
localStorage.clear();
window.location.reload();
}
});
});
};
document.body.append(s);`;
var s = document.createElement('script');
s.src = 'https://cdnjs.cloudflare.com/ajax/libs/localforage/1.5.5/localforage.min.js';
s.onload = function() {
const keys = [
'timetable/2017-2018/sem1:queryString',
'timetable/2017-2018/sem2:queryString',
'timetable/2017-2018/sem3:queryString',
'timetable/2017-2018/sem4:queryString',
];
const data = [];
keys.forEach(key => localforage.getItem(key)
.then(queryString => {
data.push([key, queryString]);
if (data.length === keys.length) {
const stringified = JSON.stringify(data, null, 2);
console.log('Data extracted! Go to https://deploy-preview-518--nusmods.netlify.com/ and paste the code below into the console');
console.log(loaderTemplate(stringified));
}
}))
};
document.body.append(s);
@ZhangYiJiang
Copy link
Author

ZhangYiJiang commented Dec 16, 2017

To use:

  1. Run the extractor.js code in the browser console (Ctrl + Shift + J) on https://nusmods.com/
  2. Copy the outputted code from the console
  3. Go to https://deploy-preview-518--nusmods.netlify.com/ and paste the code into the console there. The page will reload after the v2 data have been saved.
  4. Verify that the the timetable has been migrated successfully for all four semesters. This means that
    • The modules match
    • The position of lessons match
    • localStorage should have an extra v2Migration key that indicates the migration has been completed successfully

@yangshun
Copy link

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment