Post by Shorty on Jul 18, 2013 8:50:14 GMT -8
Alright I'm updating my karma plugin and right now the key is just a number, but in the update it'll be JSON. So I'm having to convert the old key to JSON and I have this code that runs like 5 different times and it takes up a lot of space. So I was wondering if there was a way that I could put the code to convert the number to JSON in a function and be able to reference it outside of the function. I tried something but it didn't work.Is that possible because I really don't like having this repeat that many times and I couldn't thing of any other way to convert it?
Here's the code.
Here's the code.
var karma0 = karmaKey.get(userid);
if (karma0 == undefined) {
karma0 = {
'n': 0,
't': '',
'g': [{
'id': '',
'dn': '',
'ts': '',
'type': ''
}, {
'id': '',
'dn': '',
'ts': '',
'type': ''
}, {
'id': '',
'dn': '',
'ts': '',
'type': ''
}, {
'id': '',
'dn': '',
'ts': '',
'type': ''
}, {
'id': '',
'dn': '',
'ts': '',
'type': ''
}],
'r': [{
'id': '',
'dn': '',
'ts': '',
'type': ''
}, {
'id': '',
'dn': '',
'ts': '',
'type': ''
}, {
'id': '',
'dn': '',
'ts': '',
'type': ''
}, {
'id': '',
'dn': '',
'ts': '',
'type': ''
}, {
'id': '',
'dn': '',
'ts': '',
'type': ''
}]
};
} else if (karma0.match(/{/)) {
karma0 = JSON.parse(karma0);
} else {
karma0 = {
'n': karma0,
't': '',
'g': [{
'id': '',
'dn': '',
'ts': '',
'type': ''
}, {
'id': '',
'dn': '',
'ts': '',
'type': ''
}, {
'id': '',
'dn': '',
'ts': '',
'type': ''
}, {
'id': '',
'dn': '',
'ts': '',
'type': ''
}, {
'id': '',
'dn': '',
'ts': '',
'type': ''
}],
'r': [{
'id': '',
'dn': '',
'ts': '',
'type': ''
}, {
'id': '',
'dn': '',
'ts': '',
'type': ''
}, {
'id': '',
'dn': '',
'ts': '',
'type': ''
}, {
'id': '',
'dn': '',
'ts': '',
'type': ''
}, {
'id': '',
'dn': '',
'ts': '',
'type': ''
}]
};
}