reduce-first
Return early from reduce
Last updated 2 years ago by bconnorwhite .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install reduce-first 
SYNC missed versions from official npm registry.

reduce-first

NPM TypeScript

Return early from reduce.

If I should maintain this repo, please ⭐️ GitHub stars

DM me on Twitter if you have questions or suggestions. Twitter


Array.prototype.find returns the element from an array, and Array.prototype.reduce allows you to map to antother value. reduceFirst combines these to return a transformation of the first item that returns a value.

Installation

yarn add reduce-first
npm install reduce-first
pnpm add reduce-first

Usage

import reduceFirst from "reduce-first";

const list = ["a", "b", "c", "d", "e"];

const result1 = reduceFirst(list, (value, index, arr) => {
  if(value === "c") {
    return `found ${value}`;
  }
});
// result1 === "found c"

const result2 = reduceFirst(list, (value, index, arr) => {
  if(value === "x") {
    return `found ${value}`;
  }
});
// result2 === undefined

Dev Dependencies

  • autorepo: Autorepo abstracts away your dev dependencies, providing a single command to run all of your scripts.

License license

MIT - MIT License

Current Tags

  • 2.0.0                                ...           latest (2 years ago)

3 Versions

  • 2.0.0                                ...           2 years ago
  • 1.0.1                                ...           4 years ago
  • 1.0.0                                ...           4 years ago
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (1)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |