Git Product home page Git Product logo

map-chain's Introduction

Map Chain

Description

This function allows you to perform a chain of transformations on a given value using the provided functions.

Note

The function does not implement copy/deep copy of reference types. If you want to avoid mutating the original object, take care to copy it before passing it to the function.

Arguments

  • initialValue <any> - The value on which the transformations will be performed (set during function initialization).
  • globalFailValue <any | function> - the value to be set in case the computations end in failure and there is no localFailValue in a particular transformation segment (set during function initialization). It is also possible to pass a function that takes the transformed value as an argument and returns a new value.
  • transformFn <function> - The transformation function applied to the current value in the chain. It takes the current value as an argument and returns a new value.
  • localFailValue <any | function> - The value to be set in case the computations end in failure. It takes precedence over globalFailValue and is set for each transformation segment. It is also possible to pass a function that takes the transformed value as an argument and returns a new value.
  • truthCondition <function> - The truth condition that determines whether the chain should continue after a transformation. This is an optional argument that can be a function. If provided, it takes the result of transformFn applied to the current initialValue and returns true or false. If truthCondition is not a function, the transformation will continue if the result is not equal to null or undefined.

To obtain the resulting value, simply call the function without any arguments ().

Usage Examples:

mapChain(10, 30)((x) => x * 2)(() => null, "fail")(() => null, 1008)(); // 'fail'
mapChain(10, 30)((x) => x * 2)(() => null)(() => null, 1008)(); // 30
mapChain(10)((x) => x * 2)(() => null)(() => null, 1008)(); // null
mapChain(10)((x) => x * 2)((x) => x + 2)((x) => x / 4)(); // 5.5
mapChain(10)((x) => x * 2)((x) => x + 2)((x) => x / 4); // expression () => {}

const flatterer = (persone) => {
  return mapChain(persone)(
    ({ firstName, lastName, age }) => `${firstName} ${lastName}, ${age - 5}`,
    (string) => {
      const persone = string.split(" ");
      persone[1] = persone[1][0] + ".,";
      return persone.join(" ");
    },
    (result) => result.length < 15
  )();
};

const sergeiVasilenko = flatterer({
  firstName: "Sergei",
  lastName: "Valilenko",
  age: 36,
}); // 'Sergei V., 31'

const joeSmith = flatterer({
  firstName: "Joe",
  lastName: "Smith",
  age: 30,
}); // 'Joe Smith, 25'

map-chain's People

Contributors

sergei-vasilenko avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.