1
1
'use strict' ;
2
2
3
+ import { InitOptions } from './types' ;
4
+
3
5
/*
4
6
* Module dependencies.
5
7
*/
@@ -21,11 +23,9 @@ module.exports = Entity;
21
23
22
24
/**
23
25
* Initialize new `Entity` with `options`.
24
- *
25
- * @param {Object } options
26
26
*/
27
27
28
- function Entity ( options ) {
28
+ function Entity ( options : InitOptions ) {
29
29
this . options ( options ) ;
30
30
this . initialize ( ) ;
31
31
}
@@ -70,25 +70,18 @@ Entity.prototype.storage = function() {
70
70
71
71
/**
72
72
* Get or set storage `options`.
73
- *
74
- * @param {Object } options
75
- * @property {Object } cookie
76
- * @property {Object } localStorage
77
- * @property {Boolean } persist (default: `true`)
78
73
*/
79
74
80
- Entity . prototype . options = function ( options ) {
75
+ Entity . prototype . options = function ( options : InitOptions ) {
81
76
if ( arguments . length === 0 ) return this . _options ;
82
77
this . _options = defaults ( options || { } , this . defaults || { } ) ;
83
78
} ;
84
79
85
80
/**
86
81
* Get or set the entity's `id`.
87
- *
88
- * @param {String } id
89
82
*/
90
83
91
- Entity . prototype . id = function ( id ) {
84
+ Entity . prototype . id = function ( id : string ) : string | undefined {
92
85
switch ( arguments . length ) {
93
86
case 0 :
94
87
return this . _getId ( ) ;
@@ -101,11 +94,9 @@ Entity.prototype.id = function(id) {
101
94
102
95
/**
103
96
* Get the entity's id.
104
- *
105
- * @return {String }
106
97
*/
107
98
108
- Entity . prototype . _getId = function ( ) {
99
+ Entity . prototype . _getId = function ( ) : string | null {
109
100
if ( ! this . _options . persist ) {
110
101
return this . _id === undefined ? null : this . _id ;
111
102
}
@@ -129,21 +120,19 @@ Entity.prototype._getId = function() {
129
120
130
121
/**
131
122
* Get the entity's id from cookies.
132
- *
133
- * @return {String }
134
123
*/
135
124
136
- Entity . prototype . _getIdFromCookie = function ( ) {
125
+ // FIXME `options.cookie` is an optional field, so `this._options.cookie.key`
126
+ // can thrown an exception.
127
+ Entity . prototype . _getIdFromCookie = function ( ) : string {
137
128
return this . storage ( ) . get ( this . _options . cookie . key ) ;
138
129
} ;
139
130
140
131
/**
141
132
* Get the entity's id from cookies.
142
- *
143
- * @return {String }
144
133
*/
145
134
146
- Entity . prototype . _getIdFromLocalStorage = function ( ) {
135
+ Entity . prototype . _getIdFromLocalStorage = function ( ) : string | null {
147
136
if ( ! this . _options . localStorageFallbackDisabled ) {
148
137
return store . get ( this . _options . cookie . key ) ;
149
138
}
@@ -152,11 +141,9 @@ Entity.prototype._getIdFromLocalStorage = function() {
152
141
153
142
/**
154
143
* Set the entity's `id`.
155
- *
156
- * @param {String } id
157
144
*/
158
145
159
- Entity . prototype . _setId = function ( id ) {
146
+ Entity . prototype . _setId = function ( id : string ) {
160
147
if ( this . _options . persist ) {
161
148
this . _setIdInCookies ( id ) ;
162
149
this . _setIdInLocalStorage ( id ) ;
@@ -167,21 +154,17 @@ Entity.prototype._setId = function(id) {
167
154
168
155
/**
169
156
* Set the entity's `id` in cookies.
170
- *
171
- * @param {String } id
172
157
*/
173
158
174
- Entity . prototype . _setIdInCookies = function ( id ) {
159
+ Entity . prototype . _setIdInCookies = function ( id : string ) {
175
160
this . storage ( ) . set ( this . _options . cookie . key , id ) ;
176
161
} ;
177
162
178
163
/**
179
164
* Set the entity's `id` in local storage.
180
- *
181
- * @param {String } id
182
165
*/
183
166
184
- Entity . prototype . _setIdInLocalStorage = function ( id ) {
167
+ Entity . prototype . _setIdInLocalStorage = function ( id : string ) {
185
168
if ( ! this . _options . localStorageFallbackDisabled ) {
186
169
store . set ( this . _options . cookie . key , id ) ;
187
170
}
@@ -191,11 +174,11 @@ Entity.prototype._setIdInLocalStorage = function(id) {
191
174
* Get or set the entity's `traits`.
192
175
*
193
176
* BACKWARDS COMPATIBILITY: aliased to `properties`
194
- *
195
- * @param {Object } traits
196
177
*/
197
178
198
- Entity . prototype . properties = Entity . prototype . traits = function ( traits ) {
179
+ Entity . prototype . properties = Entity . prototype . traits = function (
180
+ traits : object
181
+ ) : object | undefined {
199
182
switch ( arguments . length ) {
200
183
case 0 :
201
184
return this . _getTraits ( ) ;
@@ -209,11 +192,9 @@ Entity.prototype.properties = Entity.prototype.traits = function(traits) {
209
192
/**
210
193
* Get the entity's traits. Always convert ISO date strings into real dates,
211
194
* since they aren't parsed back from local storage.
212
- *
213
- * @return {Object }
214
195
*/
215
196
216
- Entity . prototype . _getTraits = function ( ) {
197
+ Entity . prototype . _getTraits = function ( ) : object {
217
198
var ret = this . _options . persist
218
199
? store . get ( this . _options . localStorage . key )
219
200
: this . _traits ;
@@ -222,11 +203,9 @@ Entity.prototype._getTraits = function() {
222
203
223
204
/**
224
205
* Set the entity's `traits`.
225
- *
226
- * @param {Object } traits
227
206
*/
228
207
229
- Entity . prototype . _setTraits = function ( traits ) {
208
+ Entity . prototype . _setTraits = function ( traits : object ) {
230
209
traits = traits || { } ;
231
210
if ( this . _options . persist ) {
232
211
store . set ( this . _options . localStorage . key , traits ) ;
@@ -238,12 +217,9 @@ Entity.prototype._setTraits = function(traits) {
238
217
/**
239
218
* Identify the entity with an `id` and `traits`. If we it's the same entity,
240
219
* extend the existing `traits` instead of overwriting.
241
- *
242
- * @param {String } id
243
- * @param {Object } traits
244
220
*/
245
221
246
- Entity . prototype . identify = function ( id , traits ) {
222
+ Entity . prototype . identify = function ( id : string , traits : object ) {
247
223
traits = traits || { } ;
248
224
var current = this . id ( ) ;
249
225
if ( current === null || current === id )
@@ -256,11 +232,9 @@ Entity.prototype.identify = function(id, traits) {
256
232
257
233
/**
258
234
* Save the entity to local storage and the cookie.
259
- *
260
- * @return {Boolean }
261
235
*/
262
236
263
- Entity . prototype . save = function ( ) {
237
+ Entity . prototype . save = function ( ) : boolean {
264
238
if ( ! this . _options . persist ) return false ;
265
239
this . _setId ( this . id ( ) ) ;
266
240
this . _setTraits ( this . traits ( ) ) ;
0 commit comments