# nn-transformer

> Recursively transforms every marked object in an array or object.

Latest version **2.0.0** (published 2021-10-30) · ISC license · 0 weekly downloads

## Install

```sh
npm install nn-transformer
pnpm add nn-transformer
yarn add nn-transformer
bun add nn-transformer
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2021-10-30 |
| First published | 2021-10-21 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 17.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Nneba13 |
| Maintainers | nneba13 |

## Links

- npm: https://www.npmjs.com/package/nn-transformer
- npm.io page: https://npm.io/package/nn-transformer

## Dependencies (2)

- [nn-validate](https://npm.io/package/nn-validate.md) ^1.2.0
- [nn-type-cast](https://npm.io/package/nn-type-cast.md) ^1.0.0

## Recent versions

- 2.0.0 (latest) — 2021-10-30
- 1.1.0 — 2021-10-27
- 1.0.2 — 2021-10-27
- 1.0.1 — 2021-10-27
- 1.0.0 — 2021-10-21

## README

# `nn-transformer`

> Recursively transforms every marked object in an array or object.


## Object Transformations

When passing a value to the _transform_ function any marked objects will be transformed using a transformer function. A marked object is any object with a "\*" value. If value of "\*" is the name of a transformer then the object will be passed to the transformer function. If the value of "\*" is not a named transformer then the default transformer will be used.


## Usage

transform
* __value__: any - The value to transform.
* __configuration__: object - The transform configuration.
	* __filter__ (optional): FilterFn | string[] | string - Constraints.
	* __maxLevel__ (optional): number - The maximum depth. Default is 1024.
	* __transformer__: TransformerFn - Default transformer function.
	* __transformers__: object - Object containing named TransformerFn.
* __options__: object (optional) - Arbitrary object to pass to the transformer function.


FilterFn
* Params
	* value: any
* Returns
	* boolean - true to accept the value or false if value is unacceptable.


TransformerFn
* Params
	* args: object
		* __level__: number - Indicates how deep an object will be traversed during the transformation.
		* __property__: string - The property being transformed.
		* __value__: any - The value being transformed.
		* __options__: object - Object of options values passed to the transformer function.
		* __transform__: ( value: any ) => any - Use to transform a nested value.
* Returns
	* any - The transformed value.


## Build-in Transformers
* \*map
	* This transformer will transform nested values.
* \*raw
	* This transformer will return the object without any transformations, including nested objects. This transformer is also useful for filtering out values.


## Examples

```
import { transform } from 'nn-transformer';

let configuration =
{
	transformer ( { level, property, value, options, transform } )
	{
		return options[ value.prop ];
	},
	transformers:
	{
		"A": ( { value, options } ) => ( options[ value.prop ] )
	}
};


await transform( 123, configuration );
// No effect, returns 123


// Array
await transform( [ 123, 456, { "*": "*raw", value: 789 } ], configuration );
// Returns [ 123, 456, { value: 789 } ]


// Object (using transformer function)
await transform( { "A": "ABC", "B": { "*": "A", "prop": "DEF" } }, configuration, { "DEF": 123 } );
// Returns { "A": "ABC", "B": 123 }


// Object (uses default transformer because there is no transformer named '*')
await transform( { "A": "ABC", "B": { "*": "*", "prop": "DEF" } }, configuration, { "DEF": 123 } );
// Returns { "A": "ABC", "B": 123 }


// Object (filter values)
let data =
{
	"A": "ABC",
	"B": { "*": { "transformer": "*raw", "filter": "B", "value": "DEF" } }, // Note: The value will replace this object.
	"C": { "*": { "transformer": "*raw", "filter": "C", "value": "GHI" } },
	"D": { "*": { "transformer": "*raw", "filter": "D", "*": "JKL" } } // Note: The '*' value will replace the '*' object.
};

let configuration =
{
	filter: [ 'B', 'D' ]
};

await transform( data, configuration );
// Returns { "A": "ABC", "B": "DEF", "D": { "*": "JKL" } }
```

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