# objectra

> Utility for deep object duplication, serialization and parsing

Latest version **1.1.1** (published 2021-12-04) · ISC license · 0 weekly downloads

## Install

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

## 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 | 1.1.1 |
| Published | 2021-12-04 |
| First published | 2021-12-01 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 26.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Vadym Iefremov |
| Maintainers | vaddims |
| Keywords | copy, duplicate, parse, serialize, inheritance |

## Links

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

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 1.1.1 (latest) — 2021-12-04
- 1.1.0 — 2021-12-01

## README

# Objectra

Utility for deep object duplication, serialization and parsing.

# Install

```bash
npm install objectra
```

# Usage

## Basic functionality with built-in [transformators](#transformators)

Import the Objectra class

```typescript
const { Objectra } = require('objectra');
```

Let's say we want to copy a value without losing any data (for example object inheritance) but remove all object references. In this case the `duplicate` method will help.

```typescript
const set = new Set<string>(['Hello world']);
const duplicatedValue = Objectra.duplicate(set);
```

But sometimes we may have another need for example to serialize a value into a pure object.

```typescript
const serializedSet = Objectra.serialize(set);
```

The returned data from the above method will return an object with the Objectra schema which gives the ability to parse it at any time.

```typescript
const parsedSet = Objectra.parse(serializedSet);
```
## Transformators

A Transformator is a set of transformers (parsing instructions) that Objectra uses to parse values. Each class has its own transformator. Objectra does not support implicit parsing of a class object and therefore if we try to parse a class instance that does not have a transformator registered it will throw an error.

__Built-in class transformators:__

* All primitives (including Symbol and BigInt)
* Object
* Array
* Objectra
* Map
* Set

## Transformator creation

To understand how a transformator works we first create a class.

```typescript
class Point {
    constructor(public x: number, public y: number) {}
}
```

Now we can create a transformator for the class. Let's add 2 methods in it: 

* __construct__ which takes as an argument the value that was in the class before serialization and returns an instance of the class
* __simplify__ which takes an instance of a class as an argument and returns it as a pure object (object or array) or even as a primitive value

```typescript
Objectra.addTransformator(Point, {
    construct: (content: any) => new Point(content.x, content.y),
    simplify: (point: Point) => ({ ...point }),
});
```

And it's all! We can now use Objectra to manage all future Point instances.

# License

[ISC](https://choosealicense.com/licenses/isc/)


# Author
- [Vaddims](https://github.com/Vaddims)

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