Remember a moment of thought change

Optimize the code. Since the strings in the array are referenced by directly writing dead strings in two places in the code, spelling errors will cause potential bugs. Therefore, it is easy to call an array into a json.
 
module.exports = {
    PM: ['/getmyitems', '/getwardrobe', '/additem', '/moveitem', '/exchangeitem', '/splititem', '/mergeitem', '/pickitem', '/dropitem', '/takeon', '/takeoff', '/takeonclothing', '/takeoffclothing', '/deleteitem', '/clearitem', '/find', '/setbulidlev', '/fly', '/chgmap', '/flytonpc', '/setpopulation', '/setfood', '/settime', '/setbuildtimes', '/addmoney', '/submoney', '/setsignature', '/addtitle', '/createprofession', '/changeprofession', '/chgattr', '/buildupgrade', '/buildset', '/builddel', '/buildadd', '/buildput', '/homeupgrade', '/createprofession'],// PM commands are all lowercase

    PM_COMMAND: {
        GETMYITEMS: '/getmyitems',
        GETWARDROBE: '/getwardrobe',
        ADDITEM: '/additem',
        MOVEITEM: '/moveitem'
        // After manually copying and pasting it here, I realized that the array can be converted through code. In the previous work, the conversion was often useful, but it was a business requirement. Like this code optimization, the conversion requirement is not business. Demand, after manually writing a few capital letters, seeing that there are still so many strings to write manually, I naturally thought of using code to solve it. This is a job skill applied to life needs, although the final purpose is still for work. optimization, hereby commemorate
    }
};

  

Below is the conversion process and results:

var PM = ['/getmyitems', '/getwardrobe', '/additem', '/moveitem', '/exchangeitem', '/splititem', '/mergeitem', '/pickitem', '/dropitem', '/takeon', '/takeoff', '/takeonclothing', '/takeoffclothing', '/deleteitem', '/clearitem', '/find', '/setbulidlev', '/fly', '/chgmap', '/flytonpc', '/setpopulation', '/setfood', '/settime', '/setbuildtimes', '/addmoney', '/submoney', '/setsignature', '/addtitle', '/createprofession', '/changeprofession', '/chgattr', '/buildupgrade', '/buildset', '/builddel', '/buildadd', '/buildput', '/homeupgrade', '/createprofession'];
var UPCase = {};
for (var i = 0; i < PM.length; i++) {
    var key = PM[i].toLocaleUpperCase().replace("/", "");
    UPCase[key] = PM[i];
}

console.log(UPCase);

  

{ GETMYITEMS: '/getmyitems',
  GETWARDROBE: '/getwardrobe',
  ADDITEM: '/additem',
  MOVEITEM: '/moveitem',
  EXCHANGEITEM: '/exchangeitem',
  SPLITITEM: '/ splititem',
  MERGEITEM: '/mergeitem',
  PICKITEM: '/pickitem',
  DROPITEM: '/ dropitem',
  TAKEON: '/takeon',
  TAKEOFF: '/takeoff',
  TAKEONCLOTHING: '/takeonclothing',
  TAKEOFFCLOTHING: '/takeoffclothing',
  DELETEITEM: '/deleteitem',
  CLEARITEM: '/clearitem',
  FIND: '/find',
  SETBULIDLEV: '/ setbulidlev',
  FLY: '/ fly',
  CHGMAP: '/chgmap',
  FLYTONPC: '/flytonpc',
  SETPOPULATION: '/setpopulation',
  SETFOOD: '/setfood',
  SETTIME: '/settime',
  SETBUILDTIMES: '/setbuildtimes',
  ADDMONEY: '/addmoney',
  SUBMONEY: '/submoney',
  SETSIGNATURE: '/setsignature',
  ADDTITLE: '/addtitle',
  CREATEPROFESSION: '/createprofession',
  CHANGEPROFESSION: '/changeprofession',
  CHGATTR: '/chgattr',
  BUILDUPGRADE: '/buildupgrade',
  BUILDSET: '/buildset',
  BUILDDEL: '/builddel',
  BUILDADD: '/buildadd',
  BUILDPUT: '/buildput',
  HOMEUPGRADE: '/ homeupgrade'}

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325070260&siteId=291194637