├── .babelrc ├── .gitignore ├── LICENSE ├── README.md ├── dist └── index.js ├── package.json └── src └── index.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["es2015-loose", "stage-0", "react"], 3 | "plugins": [ 4 | "transform-runtime", 5 | "add-module-exports" 6 | ] 7 | } 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | build 3 | lib-cov 4 | *.seed 5 | *.log 6 | *.csv 7 | *.dat 8 | *.out 9 | *.pid 10 | *.gz 11 | _book 12 | 13 | pids 14 | logs 15 | results 16 | 17 | npm-debug.log 18 | node_modules 19 | *.sublime* 20 | *.node 21 | coverage 22 | *.orig 23 | .idea 24 | sandbox 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016 Contra 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # jif [![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] 2 | 3 | ## Information 4 | 5 | - Dead simple ternary or if/else replacement. 6 | - All arguments (conditions, ifTrue, and ifFalse) can be either a value or a function which returns a value. 7 | - Super useful for cleaning up JSX, which doesn't allow use of if/else. 8 | 9 | ## Install 10 | 11 | ``` 12 | npm install jif --save 13 | ``` 14 | ## jif(condition[, ifTrue, ifFalse]) 15 | 16 | ### ES6 17 | 18 | ```js 19 | import jif from 'jif' 20 | 21 | const truthy = true 22 | const falsey = false 23 | 24 | // lets get started with some primitives... 25 | jif(truthy, 123, 456) // 123 26 | jif(falsey, 123, 456) // 456 27 | 28 | // and now some functions! 29 | jif(truthy, () => 123) // 123 30 | jif(falsey, () => 123) // undefined 31 | ``` 32 | 33 | ### ES5 34 | 35 | ```js 36 | var jif = require('jif'); 37 | 38 | var truthy = true; 39 | var falsey = false; 40 | 41 | // lets get started with some primitives... 42 | jif(truthy, 123, 456); // 123 43 | jif(falsey, 123, 456); // 456 44 | 45 | // and now some functions! 46 | jif(truthy, function(){ 47 | return 123; 48 | }); // 123 49 | jif(falsey, function(){ 50 | return 123; 51 | }); // undefined 52 | ``` 53 | 54 | [downloads-image]: http://img.shields.io/npm/dm/jif.svg 55 | [npm-url]: https://npmjs.org/package/jif 56 | [npm-image]: http://img.shields.io/npm/v/jif.svg 57 | -------------------------------------------------------------------------------- /dist/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | exports.__esModule = true; 4 | var result = function result(fn) { 5 | return typeof fn === 'function' ? fn() : fn; 6 | }; 7 | 8 | exports.default = function (cond, ifTrue, ifFalse) { 9 | return result(cond) ? result(ifTrue) : result(ifFalse); 10 | }; 11 | 12 | module.exports = exports['default']; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jif", 3 | "version": "1.0.1", 4 | "description": "Dead simple ternary or if/else replacement", 5 | "main": "dist/index.js", 6 | "keywords": [ 7 | "ternary", 8 | "function", 9 | "react", 10 | "jsx", 11 | "if else", 12 | "functional" 13 | ], 14 | "repository": { 15 | "type": "git", 16 | "url": "git+https://github.com/contra/jif.git" 17 | }, 18 | "author": "Contra (http://contra.io)", 19 | "license": "MIT", 20 | "bugs": { 21 | "url": "https://github.com/contra/jif/issues" 22 | }, 23 | "homepage": "https://github.com/contra/jif#readme", 24 | "files": [ 25 | "dist" 26 | ], 27 | "scripts": { 28 | "build": "babel src --out-dir dist", 29 | "clean": "rimraf dist", 30 | "test": "eslint src" 31 | }, 32 | "devDependencies": { 33 | "babel": "^6.3.26", 34 | "babel-cli": "^6.4.0", 35 | "babel-core": "^6.4.0", 36 | "babel-eslint": "^4.1.6", 37 | "babel-loader": "^6.2.1", 38 | "babel-plugin-add-module-exports": "^0.1.2", 39 | "babel-plugin-transform-runtime": "^6.4.3", 40 | "babel-preset-es2015": "^6.3.13", 41 | "babel-preset-es2015-loose": "^7.0.0", 42 | "babel-preset-react": "^6.3.13", 43 | "babel-preset-stage-0": "^6.3.13", 44 | "eslint": "^1.10.3", 45 | "eslint-config-rackt": "^1.1.1", 46 | "eslint-plugin-react": "^3.15.0", 47 | "rimraf": "^2.5.0" 48 | }, 49 | "eslintConfig": { 50 | "parser": "babel-eslint", 51 | "env": { 52 | "browser": true, 53 | "node": true, 54 | "es6": true 55 | }, 56 | "ecmaFeatures": { 57 | "modules": true 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | const result = (fn) => typeof fn === 'function' ? fn() : fn 2 | 3 | export default (cond, ifTrue, ifFalse) => 4 | result(cond) ? result(ifTrue) : result(ifFalse) 5 | --------------------------------------------------------------------------------