Common FP

Functional programming for the commoner

What Is It?#

An opinionated utility library with a few goals

Get Started#

  1. Install
    .sh
    npm i common-fp
    
    # using typescript?
    npm i common-fp-types
  2. And use
    import { mapValues } from 'common-fp'
    
    const giveEveryoneAnApple = mapValues(numApples => numApples + 1)
    
    const applesPerPersonObj = {
      jason: 2,
      amy: 3,
    }
    
    const resultObj = giveEveryoneAnApple(applesPerPersonObj)
    console.log(resultObj)
    // a new object {
    //   jason: 3,
    //   amy: 4,
    // }
    import { mapValues } from 'common-fp'
    
    const giveEveryoneAnApple = mapValues((numApples: number) => numApples + 1)
    
    const applesPerPersonObj = {
      jason: 2,
      amy: 3,
    }
    
    const resultObj = giveEveryoneAnApple(applesPerPersonObj)
    console.log(resultObj)
    // a new object {
    //   jason: 3,
    //   amy: 4,
    // }
    
    import { mapValues } from 'common-fp'
    
    const giveEveryoneAnApple = mapValues(
      numApples => numApples + 1
    )
    
    const applesPerPersonObj = {
      jason: 2,
      amy: 3,
    }
    
    const resultObj = giveEveryoneAnApple(
      applesPerPersonObj
    )
    console.log(resultObj)
    // a new object {
    //   jason: 3,
    //   amy: 4,
    // }
    import { mapValues } from 'common-fp'
    
    const giveEveryoneAnApple = mapValues(
      (numApples: number) => numApples + 1
    )
    
    const applesPerPersonObj = {
      jason: 2,
      amy: 3,
    }
    
    const resultObj = giveEveryoneAnApple(
      applesPerPersonObj
    )
    console.log(resultObj)
    // a new object {
    //   jason: 3,
    //   amy: 4,
    // }
    
    You can also pass a Map
    const applesPerPersonMap = new Map([
      ['jason', 2],
      ['amy', 3],
    ])
    
    const resultMap = giveEveryoneAnApple(applesPerPersonMap)
    console.log(resultMap)
    // a new Map [
    //   ['jason', 3],
    //   ['amy', 4],
    // ]
    const applesPerPersonMap = new Map([
      ['jason', 2],
      ['amy', 3],
    ])
    
    const resultMap = giveEveryoneAnApple(applesPerPersonMap)
    console.log(resultMap)
    // a new Map [
    //   ['jason', 3],
    //   ['amy', 4],
    // ]
    
    const applesPerPersonMap = new Map([
      ['jason', 2],
      ['amy', 3],
    ])
    
    const resultMap = giveEveryoneAnApple(
      applesPerPersonMap
    )
    console.log(resultMap)
    // a new Map [
    //   ['jason', 3],
    //   ['amy', 4],
    // ]
    const applesPerPersonMap = new Map([
      ['jason', 2],
      ['amy', 3],
    ])
    
    const resultMap = giveEveryoneAnApple(
      applesPerPersonMap
    )
    console.log(resultMap)
    // a new Map [
    //   ['jason', 3],
    //   ['amy', 4],
    // ]
    
    mapValues works with arrays and Sets as well
    const capitalize = str => str[0].toUpperCase() + str.slice(1)
    const capitalizeAll = mapValues(capitalize)
    
    const peopleArr = ['kim', 'grace']
    const resultArr = capitalizeAll(peopleArr)
    console.log(resultArr)
    // a new array [
    //   'Kim',
    //   'Grace',
    // ]
    
    const peopleSet = new Set(['kim', 'grace'])
    const resultSet = capitalizeAll(peopleSet)
    console.log(resultSet)
    // a new Set [
    //   'Kim',
    //   'Grace',
    // ]
    const capitalize = (str: string) => str[0].toUpperCase() + str.slice(1)
    const capitalizeAll = mapValues(capitalize)
    
    const peopleArr = ['kim', 'grace']
    const resultArr = capitalizeAll(peopleArr)
    console.log(resultArr)
    // a new array [
    //   'Kim',
    //   'Grace',
    // ]
    
    const peopleSet = new Set(['kim', 'grace'])
    const resultSet = capitalizeAll(peopleSet)
    console.log(resultSet)
    // a new Set [
    //   'Kim',
    //   'Grace',
    // ]
    
    const capitalize = str =>
      str[0].toUpperCase() + str.slice(1)
    const capitalizeAll = mapValues(capitalize)
    
    const peopleArr = ['kim', 'grace']
    const resultArr = capitalizeAll(peopleArr)
    console.log(resultArr)
    // a new array [
    //   'Kim',
    //   'Grace',
    // ]
    
    const peopleSet = new Set(['kim', 'grace'])
    const resultSet = capitalizeAll(peopleSet)
    console.log(resultSet)
    // a new Set [
    //   'Kim',
    //   'Grace',
    // ]
    const capitalize = (str: string) =>
      str[0].toUpperCase() + str.slice(1)
    const capitalizeAll = mapValues(capitalize)
    
    const peopleArr = ['kim', 'grace']
    const resultArr = capitalizeAll(peopleArr)
    console.log(resultArr)
    // a new array [
    //   'Kim',
    //   'Grace',
    // ]
    
    const peopleSet = new Set(['kim', 'grace'])
    const resultSet = capitalizeAll(peopleSet)
    console.log(resultSet)
    // a new Set [
    //   'Kim',
    //   'Grace',
    // ]
    

Why Build It?#

I wanted a utility library that worked with data types generically. Years ago I built a crude personal version I really liked. Common FP is a refined and expanded version that hopefully others find helpful.

Who Is This For?#

Common FP is for people who

Who Isn't This For?#

Common FP is not for everyone. You may prefer other libraries if you