Skip to content

Commit

Permalink
feat: Add support for your own strinfigy/parse functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesus Seijas committed Aug 10, 2018
1 parent 4b20e87 commit 4499729
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 229 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ By default, it is able to stringify and parse objects that contains `BigInt` num

* [Installation](#installation)
* [Example of use](#example-of-use)
* [Using CircularJSON](#using-circularjson)
* [Adding custom types](#adding-custom-types)
* [Contributing](#contributing)
* [Code of Conduct](#code-of-conduct)
Expand All @@ -30,6 +31,20 @@ const str = Vilanova.stringify(obj);
const newObj = Vilanova.parse(str);
```

## Using CircularJSON

Vilanova allows you to use your own stringify and parse functions. One of the most used is CircularJSON.

```javascript
const CircularJSON = require('circular-json');
const Vilanova = require('vilanova');

const obj = { id: 1, name: 'name', value: 123456789012345678901234567890n };
const str = Vilanova.stringify(obj, CircularJSON.strinfigy);
const newObj = Vilanova.parse(str, CircularJSON.parse);
```


## Adding custom types

To add a custom type, you can use `Vilanova.addType` method. You must provide the name of the type, the reviver function and the replacer function.
Expand Down
23 changes: 15 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ class Vilanova {
* @param {Function} reviver - Function for the reviver of this type.
*/
static addType(name, replacer, reviver) {
Vilanova.types[name.toLowerCase()] = { replacer, reviver };
Vilanova.types[name] = { replacer, reviver };
}

/**
* Add the default types for Vilanova.
*/
static addDefaultTypes() {
// eslint-disable-next-line
Vilanova.addType('bigint', value => `#BigInt:${value}`, value => BigInt(value));
Vilanova.addType('bigint', value => `#bigint:${value}`, value => BigInt(value));
}

/**
Expand All @@ -29,7 +29,7 @@ class Vilanova {
* @returns {Object} Stringified version or the source object.
*/
static replacer(key, value) {
const type = Vilanova.types[(typeof value).toLowerCase()];
const type = Vilanova.types[(typeof value)];
if (type) {
return type.replacer(value);
}
Expand All @@ -40,10 +40,12 @@ class Vilanova {
* Converts a JavaScript value to a JSON string, using replacers for types
* that can be intercepted.
* @param {Object} obj - Input object.
* @param {Function} fn - Function that performs the stringify, undefined for JSON.stringify.
* @returns {String} Stringified version of the object.
*/
static stringify(obj) {
return JSON.stringify(obj, Vilanova.replacer);
static stringify(obj, fn) {
const stringifyFn = fn || JSON.stringify;
return stringifyFn(obj, Vilanova.replacer);
}

/**
Expand Down Expand Up @@ -78,18 +80,23 @@ class Vilanova {
}
const token = Vilanova.getToken(value);
if (token.type) {
const type = Vilanova.types[token.type.toLowerCase()];
const type = Vilanova.types[token.type];
if (type) {
return type.reviver(token.value);
}
}
return value;
}

/**
* Parses a JSON string, constructing the JavaScript value or object described by the string.
* @param {String} str - String to be parsed.
* @param {Function} fn - Function for the parse, undefined for default JSON.parse.
* @returns {Object} The object parsed from the string.
*/
static parse(str) {
return JSON.parse(str, Vilanova.reviver);
static parse(str, fn) {
const parseFn = fn || JSON.parse;
return parseFn(str, Vilanova.reviver);
}
}

Expand Down
221 changes: 11 additions & 210 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "vilanova",
"description": "JSON stringify/parse pluggable library with support for BigInt",
"version": "0.0.1",
"version": "0.1.0",
"main": "lib/index.js",
"keywords": [
"JSON",
Expand Down Expand Up @@ -39,6 +39,7 @@
"license": "MIT",
"dependencies": {},
"devDependencies": {
"circular-json": "^0.5.5",
"eslint": "^4.10.0",
"eslint-config-airbnb-base": "^12.1.0",
"eslint-plugin-import": "^2.8.0",
Expand Down
Loading

0 comments on commit 4499729

Please sign in to comment.