Skip to content
This repository was archived by the owner on Feb 4, 2018. It is now read-only.

Commit 578f085

Browse files
authored
Merge pull request #111 from bem-sdk/docs/serialization
docs: serialization
2 parents d131857 + 781c215 commit 578f085

File tree

2 files changed

+45
-16
lines changed

2 files changed

+45
-16
lines changed

README.md

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Contents
2626
* [Install](#install)
2727
* [Usage](#usage)
2828
* [API](#api)
29+
* [Serialization](#serialization)
2930
* [TypeScript support](#typescript-support)
3031
* [Debuggability](#debuggability)
3132
* [Deprecation](#deprecation)
@@ -270,7 +271,7 @@ buttonTextBoldName.belongsTo(buttonName); // false
270271

271272
### valueOf()
272273

273-
Returns object representing the entity name.
274+
Returns normalized object representing the entity name.
274275

275276
```js
276277
const BemEntityName = require('@bem/entity-name');
@@ -283,7 +284,15 @@ name.valueOf();
283284

284285
### toJSON()
285286

286-
Returns object for `JSON.stringify()` purposes.
287+
Returns raw data for `JSON.stringify()` purposes.
288+
289+
```js
290+
const BemEntityName = require('@bem/entity-name');
291+
292+
const name = new BemEntityName({ block: 'input', mod: 'available' });
293+
294+
JSON.stringify(name); // {"block":"input","mod":{"name":"available","val":true}}
295+
```
287296

288297
### toString()
289298

@@ -354,6 +363,31 @@ BemEntityName.isBemEntityName(entityName); // true
354363
BemEntityName.isBemEntityName({ block: 'button' }); // false
355364
```
356365

366+
Serialization
367+
-------------
368+
369+
The `BemEntityName` has `toJSON` method to support `JSON.stringify()` behaviour.
370+
371+
Use `JSON.stringify` to serialize an instance of `BemEntityName`.
372+
373+
```js
374+
const BemEntityName = require('@bem/entity-name');
375+
376+
const name = new BemEntityName({ block: 'input', mod: 'available' });
377+
378+
JSON.stringify(name); // {"block":"input","mod":{"name":"available","val":true}}
379+
```
380+
381+
Use `JSON.parse` to deserialize JSON string and create an instance of `BemEntityName`.
382+
383+
```js
384+
const BemEntityName = require('@bem/entity-name');
385+
386+
const str = '{"block":"input","mod":{"name":"available","val":true}}';
387+
388+
new BemEntityName(JSON.parse(str)); // BemEntityName({ block: 'input', mod: 'available' });
389+
```
390+
357391
TypeScript support
358392
------------------
359393

@@ -390,18 +424,6 @@ console.log(`name: ${name}`);
390424
// ➜ name: input_available
391425
```
392426

393-
Also `BemEntityName` has `toJSON` method to support `JSON.stringify()` behaviour.
394-
395-
```js
396-
const BemEntityName = require('@bem/entity-name');
397-
398-
const name = new BemEntityName({ block: 'input', mod: 'available' });
399-
400-
console.log(JSON.stringify(name));
401-
402-
// ➜ {"block":"input","mod":{"name":"available","val":true}}
403-
```
404-
405427
Deprecation
406428
-----------
407429

lib/entity-name.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ class BemEntityName {
284284
}
285285

286286
/**
287-
* Returns object representing the entity name. Is needed for debug in Node.js.
287+
* Returns normalized object representing the entity name.
288288
*
289289
* In some browsers `console.log()` calls `valueOf()` on each argument.
290290
* This method will be called to get custom string representation of the object.
@@ -305,7 +305,14 @@ class BemEntityName {
305305
valueOf() { return this._data; }
306306

307307
/**
308-
* Return raw data for `JSON.stringify()`.
308+
* Returns raw data for `JSON.stringify()` purposes.
309+
*
310+
* @example
311+
* const BemEntityName = require('@bem/entity-name');
312+
*
313+
* const name = new BemEntityName({ block: 'input', mod: 'available' });
314+
*
315+
* JSON.stringify(name); // {"block":"input","mod":{"name":"available","val":true}}
309316
*
310317
* @returns {BemSDK.EntityName.StrictRepresentation}
311318
*/

0 commit comments

Comments
 (0)