fn-code
v0.1.5
Published
Functions (fn) to make code cleaner
Downloads
611
Maintainers
Readme
Fn functions
Functions (fn) that make your javascript code cleaner
Use fn.one
to set constants conditionally:
Installation
The latest version is available at: https://www.npmjs.com/package/fn-code
Use your favorite package manager to install. For instance:
yarn add fn-code
Then import it:
import fn from 'fn-code'
Importing as fn
is the way I prefer to use it. But it's a matter of preference, you could either chose another name or deconstruct the functions:
import cleanCode from 'fn-code'
import { one } from 'fn-code'
Why use these functions?
You like functional programming. You find yourself doing shenanigans to achieve your goals. Let's see:
I want to make my variable a const, but depending on different values of another variable
For example:
My const variable binomialName
depends on the animal
variable value.
You try something like:
let binomialName = ''
if(animal === 'cat') binomialName = 'Felis catus'
if(animal === 'dog') binomialName = 'Canis familiaris'
But this is not what you want since binomialName name is not a const.
You try something like:
const binomialName = (animal === 'cat') ? 'Felis catus' : 'Canis familiaris'
This is meets the const criteria. Now what if you would have a third species now (animal 'lion' for instance)?
const binomialName = (animal === 'cat') ? 'Felis catus' : ((animal === 'lion') ? 'Panthera leo' : 'Canis familiaris')
Ughhh! This escalates badly. Also, the ternary operator is only clean when the third operand is the default value (and not another conditional).
So, you are clever and you make a function:
const binomialName = ((animal) => {
switch (animal) {
case 'cat':
return 'Felis catus'
case 'lion':
return 'Panthera leo'
case 'dog':
return 'Canis familiaris'
}
})(animal)
This is better since we have const and switch. But passing those parameters to make the function pure still looks weird.
Usage
You can use fn-code npm package to:
1. Set const conditionally (fn.one/fn.switch)
import fn from 'fn-code'
const binomialName = fn.one(animal, {
'cat': 'Felis catus'
'lion': 'Panthera leo'
'dog': 'Canis familiaris'
})
But what if you want to have a default value for binomialName when no condition is met?
For that, you can use the third optional argument, passing { default: '' }
import fn from 'fn-code'
const binomialName = fn.one(animal, {
'cat': 'Felis catus'
'lion': 'Panthera leo'
'dog': 'Canis familiaris'
}, { default: 'Species not found' })
Hold on. Not everything fits as an object key: you want to pass a regex or a more specific comparison than case == value
..
No problem! You can define the switch as an array of case functions and values!
(These case functions must always return boolean)
const binomialName = fn.one(animal, [
{
case: (str: string) => str.includes('cat'),
value: 'Felis catus'
},
{
case: (str: string) => str.includes('lion'),
value: 'Panthera leo'
},
{
case: (str: string) => str.includes('dog'),
value: 'Canis familiaris'
}
], { default: 'Species not found' })
Alternatively, if it feels more familiar you can use fn.switch
instead, as it is an alias for fn.one
.
2. ...
Other similar functions might be added to the module.
Testing
Run the test suit with yarn test
.
Contributing
If you want to contribute in any of theses ways:
- Give your ideas or feedback
- Question something
- Point out a problem or issue
- Enhance the code or its documentation
- Help in any other way
You can (and should) open an issue or even a pull request!
Thanks for your interest in contributing to this repo!
Author
Luiz Felipe Zarco ([email protected])
License
This code is licensed under the MIT License. See the LICENSE.md file for more info.