Open shfshanyue opened 3 years ago
代码见 codepen,如何实现链式调用
function find (data) {
return {
data,
where (match) {
this.data = this.data.filter((item) => {
return Object.entries(match).every(([key, value]) => {
if (value instanceof RegExp) {
return value.test(item[key])
}
return item[key] === value
})
})
return this
},
orderBy (key, type) {
this.data.sort((x, y) => type !== 'desc' ? x[key] - y[key] : y[key] - x[key])
return this
}
}
}
function find(data) {
class FindManager {
constructor(data) {
this.data = data;
}
where(query) {
const data = [...this.data];
return new FindManager(data.filter((item) => {
return Object.entries(query).every(([key, filter]) => {
if (Object.prototype.toString.call(filter).slice(8, -1) === 'Regex') {
return filter.test(item[key]);
} else {
return filter === item[key];
}
});
}));
}
orderBy(key, order) {
const data = [...this.data];
data.sort((a, b) => {
return order === 'asc' ? a[key] - b[key] : b[key] - a[key];
});
return new FindManager(data);
}
get value() {
return this.data;
}
}
return new FindManager(data);
}
function find (data) {
const temp = Array.isArray(data) ? [...data] : {...data}
let opt = {
where: function where (opt) {
return find(Object.entries(opt).reduce((prev, [key, match]) => {
return prev.filter(curr => {
return match.test(curr[key])
})
}, this))
},
orderBy: function order (key, type) {
return this.sort((prev, next) => {
switch (type) {
case 'desc':
return next - prev
case 'asc':
return prev - next
default:
return prev - next
}
})
}
}
Object.setPrototypeOf(opt, Array.prototype)
Object.setPrototypeOf(temp, opt)
return temp
}
// 综合一下两位大佬的写法
class Find {
constructor(data) {
this.data = [...data];
}
where(query) {
this.data = this.data.filter((item) => {
return Object.entries(query).every(([key, value]) => {
if (value instanceof RegExp) {
return value.test(item[key]);
} else {
return item[key] === value;
}
});
});
return this;
}
orderBy(key, type) {
this.data = this.data.sort((a, b) => (type !== 'desc' ? a[key] - b[key] : b[key] - a[key]));
return this;
}
get value() {
return this.data;
}
}
function find(data) {
return new Find(data);
}
如下代码所示,使用
find
函数实现链式调用