All Projects → Xaber20110202 → Atcon

Xaber20110202 / Atcon

Licence: mit
atom condtions

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to Atcon

Feedkit
An RSS, Atom and JSON Feed parser written in Swift
Stars: ✭ 895 (+3214.81%)
Mutual labels:  atom
Atom Modular Snippets
:atom: A modular solution to snippets in @Atom.
Stars: ✭ 8 (-70.37%)
Mutual labels:  atom
Hfeed2atom
Python functions to convert a h-feed to Atom 1.0
Stars: ✭ 11 (-59.26%)
Mutual labels:  atom
Qolor
An atom package to color your queries!
Stars: ✭ 18 (-33.33%)
Mutual labels:  atom
Atom Korean Spell Daum
Daum 맞춤법 검사기로 텍스트를 보내 손쉽게 교정 가능한 Atom package
Stars: ✭ 24 (-11.11%)
Mutual labels:  atom
Atom Mocha Test Runner
Run your Atom package tests using Mocha
Stars: ✭ 10 (-62.96%)
Mutual labels:  atom
Atom One Dark Terminal
A Terminal theme that mimics the One Dark theme made by the Atom team.
Stars: ✭ 890 (+3196.3%)
Mutual labels:  atom
Github
Git and GitHub integration for Atom
Stars: ✭ 880 (+3159.26%)
Mutual labels:  atom
Atomic Chrome
Edit Chrome textareas in Atom
Stars: ✭ 930 (+3344.44%)
Mutual labels:  atom
Color Indent
Use colors to show in a non intrusive manner different indentation levels.
Stars: ✭ 11 (-59.26%)
Mutual labels:  atom
Language Diff
Stars: ✭ 22 (-18.52%)
Mutual labels:  atom
Autocomplete Haskell
Atom autocomplete-plus provider for haskell
Stars: ✭ 23 (-14.81%)
Mutual labels:  atom
Seti Ui
A subtle dark colored UI theme for Atom.
Stars: ✭ 856 (+3070.37%)
Mutual labels:  atom
Miniflux Legacy
Minimalist RSS reader (version 1.x)
Stars: ✭ 897 (+3222.22%)
Mutual labels:  atom
Language Dot
Dot (Graphviz) package for Atom
Stars: ✭ 11 (-59.26%)
Mutual labels:  atom
Vuejs Snippets
Collection of Vuejs 2.0+ snippets
Stars: ✭ 17 (-37.04%)
Mutual labels:  atom
Atom Check Updates
A CLI for easily updating Atom to the latest version on RPM or Debian-based systems.
Stars: ✭ 8 (-70.37%)
Mutual labels:  atom
Atom Annotations
Atom package that shows annotations (e.g. for overriden methods interface implementations) in your PHP source code.
Stars: ✭ 14 (-48.15%)
Mutual labels:  atom
Atom Svgo
Minify SVG with SVGO.
Stars: ✭ 12 (-55.56%)
Mutual labels:  atom
Atom Sloc
Atom SLOC package for the statusbar
Stars: ✭ 10 (-62.96%)
Mutual labels:  atom

atcon

atcon是什么

  1. con即condition条件,atcon,指为条件而生;
  2. at也来源atom,代表它是一个原子操作;
  3. 把复杂的if else逻辑转变为简单的原子操作,就在atcon。

atcon的目标

和复杂的 if else 说再见

为什么

具体见使用atcon告别混乱的if else

使用方式

npm install --save atcon
const atcon = require('atcon');
atcon(conditions, states, predicate);

执行逻辑

  1. 根据states数组项元素,依次查找condtions对象(也可以是数组)的state0属性,得到conditons1对象,再查找conditons1state1属性...... 其实相当于一个reduce
  2. predicate接收reduce传进来的每一项的conditon[state],如果满足条件,predicate函数 return true 就退出查找,得到该值
  3. 如果conditon[state]不存在,则重新回到上层查找,层层回溯,并获取该层对象的__DEFAULT__属性,传递给predicate,同样的,如果return true,退出查找,得到该值。其实相当于 switch内的default

具体例子

const imgMap = {
    online: {
        '2': {
            a: 'img_b',
            b: 'img_o'
        },
        '3': {
            a: 'img_b',
            b: 'img_p'
        },
        '4': 'img_c',
        '5': 'img_d',
        '6': 'img_e'
    },
    offline: {
        '2': 'img_h',
        '3': 'img_i',
        '4': 'img_j',
        '5': 'img_k',
        '6': 'img_l'
    },
    __DEFAULT__: 'img_a'
};

const noticeMap = {
    b: {
        '3': 'text3',
        '5': 'text5'
    },
    a: 'textaaa',
    __DEFAULT__: 'textdefault'
};

const isString = obj => Object.prototype.toString.call(obj) === '[object String]';

atcon(imgMap, ['online', 3, 'a'], isString); // 'img_b'
atcon(imgMap, ['online', 3, 'c'], isString); // 'img_a'
atcon(imgMap, ['offline', 3, 'v'], isString); // 'img_i'
atcon(imgMap, ['noline'], isString); // 'img_a'

atcon(noticeMap, ['b', 1], isString); // 'textdefault'
atcon(noticeMap, ['a', 6, 1, 5, 6], isString); // 'textaaa'

注意

atcon(noticeMap, ['b'], isString); // undefined

返回的是 undefined,因为走进了 switch case b的逻辑,但是switch case b是一个对象,没有满足isString的条件,而这里没有指定下一层状态的话,循环就会在这一层戛然而止,而不再做回溯。

更多例子可直接参考mocha test

最后

希望大家用得开心。

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].