GithubHelp home page GithubHelp logo

fe-ab-utils's Introduction

前端灰度

前端开发中会有一些类似ABTest或者灰度相关的需求。在传统的开发中,会通过类似 if-else 的形式去组织代码。但是当灰度版本变多、灰度代码涉及的文件较多时,会使代码变得 丑陋。所以我们希望有一种更 优雅 的方式去处理前端灰度相关需求。

传统 丑陋 的灰度代码

const isMatchGray = Boolean(Math.random() > 0.5)

function App() {

  const title = isMatchGray ? 'title' : 'newTitle'
  const containerStyle = isMatchGray ? { background: '#ccc' } : { background: '#eee' }

  return (
    <div style={containerStyle}>
      <div>{title}</div>
    </div>
  )
}

此时,当有一个新的灰度需求出现,我们需要调整代码:

const isMatchGrayOfA = Boolean(Math.random() > 0.5)
const isMatchGrayOfB = Boolean(Math.random() > 0.5)

function App() {

  const title = isMatchGrayOfA ? 'title' : 'newTitle'
  const containerStyle = isMatchGrayOfB ? { background: '#ccc' } : { background: '#eee' }

  return (
    <div style={containerStyle}>
      <div>{title}</div>
    </div>
  )
}

设计更有 语义化 的灰度api

// 1. 传入字符串
GrayVersion.match('return when matched').not('return when matched')

// 2. 传入函数
GrayVersion
  .match(() => {
    msg: 'return a function will perform when matched'
  })
  .not(() => {
    msg: 'return a function will perform when not matched'
  })

最终的灰度代码

import GL from 'gray-level'

const isMatchGrayOfA = Boolean(Math.random() > 0.5)
const isMatchGrayOfB = Boolean(Math.random() > 0.5)
const GrayOfA = GL.init('A', isMatchGrayOfA)
const GrayOfB = GL.init('B', isMatchGrayOfB)

function App() {

  const title = GrayOfA.match('title').not('newTitle')
  const containerStyle = GrayOfB.match({ background: '#ccc' }).not({ background: '#eee' })

  return (
    <div style={containerStyle}>
      <div>{title}</div>
    </div>
  )
}

fe-ab-utils's People

Contributors

bibidu avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  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.