Giter Club home page Giter Club logo

lessif's Introduction

lessif

Conbination condition with less if. 用更少的代码实现组合判断。

npm test status license

English | 中文

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

lessif's People

Contributors

shalldie avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.