Coder Social home page Coder Social logo

akua's Introduction

Akua

Chainable if!

Akua is a very useful tools! It can solve your nested if-tree, make it become chainable if!

Akua can help you code maintainable if!

Install

npm install akua --save

Use

normal nested if-tree

let normalIf = '';
const normailIfStart = new Date().getTime();
if (conditionA) {
  const tempA = 'tempA';
  normalIf += 'a';
  if (conditionB) {
    normalIf += 'b';
    normalIf += tempA;
    if (conditionC) {
      normalIf += 'c';
      if (conditionD) {
        normalIf += 'd';
        if (conditionE) {
          normalIf += 'e';
        }
      }
    }
  } else {
    normalIf += '!b';
  }

  if (conditionF) {
    normalIf += 'f';
    if (conditionG) {
      normalIf += 'g';
    }
  }
} else {
  normalIf += '!a';
}

akua if

let akuaIf = '';
const ifTree = new akua();
ifTree
.inject(conditionA, 'a', (obj) => {
  obj.tempA = 'tempA';
  akuaIf += 'a';
})
.inject(!conditionA, '!a', () => {
  akuaIf += '!a';
})
.inject(conditionB, 'a->b', (obj) => {
  akuaIf += 'b';
  akuaIf += obj.tempA;
})
.inject(!conditionB, 'a->!b', () => {
  akuaIf += '!b';
})
.inject(conditionC, 'b->c', () => {
  akuaIf += 'c';
})
.inject(conditionD, 'c->d', () => {
  akuaIf += 'd';
})
.inject(conditionE, 'd->e', () => {
  akuaIf += 'e';
})
.inject(conditionF, 'a->f', () => {
  akuaIf += 'f';
})
.inject(conditionG, 'f->g', () => {
  akuaIf += 'g';
})
.parse();

Finally, normalIf === akuaIf, you can git clone this project and npm run test if you doubt!

Unit Test Exampls

Perfomance

npm run test

test: 1000, success: 1000, fail: 0
normal-if cost time: 2ms
akua-if cost time: 7ms
test: 100000, success: 100000, fail: 0
normal-if cost time: 19ms
akua-if cost time: 175ms

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.