Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pichsenmeister/json-transients
A simple and minimalist wrapper library to deal with JavaScript object to JSON transformations that supports removing transient fields.
https://github.com/pichsenmeister/json-transients
json json-data json-parser json-schema jsontool
Last synced: about 1 month ago
JSON representation
A simple and minimalist wrapper library to deal with JavaScript object to JSON transformations that supports removing transient fields.
- Host: GitHub
- URL: https://github.com/pichsenmeister/json-transients
- Owner: pichsenmeister
- License: mit
- Created: 2020-05-11T02:41:06.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-03-04T17:24:30.000Z (almost 2 years ago)
- Last Synced: 2024-12-15T11:36:58.743Z (about 1 month ago)
- Topics: json, json-data, json-parser, json-schema, jsontool
- Language: JavaScript
- Homepage:
- Size: 2.36 MB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 16
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# json-transients
A simple and minimalist wrapper library to deal with JavaScript object to JSON transformations that supports removing transient fields.
* [Installation](#installation)
* [Getting started](#getting-started)
* [License](#license)## Installation
Install via npm
```
npm install json-transients
```or yarn
```
yarn add json-transients
```## Getting started
Let's take a very simple JSON object as example ...
```
const json = {
$_transient: '',
isUndefined: undefined,
str: "string",
number: 1,
bool: true,
fn: () => { }
}
```... and transform it to remove transient fiels (properties prefixed with `$_` in this example)
```
const JsonTransients = require('json-transients')const jst = new JsonTransients()
const result = jst.transform(json)
```This will remove all transient fields and return a valid JSON object:
```
{
isUndefined: null,
str: "string",
number: 1,
bool: true
}
```## Config
Each instance of `JsonTransients` takes a configuration object with following properties:
| Property | Required | Default | Description |
| ---- | ---- | ---- | ---- |
| `prefix` | no | `$_` | Prefix for transients fields that are being removed |
| `transformUndefined` | no | `true` | Sets all `undefined` properties to `null`. If set to `false` all `undefined` properties will be removed. You can also define a custom handler. |
| `transformDate` | no | `toISOString()` | Define a custom handler to transfrom `Date` objects. Sets all date properties to ISO string by default. |### Example
```
const jst = new JsonTransients({
// use a custom prefix
prefix: 'CUSTOM_`,
// transform all undefined properties to "not_defined" string
transformUndefined: () => {
return 'not_defined'
},
// transform all dates to timestamp
transformDate: (value) => {
return value.getTime()
}
})
```## License
This project is licensed under the MIT license, Copyright (c) 2020 David Pichsenmeister | [pichsenmeister.com](https://pichsenmeister.com). For more information see [LICENSE](LICENSE).