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

Commit 0ba2093

Browse files
author
blond
committed
refactor(typings): add simple types
1 parent 9ac650f commit 0ba2093

File tree

3 files changed

+44
-60
lines changed

3 files changed

+44
-60
lines changed

globals.d.ts

Lines changed: 23 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,107 +3,91 @@ declare namespace BemSDK {
33
/**
44
* Types of BEM entities.
55
*/
6-
export type TYPE = 'block' | 'blockMod' | 'elem' | 'elemMod';
6+
export type Type = 'block' | 'blockMod' | 'elem' | 'elemMod';
7+
export type BlockName = string;
8+
export type ElementName = string;
9+
export type ModifierName = string;
10+
export type ModifierValue = string | true;
11+
export type Id = string;
712

813
/**
914
* Abstract object to represent entity name
1015
*/
11-
interface AbstractEntityRepresentation {
16+
interface AbstractRepresentation {
1217
/**
1318
* The block name of entity.
1419
*/
15-
block: string;
20+
block: BlockName;
1621
/**
1722
* The element name of entity.
1823
*/
19-
elem?: string;
24+
elem?: ElementName;
2025
mod?: any;
2126
}
2227

2328
/**
2429
* Object to represent modifier of entity name.
2530
*/
26-
export interface ModifierRepresentation {
31+
export interface Modifier {
2732
/**
2833
* The modifier name of entity.
2934
*/
30-
name: string;
35+
name: ModifierName;
3136
/**
3237
* The modifier value of entity.
3338
*/
34-
val: string | true;
39+
val: ModifierValue;
3540
}
3641

3742
/**
3843
* Strict object to represent entity name.
3944
*/
40-
export interface StrictRepresentation extends AbstractEntityRepresentation {
45+
export interface Representation extends AbstractRepresentation {
4146
/**
4247
* The modifier of entity.
4348
*/
44-
mod?: ModifierRepresentation;
49+
mod?: Modifier;
4550
}
4651

4752
/**
4853
* Object to create representation of entity name.
4954
*/
50-
export interface Options extends AbstractEntityRepresentation {
55+
export interface Options extends AbstractRepresentation {
5156
/**
5257
* The modifier of entity.
5358
*/
54-
mod?: string | {
59+
mod?: ModifierName | {
5560
/**
5661
* The modifier name of entity.
5762
*/
58-
name: string;
63+
name: ModifierName;
5964
/**
6065
* The modifier value of entity.
6166
*/
62-
val?: string | boolean;
67+
val?: ModifierValue;
6368
};
6469
/**
6570
* The modifier name of entity. Used if `mod.name` wasn't specified.
6671
* @deprecated use `mod.name` instead.
6772
*/
68-
modName?: string;
73+
modName?: ModifierName;
6974
/**
7075
* The modifier value of entity. Used if neither `mod.val` nor `val` were not specified.
7176
* @deprecated use `mod.name` instead.
7277
*/
73-
modVal?: string;
78+
modVal?: ModifierValue;
7479
}
7580

7681
/**
77-
* Non-strict object to represent entity name.
82+
* Object to create representation of entity name with `create` method.
7883
*
7984
* Contains old field: `val`, `modName` and `modVal.
8085
*/
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-
};
86+
export interface CreateOptions extends Options {
9587
/**
9688
* The modifier value of entity. Used if neither `mod.val` were not specified.
9789
*/
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;
90+
val?: ModifierValue;
10791
}
10892
}
10993
}

index.d.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@ import './globals.d';
33
declare class BemEntityName {
44
constructor(obj: BemSDK.EntityName.Options);
55

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 type: BemSDK.EntityName.TYPE;
6+
readonly block: BemSDK.EntityName.BlockName;
7+
readonly elem: BemSDK.EntityName.ElementName | undefined;
8+
readonly mod: BemSDK.EntityName.Modifier | undefined;
9+
readonly modName: BemSDK.EntityName.ModifierName | undefined;
10+
readonly modVal: BemSDK.EntityName.ModifierValue | undefined;
11+
readonly type: BemSDK.EntityName.Type;
1212
readonly scope: BemEntityName | null;
13-
readonly id: string;
13+
readonly id: BemSDK.EntityName.Id;
1414

1515
isSimpleMod(): boolean | null;
1616
isEqual(entityName: BemEntityName): boolean;
1717
belongsTo(entityName: BemEntityName): boolean;
18-
valueOf(): BemSDK.EntityName.StrictRepresentation;
19-
toJSON(): BemSDK.EntityName.StrictRepresentation;
18+
valueOf(): BemSDK.EntityName.Representation;
19+
toJSON(): BemSDK.EntityName.Representation;
2020
toString(): string;
2121
inspect(depth: number, options: object): string;
2222

23-
static create(obj: BemSDK.EntityName.NonStrictRepresentation | string): BemEntityName;
23+
static create(obj: BemSDK.EntityName.CreateOptions | string): BemEntityName;
2424
static isBemEntityName(entityName: any): boolean;
2525
}
2626

lib/entity-name.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class BemEntityName {
6161
*
6262
* name.block; // button
6363
*
64-
* @returns {string} name of entity block.
64+
* @returns {BemSDK.EntityName.BlockName} name of entity block.
6565
*/
6666
get block() { return this._data.block; }
6767

@@ -76,7 +76,7 @@ class BemEntityName {
7676
*
7777
* name.elem; // text
7878
*
79-
* @returns {?string} - name of entity element.
79+
* @returns {?BemSDK.EntityName.ElementName} - name of entity element.
8080
*/
8181
get elem() { return this._data.elem; }
8282

@@ -94,7 +94,7 @@ class BemEntityName {
9494
* modName.mod; // { name: 'disabled', val: true }
9595
* blockName.mod; // undefined
9696
*
97-
* @returns {?BemSDK.EntityName.ModifierRepresentation} - entity modifier.
97+
* @returns {?BemSDK.EntityName.Modifier} - entity modifier.
9898
*/
9999
get mod() { return this._data.mod; }
100100

@@ -103,7 +103,7 @@ class BemEntityName {
103103
*
104104
* If entity is not modifier then returns `undefined`.
105105
*
106-
* @returns {?string} - entity modifier name.
106+
* @returns {?BemSDK.EntityName.ModifierName} - entity modifier name.
107107
* @deprecated use {@link BemEntityName#mod.name}
108108
*/
109109
get modName() {
@@ -117,7 +117,7 @@ class BemEntityName {
117117
*
118118
* If entity is not modifier then returns `undefined`.
119119
*
120-
* @returns {?(string|true)} - entity modifier name.
120+
* @returns {?BemSDK.EntityName.ModifierValue} - entity modifier name.
121121
* @deprecated use {@link BemEntityName#mod.val}
122122
*/
123123
get modVal() {
@@ -141,7 +141,7 @@ class BemEntityName {
141141
*
142142
* name.type; // elemMod
143143
*
144-
* @returns {string} - type of entity. One of 'block', 'elem', 'blockMod', 'elemMod'.
144+
* @returns {BemSDK.EntityName.Type} - type of entity.
145145
*/
146146
get type() {
147147
if (this._type) { return this._type; }
@@ -172,7 +172,7 @@ class BemEntityName {
172172
* buttonTextName.scope; // BemEntityName { block: 'button' }
173173
* buttonTextBoldName.scope; // BemEntityName { block: 'button', elem: 'elem' }
174174
*
175-
* @returns {BemEntityName} - scope entity name.
175+
* @returns {(BemEntityName|null)} - scope entity name.
176176
*/
177177
get scope() {
178178
if (this.type === TYPES.BLOCK) { return null; }
@@ -200,7 +200,7 @@ class BemEntityName {
200200
*
201201
* name.id; // button_disabled
202202
*
203-
* @returns {string} - id of entity.
203+
* @returns {BemSDK.EntityName.Id} - id of entity.
204204
*/
205205
get id() {
206206
if (this._id) { return this._id; }
@@ -300,7 +300,7 @@ class BemEntityName {
300300
*
301301
* // ➜ { block: 'button', mod: { name: 'focused', value: true } }
302302
*
303-
* @returns {BemSDK.EntityName.StrictRepresentation}
303+
* @returns {BemSDK.EntityName.Representation}
304304
*/
305305
valueOf() { return this._data; }
306306

@@ -314,7 +314,7 @@ class BemEntityName {
314314
*
315315
* JSON.stringify(name); // {"block":"input","mod":{"name":"available","val":true}}
316316
*
317-
* @returns {BemSDK.EntityName.StrictRepresentation}
317+
* @returns {BemSDK.EntityName.Representation}
318318
*/
319319
toJSON() {
320320
return this._data;
@@ -373,7 +373,7 @@ class BemEntityName {
373373
* BemEntityName.create({ block: 'my-button', modName: 'theme', modVal: 'red' });
374374
* // → BemEntityName { block: 'my-button', mod: { name: 'theme', val: 'red' } }
375375
*
376-
* @param {(BemSDK.EntityName.NonStrictRepresentation|string)} obj — representation of entity name.
376+
* @param {(BemSDK.EntityName.CreateOptions|string)} obj — representation of entity name.
377377
* @returns {BemEntityName} An object representing entity name.
378378
*/
379379
static create(obj) {

0 commit comments

Comments
 (0)