@@ -26,6 +26,7 @@ Contents
26
26
* [ Install] ( #install )
27
27
* [ Usage] ( #usage )
28
28
* [ API] ( #api )
29
+ * [ Serialization] ( #serialization )
29
30
* [ TypeScript support] ( #typescript-support )
30
31
* [ Debuggability] ( #debuggability )
31
32
* [ Deprecation] ( #deprecation )
@@ -270,7 +271,7 @@ buttonTextBoldName.belongsTo(buttonName); // false
270
271
271
272
### valueOf()
272
273
273
- Returns object representing the entity name.
274
+ Returns normalized object representing the entity name.
274
275
275
276
``` js
276
277
const BemEntityName = require (' @bem/entity-name' );
@@ -283,7 +284,15 @@ name.valueOf();
283
284
284
285
### toJSON()
285
286
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
+ ```
287
296
288
297
### toString()
289
298
@@ -354,6 +363,31 @@ BemEntityName.isBemEntityName(entityName); // true
354
363
BemEntityName .isBemEntityName ({ block: ' button' }); // false
355
364
```
356
365
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
+
357
391
TypeScript support
358
392
------------------
359
393
@@ -390,18 +424,6 @@ console.log(`name: ${name}`);
390
424
// ➜ name: input_available
391
425
```
392
426
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
-
405
427
Deprecation
406
428
-----------
407
429
0 commit comments