我有基于變數年齡的值要執行的函式串列。這些功能對應于給予個體的多個疫苗劑量。
- 如果
childern,則不執行任何功能(即疫苗)。 - 如果
youth然后函式 firstDose 將被執行。 - 如果有
adult兩個函式,firstDose 和 secondDose 將被執行。 - 如果
old那么三個函式,firstDose, secondDose, boosterDose 被執行
目前,我使用 switch 和 case 陳述句手動構建此串列。如果條件滿足,那么我將所需的函式推送到一個函式陣列以供稍后執行,就像舊的一樣:
vaccines.push(() => this.firstDose(ageVerification))
vaccines.push(() => this.secondDose(ageVerification, previousVaccineCertificate))
vaccines.push(() => this.boosterDose(ageVerification, previousVaccineCertificate, medicalDocuments))
我不想將其嵌入代碼中,而是希望基于配置(當前在檔案中,稍后可能會移至資料庫)。檔案中的常量應如下所示:
export const ageVaccineMapping = {
children: null,
youth: [this.firstDose(ageVerification)],
adult:[this.firstDose(ageVerification), this.secondDose(ageVerification, previousVaccineCertificate)],
old:[this.firstDose(ageVerification), this.secondDose(ageVerification, previousVaccineCertificate), this.boosterDose(ageVerification, previousVaccineCertificate, medicalDocuments)],
}
我的問題是,我應該如何創建這樣一個具有不同函式引數的常量檔案,如何在作業 ts 檔案中匯入常量檔案,以及如何在代碼中訪問該函式陣列?
我正在使用 Typescript 和 promise.all 來執行。任何線索都會有所幫助。匿名函式也是可以接受的。
請注意,這只是示例,這類似于我的用例。對于老年人,我想執行所有 3 個函式,而不僅僅是 boosterDose 函式。
另外,我希望常量串列應該有不同的函式引數串列。
uj5u.com熱心網友回復:
假設您在 JSON 中有此映射定義:
{
"groups": {
"children": [],
"youth": ["firstDose"],
"adult": ["firstDose", "secondDose"],
"old": ["firstDose", "secondDose", "boosterDose"]
},
"arguments": {
"firstDose": ["ageVerification"],
"secondDose": ["ageVerification", "previousVaccineCertificate"],
"boosterDose": ["ageVerification", "previousVaccineCertificate", "medicalDocuments"]
}
}
你的人物件看起來像這樣:
const people = [
{group: 'adult', ageVerification: 'A1', previousVaccineCertificate: 'B1', medicalDocuments: null},
{group: 'children', ageVerification: null, previousVaccineCertificate: null, medicalDocuments: null},
{group: 'old', ageVerification: 'A3', previousVaccineCertificate: 'B3', medicalDocuments: 'C3'},
{group: 'youth', ageVerification: 'A4', previousVaccineCertificate: null, medicalDocuments: null},
];
然后,您可以創建驗證服務,將 JSON 中的屬性名稱映射到函式和引數中:
class VerificationService {
mapping;
constructor(mapping) {
this.mapping = mapping;
}
verifyAsync(person) {
const pendingVerifications = this.mapping.groups[person.group].map(funcName => {
const args = this.mapping.arguments[funcName].map(prop => person[prop]);
return this[funcName](...args);
});
return Promise.all(pendingVerifications).then(results => results.every(r => r));
},
firstDose(age) {
console.log('--> verifying firstDose:', [...arguments].join(', '));
return Promise.resolve(true); // simulate async result
},
secondDose(age, previous) {
console.log('--> verifying secondDose:', [...arguments].join(', '));
return Promise.resolve(true); // simulate async result
},
boosterDose(age, previous, medical) {
console.log('--> verifying boosterDose:', [...arguments].join(', '));
return Promise.resolve(true); // simulate async result
}
}
像這樣呼叫:
const mapping = JSON.parse('(the above mapping definition)');
const verificationService = new VerificationService(mapping);
people.forEach(async (person) => {
try {
console.log('verifying person', JSON.stringify(person));
const verified = await verificationService.verifyAsync(person);
console.log('result', verified);
} catch (err) {
console.log('verification error', err);
}
});
node.js 產生這個輸出:
verifying person {"group":"adult","ageVerification":"A1","previousVaccineCertificate":"B1","medicalDocuments":null}
--> verifying firstDose: A1
--> verifying secondDose: A1, B1
verifying person {"group":"children","ageVerification":null,"previousVaccineCertificate":null,"medicalDocuments":null}
verifying person {"group":"old","ageVerification":"A3","previousVaccineCertificate":"B3","medicalDocuments":"C3"}
--> verifying firstDose: A3
--> verifying secondDose: A3, B3
--> verifying boosterDose: A3, B3, C3
verifying person {"group":"youth","ageVerification":"A4","previousVaccineCertificate":null,"medicalDocuments":null}
--> verifying firstDose: A4
result true
result true
result true
result true
return Promise.resolve(true);作業函式中的虛擬物件是異步的,但它沒有延遲。對于實際的異步結果,輸出將隨機排序,但以上僅作為演示。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/327263.html
