lessif
v0.0.2
Published
Write more conditions with less if.
Downloads
4
Readme
lessif
Write more conditions with less if. 少许代码实现大量判断。
Installation
npm install lessif --save
Usage & Example
and
Whether all provided rules pass.
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
Whether at least one provided rule passes.
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
Whether none of provided rules pass.
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
and&or&none
Put them together!
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
License
MIT