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

Commit 184757d

Browse files
authored
Merge pull request #93 from bem-sdk/issue-92
Typings
2 parents 6734ab3 + 54a24fb commit 184757d

22 files changed

+238
-74
lines changed

README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Contents
2626
* [Install](#install)
2727
* [Usage](#usage)
2828
* [API](#api)
29+
* [TypeScript support](#typescript-support)
2930
* [Debuggability](#debuggability)
3031

3132
Install
@@ -68,8 +69,8 @@ API
6869
* [toString()](#tostring)
6970
* [valueOf()](#valueof)
7071
* [toJSON()](#tojson)
71-
* [#isBemEntityName(entityName)](#isbementitynameentityname)
72-
* [#create(object)](#createobject)
72+
* [static isBemEntityName(entityName)](#static-isbementitynameentityname)
73+
* [static create(obj)](#static-createobj)
7374

7475
### constructor({ block, elem, mod })
7576

@@ -256,13 +257,13 @@ name.valueOf();
256257

257258
Returns object for `JSON.stringify()` purposes.
258259

259-
### #isBemEntityName(entityName)
260+
### static isBemEntityName(entityName)
260261

261262
Determines whether specified entity is an instance of BemEntityName.
262263

263264
Parameter | Type | Description
264265
-------------|-----------------|-----------------------
265-
`entityName` | `BemEntityName` | The entity to check.
266+
`entityName` | `*` | The entity to check.
266267

267268
```js
268269
const BemEntityName = require('@bem/entity-name');
@@ -273,7 +274,7 @@ BemEntityName.isBemEntityName(entityName); // true
273274
BemEntityName.isBemEntityName({ block: 'button' }); // false
274275
```
275276

276-
### #create(object)
277+
### static create(object)
277278

278279
Creates BemEntityName instance by any object representation or a string.
279280

@@ -311,6 +312,13 @@ BemEntityName.create({ block: 'my-button', mod: 'focused' });
311312
// ➜ BemEntityName { block: 'my-button', mod: { name: 'focused', val: true } }
312313
```
313314

315+
TypeScript support
316+
------------------
317+
318+
The package includes [typings](./index.d.ts) for TypeScript. You have to set up transpilation yourself. When you set `module` to `commonjs` in your `tsconfig.json` file, TypeScript will automatically find the type definitions for `@bem/entity-name`.
319+
320+
The interfaces are provided in global namespace `BemSDK.EntityName`. It is necessary to use interfaces in JsDoc.
321+
314322
Debuggability
315323
-------------
316324

globals.d.ts

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
declare namespace BemSDK {
2+
export namespace EntityName {
3+
/**
4+
* Types of BEM entities.
5+
*/
6+
export type TYPE = 'block' | 'blockMod' | 'elem' | 'elemMod';
7+
8+
/**
9+
* Abstract object to represent entity name
10+
*/
11+
interface AbstractEntityRepresentation {
12+
/**
13+
* The block name of entity.
14+
*/
15+
block: string;
16+
/**
17+
* The element name of entity.
18+
*/
19+
elem?: string;
20+
mod?: any;
21+
}
22+
23+
/**
24+
* Object to represent modifier of entity name.
25+
*/
26+
export interface ModifierRepresentation {
27+
/**
28+
* The modifier name of entity.
29+
*/
30+
name: string;
31+
/**
32+
* The modifier value of entity.
33+
*/
34+
val: string | true;
35+
}
36+
37+
/**
38+
* Strict object to represent entity name.
39+
*/
40+
export interface StrictRepresentation extends AbstractEntityRepresentation {
41+
/**
42+
* The modifier of entity.
43+
*/
44+
mod?: ModifierRepresentation;
45+
}
46+
47+
/**
48+
* Object to create representation of entity name.
49+
*/
50+
export interface Options extends AbstractEntityRepresentation {
51+
/**
52+
* The modifier of entity.
53+
*/
54+
mod?: string | {
55+
/**
56+
* The modifier name of entity.
57+
*/
58+
name: string;
59+
/**
60+
* The modifier value of entity.
61+
*/
62+
val?: string | boolean;
63+
};
64+
/**
65+
* The modifier name of entity. Used if `mod.name` wasn't specified.
66+
* @deprecated use `mod.name` instead.
67+
*/
68+
modName?: string;
69+
/**
70+
* The modifier value of entity. Used if neither `mod.val` nor `val` were not specified.
71+
* @deprecated use `mod.name` instead.
72+
*/
73+
modVal?: string;
74+
}
75+
76+
/**
77+
* Non-strict object to represent entity name.
78+
*
79+
* Contains old field: `val`, `modName` and `modVal.
80+
*/
81+
export interface NonStrictRepresentation extends AbstractEntityRepresentation {
82+
/**
83+
* The modifier of entity.
84+
*/
85+
mod?: string | {
86+
/**
87+
* The modifier name of entity.
88+
*/
89+
name: string;
90+
/**
91+
* The modifier value of entity.
92+
*/
93+
val?: string | boolean;
94+
};
95+
/**
96+
* The modifier value of entity. Used if neither `mod.val` were not specified.
97+
*/
98+
val?: string;
99+
/**
100+
* The modifier name of entity. Used if `mod.name` wasn't specified.
101+
*/
102+
modName?: string;
103+
/**
104+
* The modifier value of entity. Used if neither `mod.val` nor `val` were not specified.
105+
*/
106+
modVal?: string;
107+
}
108+
}
109+
}

index.d.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import './globals.d';
2+
3+
declare class BemEntityName {
4+
constructor(obj: BemSDK.EntityName.Options);
5+
6+
readonly block: string;
7+
readonly elem: string | undefined;
8+
readonly mod: BemSDK.EntityName.ModifierRepresentation | undefined;
9+
readonly modName: string | undefined;
10+
readonly modVal: string | true | undefined;
11+
readonly id: string;
12+
readonly type: BemSDK.EntityName.TYPE;
13+
14+
isSimpleMod(): boolean | null;
15+
toString(): string;
16+
valueOf(): BemSDK.EntityName.StrictRepresentation;
17+
inspect(depth: number, options: object): string;
18+
toJSON(): BemSDK.EntityName.StrictRepresentation;
19+
isEqual(entityName: BemEntityName): boolean;
20+
21+
static isBemEntityName(entityName: any): boolean;
22+
static create(obj: BemSDK.EntityName.NonStrictRepresentation | string): BemEntityName;
23+
}
24+
25+
export = BemEntityName;

index.js

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const stringifyEntity = require('@bem/naming').stringify;
99
* Enum for types of BEM entities.
1010
*
1111
* @readonly
12-
* @enum {String}
12+
* @enum {string}
1313
*/
1414
const TYPES = {
1515
BLOCK: 'block',
@@ -33,17 +33,9 @@ class EntityTypeError extends ExtendableError {
3333
}
3434
}
3535

36-
module.exports = class BemEntityName {
36+
class BemEntityName {
3737
/**
38-
* @param {object} obj — representation of entity name.
39-
* @param {string} obj.block — the block name of entity.
40-
* @param {string} [obj.elem] — the element name of entity.
41-
* @param {object} [obj.mod] — the modifier of entity.
42-
* @param {string} obj.mod.name — the modifier name of entity.
43-
* @param {string} [obj.mod.val] — the modifier value of entity.
44-
* @param {string} [obj.modName] — the modifier name of entity. Used if `mod.name` wasn't specified.
45-
* @param {string} [obj.modVal] — the modifier value of entity.
46-
* Used if neither `mod.val` nor `val` were not specified.
38+
* @param {BemSDK.EntityName.Options} obj — representation of entity name.
4739
*/
4840
constructor(obj) {
4941
if (!obj.block) {
@@ -94,7 +86,7 @@ module.exports = class BemEntityName {
9486
*
9587
* name.elem; // text
9688
*
97-
* @returns {string|undefined} - name of entity element.
89+
* @returns {?string} - name of entity element.
9890
*/
9991
get elem() { return this._data.elem; }
10092

@@ -112,7 +104,7 @@ module.exports = class BemEntityName {
112104
* modName.mod; // { name: 'disabled', val: true }
113105
* blockName.mod; // undefined
114106
*
115-
* @returns {{mod: string, val: (string|true)}|undefined} - entity modifier.
107+
* @returns {?BemSDK.EntityName.ModifierRepresentation} - entity modifier.
116108
*/
117109
get mod() { return this._data.mod; }
118110

@@ -121,7 +113,7 @@ module.exports = class BemEntityName {
121113
*
122114
* If entity is not modifier then returns `undefined`.
123115
*
124-
* @returns {string|undefined} - entity modifier name.
116+
* @returns {?string} - entity modifier name.
125117
* @deprecated - use `mod.name` instead.
126118
*/
127119
get modName() { return this.mod && this.mod.name; }
@@ -131,7 +123,7 @@ module.exports = class BemEntityName {
131123
*
132124
* If entity is not modifier then returns `undefined`.
133125
*
134-
* @returns {string|undefined} - entity modifier name.
126+
* @returns {?(string|true)} - entity modifier name.
135127
* @deprecated - use `mod.val` instead.
136128
*/
137129
get modVal() { return this.mod && this.mod.val; }
@@ -211,7 +203,7 @@ module.exports = class BemEntityName {
211203
*
212204
* name.isSimpleMod(); // null
213205
*
214-
* @returns {boolean|null}
206+
* @returns {(boolean|null)}
215207
*/
216208
isSimpleMod() {
217209
return this.mod ? this.mod.val === true : null;
@@ -250,7 +242,7 @@ module.exports = class BemEntityName {
250242
*
251243
* // ➜ { block: 'button', mod: { name: 'focused', value: true } }
252244
*
253-
* @returns {{block: string, elem: (string|undefined), mod: ({name: string, val: (string|true)}|undefined)}}
245+
* @returns {BemSDK.EntityName.StrictRepresentation}
254246
*/
255247
valueOf() { return this._data; }
256248

@@ -284,8 +276,7 @@ module.exports = class BemEntityName {
284276
/**
285277
* Return raw data for `JSON.stringify()`.
286278
*
287-
* @returns {{block: string, elem: (string|undefined),
288-
* mod: ({name: string, val: (string|true|undefined)}|undefined)}}
279+
* @returns {BemSDK.EntityName.StrictRepresentation}
289280
*/
290281
toJSON() {
291282
return this._data;
@@ -294,9 +285,6 @@ module.exports = class BemEntityName {
294285
/**
295286
* Determines whether specified entity is the deepEqual entity.
296287
*
297-
* @param {BemEntityName} entityName - the entity to compare.
298-
*
299-
* @returns {boolean} - A Boolean indicating whether or not specified entity is the deepEqual entity.
300288
* @example
301289
* const BemEntityName = require('@bem/entity-name');
302290
*
@@ -305,6 +293,9 @@ module.exports = class BemEntityName {
305293
*
306294
* inputName.isEqual(buttonName); // false
307295
* buttonName.isEqual(buttonName); // true
296+
*
297+
* @param {BemEntityName} entityName - the entity to compare.
298+
* @returns {boolean} - A Boolean indicating whether or not specified entity is the deepEqual entity.
308299
*/
309300
isEqual(entityName) {
310301
return entityName && (this.id === entityName.id);
@@ -313,16 +304,16 @@ module.exports = class BemEntityName {
313304
/**
314305
* Determines whether specified entity is instance of BemEntityName.
315306
*
316-
* @param {BemEntityName} entityName - the entity to check.
317-
*
318-
* @returns {boolean} A Boolean indicating whether or not specified entity is instance of BemEntityName.
319307
* @example
320308
* const BemEntityName = require('@bem/entity-name');
321309
*
322310
* const entityName = new BemEntityName({ block: 'input' });
323311
*
324312
* BemEntityName.isBemEntityName(entityName); // true
325313
* BemEntityName.isBemEntityName({}); // false
314+
*
315+
* @param {*} entityName - the entity to check.
316+
* @returns {boolean} A Boolean indicating whether or not specified entity is instance of BemEntityName.
326317
*/
327318
static isBemEntityName(entityName) {
328319
return entityName && entityName.__isBemEntityName__;
@@ -331,24 +322,15 @@ module.exports = class BemEntityName {
331322
/**
332323
* Creates BemEntityName instance by any object representation.
333324
*
334-
* @param {object|string} obj — representation of entity name.
335-
* @param {string} obj.block — the block name of entity.
336-
* @param {string} [obj.elem] — the element name of entity.
337-
* @param {object|string} [obj.mod] — the modifier of entity.
338-
* @param {string} [obj.val] - the modifier value of entity. Used if `obj.mod` is a string.
339-
* @param {string} obj.mod.name — the modifier name of entity.
340-
* @param {string} [obj.mod.val] — the modifier value of entity.
341-
* @param {string} [obj.modName] — the modifier name of entity. Used if `obj.mod.name` wasn't specified.
342-
* @param {string} [obj.modVal] — the modifier value of entity.
343-
* Used if neither `obj.mod.val` nor `obj.val` were not specified.
344-
*
345-
* @returns {BemEntityName} An object representing entity name.
346325
* @example
347326
* const BemEntityName = require('@bem/entity-name');
348327
*
349328
* BemEntityName.create({ block: 'my-button', mod: 'theme', val: 'red' });
350329
* BemEntityName.create({ block: 'my-button', modName: 'theme', modVal: 'red' });
351330
* // → BemEntityName { block: 'my-button', mod: { name: 'theme', val: 'red' } }
331+
*
332+
* @param {(BemSDK.EntityName.NonStrictRepresentation|string)} obj — representation of entity name.
333+
* @returns {BemEntityName} An object representing entity name.
352334
*/
353335
static create(obj) {
354336
if (BemEntityName.isBemEntityName(obj)) {
@@ -378,4 +360,11 @@ module.exports = class BemEntityName {
378360

379361
return new BemEntityName(data);
380362
}
381-
};
363+
}
364+
365+
module.exports = BemEntityName;
366+
367+
// TypeScript imports the `default` property for
368+
// an ES2015 default import (`import BemEntityName from '@bem/entity-name'`)
369+
// See: https://github.com/Microsoft/TypeScript/issues/2242#issuecomment-83694181
370+
module.exports.default = BemEntityName;

jsconfig.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES6",
4+
"module": "commonjs"
5+
},
6+
"files": [
7+
"index.js",
8+
"index.d.ts"
9+
]
10+
}

0 commit comments

Comments
 (0)