Conbination condition with less if. 用更少的代码实现组合判断。
npm install lessif --save
是否所有规则都通过。
and<boolean>(true, true, true)(true); // true
interface IPerson {
name: string;
age: number;
sex: 1 | 0;
}
const person: IPerson = {
name: 'tom',
age: 12,
sex: 1
};
const predicate = and<IPerson>(
// true
n => n.name === 'tom',
and(
// true
n => n.name.length === 3,
and(
// true
and(
// true
and(n => n.age === 12)
)
)
),
// true
{
sex: 1
}
);
predicate(person); // true
是否至少有一个规则通过。
or<boolean>(true, false, false)(true); // true
interface IPerson {
name: string;
age: number;
sex: 1 | 0;
}
const person: IPerson = {
name: 'tom',
age: 12,
sex: 1
};
const predicate = or<IPerson>(
// false
n => n.name === 'lily',
// false
or(
n => n.age === 23,
n => n.age > 23
),
// true
{
name: 'tom'
}
);
predicate(person); // true
是否所有规则都不通过。
none<boolean>(false, false, false)(true); // true
interface IPerson {
name: string;
age: number;
sex: 1 | 0;
}
const person: IPerson = {
name: 'tom',
age: 12,
sex: 1
};
const predicate = or<IPerson>(
// false
n => n.name === 'lily',
// false
or(
n => n.age === 23,
n => n.age > 23
),
// true
{
name: 'tom'
}
);
predicate(person); // false
这些规则可以放在一起用。
interface IPerson {
name: string;
age: number;
sex: 1 | 0;
}
const person: IPerson = {
name: 'tom',
age: 12,
sex: 1
};
const predicate = and<IPerson>(
// true
and({ name: 'tom' }),
// true
or(
n => n.age === 23,
n => n.age < 23
),
// true
none({
sex: 0
})
);
predicate(person); // true
MIT