
Research
/Security News
Critical Vulnerability in NestJS Devtools: Localhost RCE via Sandbox Escape
A flawed sandbox in @nestjs/devtools-integration lets attackers run code on your machine via CSRF, leading to full Remote Code Execution (RCE).
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:
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'
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, 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 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.
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'
})
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 binomalName = fn.one(animal, {
'cat': 'Felis catus'
'lion': 'Panthera leo'
'dog': '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
.
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 516 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.
Research
/Security News
A flawed sandbox in @nestjs/devtools-integration lets attackers run code on your machine via CSRF, leading to full Remote Code Execution (RCE).
Product
Customize license detection with Socket’s new license overlays: gain control, reduce noise, and handle edge cases with precision.
Product
Socket now supports Rust and Cargo, offering package search for all users and experimental SBOM generation for enterprise projects.