# extripo

> A module for serialization of class instances.

Latest version **0.8.7** (published 2023-06-27) · IDontCare license · 0 weekly downloads

## Install

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

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.8.7 |
| Published | 2023-06-27 |
| First published | 2021-11-28 |
| Weekly downloads | 0 |
| License | IDontCare |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 34.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | hermit |
| Maintainers | ashen-hermit |
| Keywords | import, export, data, serialization, json, copy |

## Links

- npm: https://www.npmjs.com/package/extripo
- Repository: https://github.com/AshenHermit/extripo
- Issues: https://github.com/AshenHermit/extripo/issues
- npm.io page: https://npm.io/package/extripo

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.8.7 (latest) — 2023-06-27
- 0.8.6 — 2021-12-04
- 0.8.2 — 2021-11-30
- 0.7.0 — 2021-11-28

## README

# Extripo
[![Npm package version](https://badgen.net/npm/v/extripo)](https://www.npmjs.com/package/extripo)  
A js module for serialization of class instances.  
It has no dependencies.

## It's all about storing data.  
This small module provides a class `Exportable`, with which you can make some data class, add in it some fields, arrays, dictionaries, containing also instances of other classes and export an instance of that class into a simple nested dictionary, or import that dictionary and get full featured class instance with data that you imported.   

## Installation
With npm:
```
npm i extripo
```
or
```
npm i https://github.com/AshenHermit/extripo.git
```
Or you can use minified script `build/extripo.min.js`
```html
<script src="https://raw.githubusercontent.com/AshenHermit/extripo/master/build/extripo.min.js"></script>
```
And then get items from it like so
```javascript
var {Exportable, FC} = extripo
```

## Usage
<table>
<tr>
<td> 

**We write data classes with some methods, and want them to be exportable/importable.**  

</td>
<td> 

**So we extend `Exportable` class and do some configuration of fields**

</td>
</tr>
<tr>
<td>

```javascript
class Room{
    constructor(area){
        this.settler = null // instance of "Person"
        this.furniture = {} // instances of "Furniture"
        this.complaints = [] // instances of "Complaint"
        this.tmpPrice = 0 // do not export
        this.area = area
    }
    isOccupied(){
        return this.settler != null
    }
    calculatePrice(){
        this.tmpPrice = this.area * 4
        if(this.isOccupied()) this.tmpPrice *= 9999
        return this.tmpPrice
    }
}
class Complaint{
    constructor(message){
        this.message = message
    }
    printMessage(){
        console.log("complaint message: " + this.message)
    }
}
class Person{name=""}
class Furniture{type=""}
```

</td>
<td> 

```javascript
const { Exportable, FC } = require("extripo");

class Room extends Exportable{
    constructor(area){
        super()
        this.configFields({
            settler: FC.instanceOf(Person),
            furniture: FC.dictOf(Furniture),
            complaints: FC.arrayOf(Complaint),
            tmpPrice: FC.ignore()
        })
        this.settler = null
        this.furniture = {}
        this.complaints = []
        this.tmpPrice = 0
        this.area = area
    }
    isOccupied(){
        return this.settler != null
    }
    calculatePrice(){
        this.tmpPrice = this.area * 4
        if(this.isOccupied()) this.tmpPrice *= 9999
        return this.tmpPrice
    }
}
class Complaint extends Exportable{
    constructor(message){
        super()
        this.message = message
    }
    printMessage(){
        console.log("complaint message: " + this.message)
    }
}
class Person extends Exportable{name=""}
class Furniture extends Exportable{type=""}
```

</td>
</tr>
</table>

Now we can call exportData or importData on instances of our classes.  
Let's check if everything works.

```javascript
// instancing room and some fields
var room = new Room(3, false) 
room.settler = new Person(); 
room.settler.name = "pillow fan";
room.furniture["le_rock"] = new Furniture()
room.complaints.push(new Complaint("le_rock is too comfortable"))

// checking methods
console.log(room.calculatePrice()) // 119988
room.complaints[0].printMessage() // complaint message: le_rock is too comfortable

// exporting room into a dictionary
var data = room.exportData()
console.log(data) // {settler: {…}, furniture: {…}, complaints: Array(1), area: 3}
console.log(JSON.stringify(data)) // {"settler":{"name":"pillow fan"},"furniture":{"le_rock": ...

// creating instance from dictionary
var newRoom = Room.create(data)
// checking methods
console.log(newRoom.calculatePrice()) // 119988
newRoom.complaints[0].printMessage() // complaint message: le_rock is too comfortable
// ok everything works
```

The ways to export:
```javascript
var data = room.exportData()
var jsonString = room.exportJSON()
```

The ways to import:
```javascript
var room = Room.create(data)
var room = Room.createFromJSON(jsonString)
var room = new Room().importData(data)
var room = new Room().importJSON(jsonString)
```

You can also deep copy an instance:
```javascript
var roomCopy = room.copy()
```

If you want to do some initialization step after importing the data or some output data processing, you can do something like this:

```javascript
class ComplicatedThing extends Exportable{
    constructor(area){
        super()
        /* fields */
        this.initialize()
    }
    initialize(){/* ... */}
    processExportData(data){/* ... */}

    importData(data){
        super.importData(data)
        this.initialize()
        return this
    }
    exportData(){
        var data = super.exportData()
        data = this.processExportData(data)
        return data
    }
}
```

If you want provide class depending on element of container and its key/index, use this:

```javascript
class DomElement extends Exportable{
    constructor(area){
        super()
        this.configFields({
            children: FC.arrayOf((child, i)=>{
                if(child.tag=="button") return ButtonClass
                else if(child.tag=="textarea") return TextareaClass
            }),

            // bad example, in this case the best way 
            // to implement this is to just make 
            // a new class with fields named like these keys
            properties: FC.arrayOf((prop, key)=>{
                if(key=="class_list") return ClassList
                else if(key == "events_config") return EventsProcessorConfig
            })
        })
        this.children = []
        this.properties = {}
        this.tag = "div"
    }
}
```

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