直接上代码吧 都在代码里写清楚了
问题就是
如何把函数中 tabs.filter.forEach 这些几乎相同的逻辑 独立出来 之前学习了策略模式 但是想不到好的方案 请大佬指点一二
let tabs = [{index:0,name:'angelaBaby'},{index:1,'陈冠西'},{index:2,'李bingbing'},{index:3,'范特西'}]
let COMMAND_TYPE_LIST = {
OTHER:'other',
RIGHT:'right',
SELF:'self',
}
/**
* @desc 抽中的成员逻辑
* @param {*} type:string - 抽中的类型
* @param {*} randomIndex:number - 抽中的数组 index
* @example
* 比如 type==='other' randomIndex=1 那么抽中的成员就是 0,2,3
* 比如 type==='right' randomIndex=1 那么抽中的成员就是 2,3
* 比如 type==='self' randomIndex=1 那么抽中的成员就是 1
* @todo
* 如何把 tabs.filter.forEach 这些几乎相同的逻辑 独立出来 之前学习了策略模式 但是想不到好的方案 请大佬指点一二
*/
function checkedPerson(type,randomIndex){
if (type === COMMAND_TYPE_LIST.OTHER) {
//抽中其他
tabs
.filter((item, index) => index !== randomIndex)
.forEach((tab) => {
//循环处理逻辑
})
} else if (type === COMMAND_TYPE_LIST.RIGHT) {
//抽中右侧
tabs
.filter((item, index) => index > randomIndex)
.forEach((tab) => {
//循环处理逻辑
})
} else if (type === COMMAND_TYPE_LIST.SELF) {
//抽中自身
tabs
.filter((item, index) => index === randomIndex)
.forEach((tab) => {
//循环处理逻辑
})
}
}
checkedPerson(COMMAND_TYPE_LIST.OTHER,2)//抽中的成员就是 0,1,3
###解耦最终操作放在外面,可用性更高点
function checkedPerson1(type,randomIndex) {
const other = (randomIndex, index) => index !== randomIndex;
const right = (randomIndex, index) => index > randomIndex;
const self = (randomIndex, index) => index === randomIndex;
const dict = {
other,
right,
self
}
return tabs.filter((item, index) => dict[type](randomIndex, index));
}
checkedPerson1('other', 1).forEach(item => {
console.log(item);
});
###forEach
里面的回调函数又可以抽对应的处理函数
let COMMAND_TYPE_LIST = {
'other': randomIndex => {
tabs.filter((item, index) => index !== randomIndex).forEach()
},
'right': randomIndex => {
tabs.filter((item, index) => index > randomIndex).forEach()
},
'self': randomIndex => {
tabs.filter((item, index) => index === randomIndex).forEach()
},
}
COMMAND_TYPE_LIST[type](3)
### function filterData(type, randomIndex, cb) {
let conditions = () => {
};
switch (type) {
case "other":
conditions = index => index !== randomIndex;
break;
case "right":
conditions = index => index > randomIndex;
break;
default:
conditions = index => index === randomIndex;
}
// 筛选数据
tabs.filter((item, index) => conditions)
.forEach(item => {
do something ...
});
}
checkedPerson(COMMAND_TYPE_LIST.OTHER, 2);//抽中的成员就是 0,1,3
###class CheckedPerson {
static COMMAND_TYPE_LIST = {};
static TypedCallbackMap = new Map();
static registerType(type, callback) {
const { TypedCallbackMap } = this;
if (TypedCallbackMap.has(type)) {
throw new ReferenceError(`Type ${type} defination already exists.`);
}
TypedCallbackMap.set(
(this.COMMAND_TYPE_LIST[type] = Symbol(type)),
callback
);
}
constructor(items = []) {
this.items = items;
return this.checkedPerson.bind(this);
}
checkedPerson(type, randomIndex) {
const { TypedCallbackMap } = this.constructor;
if (!TypedCallbackMap.has(type)) {
throw new ReferenceError(`No type definations found.`);
}
const callback = TypedCallbackMap.get(type);
return callback(this.items)(randomIndex);
}
}
CheckedPerson.registerType("OTHER", items => {
return index => items.filter(item => index !== item.index);
});
CheckedPerson.registerType("RIGHT", items => {
return index => items.filter(item => index < item.index);
});
CheckedPerson.registerType("SELF", items => {
return index => items.filter(item => index === item.index);
});
const checkedPerson = new CheckedPerson([
{ index: 0, name: "angelaBaby" },
{ index: 1, name: "陈冠西" },
{ index: 2, name: "李bingbing" },
{ index: 3, name: "范特西" }
]);
console.log(checkedPerson(CheckedPerson.COMMAND_TYPE_LIST.OTHER, 1));
console.log(checkedPerson(CheckedPerson.COMMAND_TYPE_LIST.RIGHT, 1));
console.log(checkedPerson(CheckedPerson.COMMAND_TYPE_LIST.SELF, 1));
// checkedPerson(...).forEach(...)