{"id":17228559,"url":"https://github.com/benadamstyles/rapt","last_synced_at":"2026-02-06T20:31:06.155Z","repository":{"id":65372045,"uuid":"120892136","full_name":"benadamstyles/Rapt","owner":"benadamstyles","description":"Wrap any value so you can map over it","archived":false,"fork":false,"pushed_at":"2019-05-01T16:02:17.000Z","size":2795,"stargazers_count":3,"open_issues_count":10,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-22T13:15:41.650Z","etag":null,"topics":["chain","flow","functional-js","immutable","map"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/benadamstyles.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-02-09T10:28:19.000Z","updated_at":"2018-06-26T14:22:27.000Z","dependencies_parsed_at":"2023-01-19T23:16:41.337Z","dependency_job_id":null,"html_url":"https://github.com/benadamstyles/Rapt","commit_stats":null,"previous_names":["leeds-ebooks/rapt"],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benadamstyles%2FRapt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benadamstyles%2FRapt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benadamstyles%2FRapt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benadamstyles%2FRapt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/benadamstyles","download_url":"https://codeload.github.com/benadamstyles/Rapt/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247451626,"owners_count":20940946,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chain","flow","functional-js","immutable","map"],"created_at":"2024-10-15T04:44:28.995Z","updated_at":"2026-02-06T20:31:06.149Z","avatar_url":"https://github.com/benadamstyles.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"![Rapt logo](https://leeds-ebooks.co.uk/images/oss/min/rapt-100.png)\n\n[![npm version](https://badge.fury.io/js/rapt.svg)](https://www.npmjs.com/package/rapt)\n[![Build Status](https://travis-ci.org/Leeds-eBooks/rapt.svg?branch=master)](https://travis-ci.org/Leeds-eBooks/rapt)\n[![Greenkeeper badge](https://badges.greenkeeper.io/Leeds-eBooks/rapt.svg)](https://greenkeeper.io/)\n\nRapt is a small package that wraps any value, allowing you to map over it. A bit like [Lodash](https://lodash.com/)’s [chain](https://lodash.com/docs/4.17.5#chain), or [Gulp](https://gulpjs.com/)’s [pipe](https://github.com/gulpjs/gulp/blob/v3.9.1/docs/API.md), but for values rather than collections or streams.\n\nRapt does not exist to reduce the number of characters in your code, nor to make your code run faster. For many, it will not even be the clearest way to write your code. But if, like me, you find it easier to read and understand a list of functions than a variety of assignments, operators and returns, then Rapt might help you enjoy the code you write a little more.\n\nIt lends itself to a highly functional style of JS programming, allowing you to work with and transform values without having to assign them to variables. It works best with immutable values (numbers, strings, [immutable collections](https://facebook.github.io/immutable-js/)). It is particularly well suited to the latter, and is very satisfying if you hate using `let` just so you can reassign an immutable value after modifying it, i.e. `let x = Map(); x = x.set('a', 1)`.\n\n\u003e It can be used with plain mutable objects and arrays but it may be counter-productive in terms of clarity, as the functions you pass to `Rapt#map()` will no longer be side-effect-free.\n\nRapt is particularly useful when you want to avoid unnecessarily executing expensive operations, but you don’t want to give up your functional style.\n\n## Installation\n\nRapt is [available on npm](https://www.npmjs.com/package/rapt).\n\n```sh\nnpm install --save rapt\n```\n\n```sh\nyarn add rapt\n```\n\n## Usage\n\n### CommonJS\n\n```js\nconst {rapt} = require('rapt')\n// or\nconst rapt = require('rapt').default\n```\n\n### ES Modules\n\n```js\nimport rapt from 'rapt'\n```\n\n## Examples\n\n```js\nimport {Map} from 'immutable'\n\n// without Rapt\nconst processUser = user =\u003e {\n  log(user)\n  let userMap = Map(user)\n  if (emailHasBeenVerified) {\n    userMap = userMap.set('verified', true)\n  }\n  syncWithServer(userMap)\n  return userMap\n}\n\n// with Rapt\nconst processUser = user =\u003e\n  rapt(user)\n    .tap(log)\n    .map(Map)\n    .mapIf(emailHasBeenVerified, u =\u003e u.set('verified', true))\n    .tap(syncWithServer)\n    .val()\n```\n\n```js\nimport _ from 'lodash'\n\n// without Rapt\nconst countItems = (shouldFilter, hugeArrayOfItems) =\u003e {\n  let arr = _.compact(hugeArrayOfItems)\n  if (shouldFilter) {\n    arr = arr.filter(expensiveFilterFunction)\n  }\n  const count = arr.length\n  console.log(`We have ${count} items`)\n  return count\n}\n\n// with Rapt\nconst countItems = (shouldFilter, hugeArrayOfItems) =\u003e\n  rapt(hugeArrayOfItems)\n    .map(compact)\n    .mapIf(shouldFilter, arr =\u003e arr.filter(expensiveFilterFunction))\n    .map(items =\u003e items.length)\n    .tap(count =\u003e console.log(`We have ${count} items`))\n    .val()\n```\n\n## Types\n\nRapt is written using [Flow](https://flow.org/), and works well with it – with the caveat that currently, due to [an issue with how Flow handles booleans](https://github.com/facebook/flow/issues/4196), the type of the first argument to `Rapt#mapIf()` must be `true | false` rather than `boolean` (no, they’re currently [not the same thing](https://github.com/facebook/flow/issues/4196)!).\n\n## Promises / async\n\nRapt offers first-class support for asynchronous JavaScript with [`.thenMap()`](#thenmapfunction), but it also works well without that method:\n\n```js\n// async function, returns a Promise\nconst fetchUserFromDatabase = userId =\u003e database.child('users').child(userId)\n\n// sync function, returns the userObject, mutated\nconst processUser = userObject =\u003e doSomethingToTheUserObject(userObject)\n\nconst getUser = userId =\u003e\n  rapt(userId)\n    .map(fetchUserFromDatabase)\n    .thenMap(processUser)\n    .val() // returns a promise because we used fetchUserFromDatabase\n\n// Equivalent to:\nconst getUser = userId =\u003e\n  rapt(userId)\n    .map(fetchUserFromDatabase)\n    .map(userPromise =\u003e userPromise.then(processUser))\n    .val() // returns a promise because we used fetchUserFromDatabase\n\nawait getUser('user001')\n```\n\n## API\n\n### `.map(Function)`\n\nTransform your wrapped value by mapping over it.\n\n```js\nrapt('hello')\n  .map(s =\u003e `${s} world`)\n  .val() // returns 'hello world'\n```\n\n### `.mapIf(true | false, Function)`\n\nTransform your wrapped value by mapping over it, if another value is truthy. Otherwise, do nothing and pass on the value for further chaining.\n\n```js\nrapt('hello')\n  .mapIf(true, s =\u003e `${s} world`)\n  .val() // returns 'hello world'\n\nrapt('hello')\n  .mapIf(false, s =\u003e `${s} world`)\n  .val() // returns 'hello'\n```\n\n### `.flatMap(Function)`\n\nLike `.map()`, but you should return a `Rapt` in your function (equivalent to `.map().flatten()`).\n\n```js\nrapt('hello')\n  .flatMap(s =\u003e rapt(`${s} world`).map(s =\u003e `${s}!`))\n  .map(s =\u003e s.toUpperCase())\n  .val() // returns 'HELLO WORLD!'\n```\n\n### `.thenMap(Function)`\n\nLike `.map()`, but best used in a `rapt` chain that wraps a `Promise`. It calls `Promise.resolve()` on your wrapped value however, so it will also work on immediate values.\n\n```js\nconst asyncHello = () =\u003e new Promise(resolve =\u003e resolve('hello'))\n\nrapt(asyncHello())\n  .thenMap(s =\u003e `${s} world`)\n  .thenMap(s =\u003e s.toUpperCase())\n  .val() // returns a Promise of 'HELLO WORLD!'\n  .then(str =\u003e console.log(str)) // logs 'HELLO WORLD!'\n\nrapt('hello')\n  .thenMap(s =\u003e `${s} world`)\n  .thenMap(s =\u003e s.toUpperCase())\n  .val() // returns a Promise of 'HELLO WORLD!'\n  .then(str =\u003e console.log(str)) // logs 'HELLO WORLD!'\n```\n\n### `.flatten()`\n\nUnwraps a “nested” `Rapt` (see also [`.flatMap()`](#flatmapfunction)).\n\n```js\nrapt('hello')\n  .map(s =\u003e rapt(`${s} world`).map(s =\u003e `${s}!`))\n  .flatten()\n  .map(s =\u003e s.toUpperCase())\n  .val() // returns 'HELLO WORLD!'\n```\n\n### `.tap(Function)`\n\nExecute a side effect on your wrapped value without breaking the chain.\n\n```js\nrapt('hello')\n  .tap(s =\u003e console.log(s)) // logs 'hello'\n  .map(s =\u003e `${s} world`)\n  .val() // returns 'hello world'\n\nrapt('hello')\n  .tap(s =\u003e `${s} world`)\n  .val() // returns 'hello'\n\n// Careful of side effects when working with a mutable value!\nrapt({a: 1})\n  .tap(obj =\u003e {\n    obj.b = 2\n  })\n  .val() // returns {a: 1, b: 2}\n```\n\n### `.forEach(Function)`\n\nExecute a side effect on your wrapped value and end the chain (use if you don’t need to return the wrapped value).\n\n```js\nrapt('hello')\n  .map(s =\u003e `${s} world`)\n  .forEach(s =\u003e console.log(s)) // logs 'hello world'\n\nrapt('hello').forEach(s =\u003e `${s} world`) // returns undefined\n```\n\n### `.val()` or `.value()`\n\nUnwrap your value and return it.\n\n```js\nrapt('hello').map(s =\u003e `${s} world`) // returns an instance of Rapt\n\nrapt('hello')\n  .map(s =\u003e `${s} world`)\n  .val() // returns 'hello world'\n\nrapt('hello')\n  .map(s =\u003e `${s} world`)\n  .value() // returns 'hello world'\n```\n\n### `isRapt()`\n\nA predicate function to check if a value is wrapped with `Rapt`.\n\n```js\nimport {isRapt} from 'rapt'\n// or\nconst {isRapt} = require('rapt')\n\nconst a = rapt(3)\n  .map(x =\u003e x * 2)\n  .val()\n\nconst b = rapt(3).map(x =\u003e x * 2)\n\nisRapt(5) // returns false\nisRapt(rapt(5)) // returns true\nisRapt(a) // returns false\nisRapt(b) // returns true\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbenadamstyles%2Frapt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbenadamstyles%2Frapt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbenadamstyles%2Frapt/lists"}