# fof

> functional transforms, accessors, and an ES5 fat arrow translator

Latest version **1.2.0** (published 2016-04-13) · CC0-1.0 license · 0 weekly downloads

## Install

```sh
npm install fof
pnpm add fof
yarn add fof
bun add fof
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2016-04-13 |
| First published | 2016-04-11 |
| Weekly downloads | 0 |
| License | CC0-1.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Shawn Allen |
| Maintainers | shawnbot |
| Keywords | function, transform, accessor, arrow, property, dotmap |

## Links

- npm: https://www.npmjs.com/package/fof
- Repository: https://github.com/shawnbot/fof
- Homepage: https://github.com/shawnbot/fof#readme
- Issues: https://github.com/shawnbot/fof/issues
- npm.io page: https://npm.io/package/fof

## Dependencies (2)

- [dotmap](https://npm.io/package/dotmap.md) ^0.1.0
- [through2](https://npm.io/package/through2.md) ^2.0.1

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.2.0 (latest) — 2016-04-13
- 1.1.1 — 2016-04-12
- 1.1.0 — 2016-04-11
- 1.0.1 — 2016-04-11

## README

# fof
Functional JavaScript transforms, and a handy [fat arrow] syntax translator for ES5! Some examples:

```js
var fof = require('fof');

// simple property accessor
fof('x')({x: 100}) === 100
fof('foo bar')({'foo bar': 'baz'}) === 'baz'

// deep property accessor, courtesy of dotmap
fof('x.y')){x: {y: 'z'}}) === 'z'
// you can use spaces here; '.' is the separator
fof('foo bar.baz')({'foo bar': {baz: 'qux'}}) === 'qux'

// numeric index accessor
fof(1)([true, false]) === false

// fat arrow syntax
fof('d => d.x')({x: 42}) === 42
// if, for some reason, your data has funky keys...
fof("d => d['gobbledy\\'gook']")({"gobbledy'gook": 1}) === 1
// note the double curly braces for return an object!
['a', 'b', 'c'].map(fof('(d, i) => {{data: d, index: i}}'))
// produces: [
//   {data: 'a', index: 0}, 
//   {data: 'b', index: 1}, 
//   {data: 'c', index: 2}
// ]

// object maps
fof({x: 0, y: 1})([-122, 35]) // produces {x: -122, y: 35}
// values in object maps can be any value recognized by fof()
fof({x: {y: 'd => d.z'}})({z: 100}) // produces: {x: {y: 100}}

// array maps
fof(['y', 'd => d.x'])({x: -100, y: 50}) // produces: [50, -100]
```

## API
The Node module `fof` exports a single function that returns a different type
of accessor function for different types of inputs, given `fof(x)`:

* If `x` is a string and roughly matches the syntax of an [ES6 fat arrow
  function][fat arrow], then you'll get the roughly equivalent ES5 function
  (minus the lexical scope, and a couple of other caveats). For example:
  * `fof('d => d.x')` returns an accessor for the `x` property of an object.
  * `fof('d => [+d.x, +d.y]')` returns a function that turns an object with `x`
    and `y` keys into a two-element array of coerced numbers.
  * *Note*: our "version" of fat arrow functions have an explicit `return`
    wrapped around the function body, so you _can_ use comma-separated
    expressions like `d => d.x *= 100, d` to modify the object in place and
    return it. This is not possible in native implementations.
* If `x` is a string (and _not_ a fat arrow expression), you get a deep object
  accessor from [dotmap].
* If `x` is a number, you get a numeric index accessor.
* If `x` is an object (and _not_ an Array), you get a _mapping_ function that
  returns a new Object the same set of keys and a corresponding value
  determined by `fof(x[key])`. This allows you to do things like:
  * `fof({x: 0, y: 1})` returns a function that converts a 2-element Array into
    an Object with `x` and `y` keys corresponding to the first and second
    elements.
* If `x` is an Array, you get a mapping function that returns a new Array with
  values corresponding to each index, for which `fof(x[i])` is the value. This
  allows you to do the inverse of object mapping:
  * `fof(['x', 'y'])` returns a function that converts the `x` and `y` keys of
    an object into a 2-element array.

### `fof.stream(x, options)`
The module's `stream()` method returns an object transform stream that can
optionally filter objects by an expression and leave the input objects
untouched, or filter and transform with independent expressions:

```js
// transform using an expression
objectStream
  .pipe(fof.stream('d => [d.x, d.y]'))

// filter using an expression, leaving the data as-is
objectStream
  .pipe(fof.stream('d => d.x > 100', {
    filter: true
  }))

// filter and transform with expressions
objectStream
  .pipe(fof.stream('d => [d.x, d.y]', {
    filter: 'd => d.x > 100'
  }))
```

Check out [the tests](test/spec.js) for more examples.

[dotmap]: https://github.com/jden/dotmap
[fat arrow]: https://codeplanet.io/es6-fat-arrow-functions/

---
_Source: https://npm.io/package/fof · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
