
Product
Announcing Precomputed Reachability Analysis in Socket
Socket’s precomputed reachability slashes false positives by flagging up to 80% of vulnerabilities as irrelevant, with no setup and instant results.
Functions (fn) that make your javascript code cleaner
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:
// Typescript
import fn from 'fn-code'
// Commonjs
const fn = require('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'
You like functional programming. You find yourself doing shenanigans to achieve your goals. Let's see:
For example:
My const variable binomalName
depends on the animal
variable value.
You try something like:
let binomalName = ''
if(animal === 'cat') binomalName = 'Felis catus'
if(animal === 'dog') binomalName = 'Canis familiaris'
But this is not what you want since binomalName name is not a const.
You try something like:
const binomalName = (animal === 'cat') ? 'Felis catus' : 'Canis familiaris'
This is meets the const criteria. But what if you would have a third species now (animal 'lion' for instance)?
const binomalName = (animal === 'cat') ? 'Felis catus' : ((animal === 'lion') ? 'Panthera leo' : 'Canis familiaris')
Ughhh! This escalates badly. Also ternary operator is only clean if the third operand is default value (not another conditional).
So, you are clever and you make a function:
const binomalName = ((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.
Alternatively, if it feels more familiar you can use fn.switch
as it is an alias for fn.one
,.
You can use fn-code npm package to:
import fn from 'fn-code'
const binomalName = fn.one(animal, {
'cat': 'Felis catus'
'lion': 'Panthera leo'
'dog': 'Canis familiaris'
})
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 binomalName = fn.one(animal, {
'cat': 'Felis catus'
'lion': 'Panthera leo'
'dog': 'Canis familiaris'
}, { default: 'Species not found' })
Run the test suit with yarn test
.
If you want to contribute in any of theses ways:
You can (and should) open an issue or even a pull request!
Thanks for your interest in contributing to this repo!
Luiz Felipe Zarco (felipezarco@hotmail.com)
This code is licensed under the MIT License. See the LICENSE.md file for more info.
FAQs
Functions (fn) to make code cleaner
The npm package fn-code receives a total of 362 weekly downloads. As such, fn-code popularity was classified as not popular.
We found that fn-code demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket’s precomputed reachability slashes false positives by flagging up to 80% of vulnerabilities as irrelevant, with no setup and instant results.
Product
Socket is launching experimental protection for Chrome extensions, scanning for malware and risky permissions to prevent silent supply chain attacks.
Product
Add secure dependency scanning to Claude Desktop with Socket MCP, a one-click extension that keeps your coding conversations safe from malicious packages.