https://github.com/yarabey/stringifyit
Fast object stringifier
https://github.com/yarabey/stringifyit
javascript object stringify
Last synced: 10 months ago
JSON representation
Fast object stringifier
- Host: GitHub
- URL: https://github.com/yarabey/stringifyit
- Owner: yarabey
- Created: 2017-02-22T13:14:47.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2021-06-27T10:00:44.000Z (almost 5 years ago)
- Last Synced: 2025-08-15T04:12:52.897Z (10 months ago)
- Topics: javascript, object, stringify
- Language: JavaScript
- Size: 12.7 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# stringifyit
Fast node.js stringify library with sorting and typing. Provides [Stringifier](#Stringifier) class. [Stringifier](#Stringifier) provides [stringify Symbol](#stringifierstringify--symbol) to allow you [customize](#stringifierstringifycallback--function) stringifying your own classes.
See [benchmarks](#benchmarks) for compare to other libs.
# Install
`npm i stringifyit --save`
# Features
- Supports node.js >= 4.0.0
- Supports Map/WeakMap, Set/WeakSet and typed arrays
- Supports sort Set, Map, object keys and optional sort arrays
- Supports custom stringify rules for user-defined classes
- Useful for browsers
- Very fast stringify library
# API
## Classes
- Stringifier
-
Provides interface to stringify any value
Sort Map, Set and object keys by default without ability to avoid it
## Members
## Functions
-
stringifyit(value, [options]) ⇒string -
Helper for simple stringify single value
## Stringifier
Provides interface to stringify any value
Sort Map, Set and object keys by default without ability to avoid it
**Kind**: global class
* [Stringifier](#Stringifier)
* [new Stringifier([options])](#new_Stringifier_new)
* _instance_
* [.string](#Stringifier+string) : string
* [.update(value)](#Stringifier+update)
* _inner_
* [~stringifyCallback](#Stringifier..stringifyCallback) : function
* [~stringify](#Stringifier..stringify) : Symbol
* [~options](#Stringifier..options) : Object
### new Stringifier([options])
| Param | Type |
| --- | --- |
| [options] | [options](#Stringifier..options) |
### stringifier.string : string
Accumulator string
**Kind**: instance property of [Stringifier](#Stringifier)
**Access:** public
### stringifier.update(value)
Stringifies value and append it to current accumulator string
**Kind**: instance method of [Stringifier](#Stringifier)
| Param | Type |
| --- | --- |
| value | \* |
### Stringifier~stringifyCallback : function
Custom stringify callback declared with [stringify Symbol](#Stringifier..stringify)
**Kind**: inner typedef of [Stringifier](#Stringifier)
| Param | Type | Description |
| --- | --- | --- |
| stringifier | [Stringifier](#Stringifier) | Stringifier instance |
**Example**
```js
const {stringify} = require('stringifyit');
CustomType.prototype[stringify] = function (stringifier) {
stringifier.string += 'start';
stringifier.update(this.someProp);
stringifier.update(['use', 'any', 'type']);
stringifier.string += 'end';
}
```
### Stringifier~stringify : Symbol
Symbol to add custom stringify rules for user types
**Kind**: inner typedef of [Stringifier](#Stringifier)
### Stringifier~options : Object
Stringifier options
**Kind**: inner typedef of [Stringifier](#Stringifier)
**Properties**
| Name | Type | Description |
| --- | --- | --- |
| sortArrays | boolean | Sort arrays before stringify |
| includePrimitiveTypes | boolean | Stringify primitive values (and functions) types |
| includeConstructorNames | boolean | Stringify non-primitive values constructor names |
## stringify : [stringify](#Stringifier..stringify)
**Kind**: global variable
## stringifyit(value, [options]) ⇒ string
Helper for simple stringify single value
**Kind**: global function
| Param | Type |
| --- | --- |
| value | \* |
| [options] | [options](#Stringifier..options) |
**Example**
```js
const {stringifyit} = require('stringifyit');
stringifyit({key: 'value', value: 'key'}) === stringifyit({value: 'key', key: 'value'}); // true
stringifyit(new Set(['value1', 'value2'])) === stringifyit(new Set(['value2', 'value1'])); // true
stringifyit(new Map([['key', 'value'], ['value', 'key']])) === stringifyit(new Map([['value', 'key'], ['key', 'value']])); // true
stringifyit([1, 2, 3]) === stringifyit([1, 2, 3]); // true
stringifyit([1, 2, 3], {sortArrays: true}) === stringifyit([1, 3, 2], {sortArrays: true}); // true
stringifyit([1, 2, 3]) === stringifyit([1, 3, 2]); // false
stringifyit(5) === stringifyit('5'); // false
```
## Custom stringifiers [source](stringifiers)
### Object.prototype[[stringify](#Stringifier..stringify)] : [stringifyCallback](#Stringifier..stringifyCallback)
### Array.prototype[[stringify](#Stringifier..stringify)] : [stringifyCallback](#Stringifier..stringifyCallback)
### TypedArray.prototype[[stringify](#Stringifier..stringify)] : [stringifyCallback](#Stringifier..stringifyCallback)
### Map.prototype[[stringify](#Stringifier..stringify)] : [stringifyCallback](#Stringifier..stringifyCallback)
### WeakMap.prototype[[stringify](#Stringifier..stringify)] : [stringifyCallback](#Stringifier..stringifyCallback)
### Set.prototype[[stringify](#Stringifier..stringify)] : [stringifyCallback](#Stringifier..stringifyCallback)
### WeakSet.prototype[[stringify](#Stringifier..stringify)] : [stringifyCallback](#Stringifier..stringifyCallback)
### Date.prototype[[stringify](#Stringifier..stringify)] : [stringifyCallback](#Stringifier..stringifyCallback)
# Benchmarks
Benchmarked with Node.js v6.9.5
## Usage
* `npm run bench` to run benchmarking stringifyit operations/second for different cases
## Results
```
array x 1,947,707 ops/sec ±1.60% (85 runs sampled)
object x 2,366,530 ops/sec ±1.30% (89 runs sampled)
nestedObject x 29,384 ops/sec ±1.48% (87 runs sampled)
complexObject_5items x 35,847 ops/sec ±1.87% (87 runs sampled)
complexObject_10items x 18,407 ops/sec ±2.03% (87 runs sampled)
complexObject_100items x 1,682 ops/sec ±2.09% (85 runs sampled)
set x 215,921 ops/sec ±2.52% (84 runs sampled)
map x 190,451 ops/sec ±2.57% (84 runs sampled)
```
# License
MIT