2017-04-18 42 views
1

我有這個文件叫myService.js其中我通過使用加熱或冷卻系統定義季節。他們是不同的尤其是在南半球的國家(如澳大利亞),其中季節是相反的情況:使用來自NodeJS中另一個模塊的屬性

const myService= {}; 

const yearTimes = { 
    JapanDefault: { 
     heat: new YearTime('heat', 'Sep', 15, 'Mar', 1), 
     cool: new YearTime('cool', 'Mar', 14, 'Sep', 14) 
    }, 
    AustraliaDefault: { 
     heat: new YearTime('heat', 'Jul', 1, 'Aug', 31), 
     cool: new YearTime('cool', 'Sep', 1, 'Mar', 1) 
    } 
}; 

myService.gettingAnalysis = function (site, Key) { 
    return Promise.all([ 
     myService.findingHeat(site, Key), 
     myService.findingCool(site, Key) 
    ]).spread(function (heat, cool) { 
     return { heating: heating, cooling: cooling }; 
    }); 
}; 
myService.findingHeat = function (site, Key) { 
    return Promise.resolve(YearTime[Key] && YearTime[Key]['heat'] || defaults['heating']); 
}; 

module.exports = myService; 

在另一個文件中,我必須檢查是否有不同的情況,比如我應該給一個警告,如果有使用在夏天加熱。該規範對北半球工作正常,但對於南半球來說是錯誤的,因爲它發現它在夏季(5月,6月)使用加熱系統,但在該特定情況下,該地區是冬季。 這就是所謂的Breakdown.js第二個文件:

const _ = require('underscore'); 
const util = require('util'); 
const stats = require('simple-statistics'); 

const myService = require('./myService'); 
const SUM = 10; 



... 

check('must not indicate heating in summer', function (report) { 
     const model = report.findSection('Breakdown').model; 
     const heatingSeries = _.findWhere(model.series, { name: 'Space heating' }); 
     if (!heatingSeries || model.series.length === 1) { 
      return; 
     } 

     const totalAccounts = _.size(report.asModel().accountNumbers); 
     // TODO: "summer" varies per cohort 
     const warnings = _.compact(_.map(['May', 'Jun'], function (monthLabel) { 
      const summerIndex = _.indexOf(model.xAxisLabels, monthLabel); 
      const heatingSummerCost = heatingSeries.data[summerIndex]; 
      if (heatingSummerCost > (totalAccounts * SUM)) { 
       return { 
        month: monthLabel, 
        cost: heatingSummerCost, 
        accounts: totalAccounts 
       }; 
      } 
     })); 
     this.should(!warnings.length, util.format('heating in summer recommended')); 
    }) 
... 

我試圖做是爲了使檢測如果不是夏天在這方面與myService.findingHeat(site, key)myService.seasons.AustraliaDefault更換['May', 'Jun'],它是正常有加熱成本。 有沒有人有任何想法如何解決這個問題?

回答

1

您試圖用一個由myService.findingHeatingSeason(report.site, report.cohort)myService.seasons.AustraliaDefault返回的季節對象替換數組['July', 'Aug'],這可能會導致一些不一致。你能指定你季節構造嗎?

+0

是:https://pastebin.com/DaaVAmMv –

+2

根據季節的實施['7月','8月']是本賽季的開始月份和結束月份,不是嗎?所以你將不得不創建一個返回[season.startMonth,season.endMonth]的方法。此外,'findingHeatingSeason'返回Promise.resolve,因此您將得到'myService.findingHeatingSeason(report.site,report.cohort).then(function(data){console.log(data)})'的響應。 – Harish

+1

是的,你是對的。他們是開始的月份和結束月份。在這種情況下,他們之間沒有其他月份,但在一般情況下可能會有。所以我認爲返回一個數組containsig應該是解決方案 –

相關問題