在 node.js 中创建要从常量文件执行的函数列表
Create a list of functions to be executed from constant file in node.js
我有根据变量 age 的值执行的函数列表。这些函数对应于给予个体的疫苗剂量数。
- 如果
childern
,则不执行函数(即疫苗)。
- 如果
youth
则函数firstDose将被执行。
- 如果
adult
那么两个函数firstDose和secondDose就是要
已执行。
- 如果
old
那么要执行三个函数,firstDose, secondDose, boosterDose
目前,我使用 switch 和 case 语句手动构建此列表。如果满足条件,那么我将所需的函数推送到一个函数数组中以便稍后执行,就像 old:
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 功能。
另外,我希望常量列表应该有不同的函数参数列表。
假设您在 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
worker 函数中的虚拟 return Promise.resolve(true);
是异步的,但没有延迟。实际的异步结果,输出的顺序是随机的,不过以上只是演示而已
我有根据变量 age 的值执行的函数列表。这些函数对应于给予个体的疫苗剂量数。
- 如果
childern
,则不执行函数(即疫苗)。 - 如果
youth
则函数firstDose将被执行。 - 如果
adult
那么两个函数firstDose和secondDose就是要 已执行。 - 如果
old
那么要执行三个函数,firstDose, secondDose, boosterDose
目前,我使用 switch 和 case 语句手动构建此列表。如果满足条件,那么我将所需的函数推送到一个函数数组中以便稍后执行,就像 old:
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 功能。
另外,我希望常量列表应该有不同的函数参数列表。
假设您在 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
worker 函数中的虚拟 return Promise.resolve(true);
是异步的,但没有延迟。实际的异步结果,输出的顺序是随机的,不过以上只是演示而已