npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fn-code

v0.1.5

Published

Functions (fn) to make code cleaner

Downloads

611

Readme

Fn functions

Functions (fn) that make your javascript code cleaner

License: MIT npm version Build Status Coverage Status Downloads

npm

Use fn.one to set constants conditionally:

image

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.