You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

fn-code

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fn-code

Functions (fn) to make code cleaner

0.0.1
Source
npmnpm
Version published
Weekly downloads
363
-23.58%
Maintainers
1
Weekly downloads
 
Created
Source

Fn functions

Functions (fn) that make your javascript code cleaner

License: MIT npm version Build Status Coverage Status Downloads

npm

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:

// 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'

Why Clean Code 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 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,.

Usage

You can use fn-code npm package to:

Set const conditionally (fn.one):

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' })

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 (felipezarco@hotmail.com)

License

This code is licensed under the MIT License. See the LICENSE.md file for more info.

Keywords

fn-code

FAQs

Package last updated on 23 Jul 2022

Did you know?

Socket

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.

Install

Related posts