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

Commit 2032118

Browse files
author
blond
committed
chore(package): add typing
1 parent 6734ab3 commit 2032118

File tree

5 files changed

+159
-4
lines changed

5 files changed

+159
-4
lines changed

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: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class EntityTypeError extends ExtendableError {
3333
}
3434
}
3535

36-
module.exports = class BemEntityName {
36+
class BemEntityName {
3737
/**
3838
* @param {object} obj — representation of entity name.
3939
* @param {string} obj.block — the block name of entity.
@@ -378,4 +378,11 @@ module.exports = class BemEntityName {
378378

379379
return new BemEntityName(data);
380380
}
381-
};
381+
}
382+
383+
module.exports = BemEntityName;
384+
385+
// TypeScript imports the `default` property for
386+
// an ES2015 default import (`import BemEntityName from '@bem/entity-name'`)
387+
// See: https://github.com/Microsoft/TypeScript/issues/2242#issuecomment-83694181
388+
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+
}

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
"equal"
2323
],
2424
"main": "index.js",
25+
"typings": "index.d.ts",
2526
"files": [
26-
"index.js"
27+
"index.js",
28+
"index.d.ts"
2729
],
2830
"engines": {
2931
"node": ">= 4.0"
@@ -33,13 +35,15 @@
3335
"es6-error": "4.0.2"
3436
},
3537
"devDependencies": {
38+
"@types/proxyquire": "^1.3.27",
39+
"@types/sinon": "^2.1.2",
3640
"ava": "^0.19.0",
3741
"coveralls": "^2.11.9",
3842
"eslint": "^3.0.0",
3943
"eslint-config-pedant": "^0.8.0",
4044
"nyc": "^10.0.0",
4145
"proxyquire": "^1.7.7",
42-
"sinon": "^2.0.0"
46+
"sinon": "^2.1.0"
4347
},
4448
"scripts": {
4549
"test": "npm run lint && npm run cover",

0 commit comments

Comments
 (0)