Skip to content
This repository was archived by the owner on Sep 3, 2022. It is now read-only.

Commit 679156e

Browse files
author
Julio Farah
authored
[TS] Move entity.js to entity.ts (#169)
1 parent 310509a commit 679156e

File tree

1 file changed

+20
-46
lines changed

1 file changed

+20
-46
lines changed

lib/entity.js renamed to lib/entity.ts

Lines changed: 20 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
import { InitOptions } from './types';
4+
35
/*
46
* Module dependencies.
57
*/
@@ -21,11 +23,9 @@ module.exports = Entity;
2123

2224
/**
2325
* Initialize new `Entity` with `options`.
24-
*
25-
* @param {Object} options
2626
*/
2727

28-
function Entity(options) {
28+
function Entity(options: InitOptions) {
2929
this.options(options);
3030
this.initialize();
3131
}
@@ -70,25 +70,18 @@ Entity.prototype.storage = function() {
7070

7171
/**
7272
* Get or set storage `options`.
73-
*
74-
* @param {Object} options
75-
* @property {Object} cookie
76-
* @property {Object} localStorage
77-
* @property {Boolean} persist (default: `true`)
7873
*/
7974

80-
Entity.prototype.options = function(options) {
75+
Entity.prototype.options = function(options: InitOptions) {
8176
if (arguments.length === 0) return this._options;
8277
this._options = defaults(options || {}, this.defaults || {});
8378
};
8479

8580
/**
8681
* Get or set the entity's `id`.
87-
*
88-
* @param {String} id
8982
*/
9083

91-
Entity.prototype.id = function(id) {
84+
Entity.prototype.id = function(id: string): string | undefined {
9285
switch (arguments.length) {
9386
case 0:
9487
return this._getId();
@@ -101,11 +94,9 @@ Entity.prototype.id = function(id) {
10194

10295
/**
10396
* Get the entity's id.
104-
*
105-
* @return {String}
10697
*/
10798

108-
Entity.prototype._getId = function() {
99+
Entity.prototype._getId = function(): string | null {
109100
if (!this._options.persist) {
110101
return this._id === undefined ? null : this._id;
111102
}
@@ -129,21 +120,19 @@ Entity.prototype._getId = function() {
129120

130121
/**
131122
* Get the entity's id from cookies.
132-
*
133-
* @return {String}
134123
*/
135124

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 {
137128
return this.storage().get(this._options.cookie.key);
138129
};
139130

140131
/**
141132
* Get the entity's id from cookies.
142-
*
143-
* @return {String}
144133
*/
145134

146-
Entity.prototype._getIdFromLocalStorage = function() {
135+
Entity.prototype._getIdFromLocalStorage = function(): string | null {
147136
if (!this._options.localStorageFallbackDisabled) {
148137
return store.get(this._options.cookie.key);
149138
}
@@ -152,11 +141,9 @@ Entity.prototype._getIdFromLocalStorage = function() {
152141

153142
/**
154143
* Set the entity's `id`.
155-
*
156-
* @param {String} id
157144
*/
158145

159-
Entity.prototype._setId = function(id) {
146+
Entity.prototype._setId = function(id: string) {
160147
if (this._options.persist) {
161148
this._setIdInCookies(id);
162149
this._setIdInLocalStorage(id);
@@ -167,21 +154,17 @@ Entity.prototype._setId = function(id) {
167154

168155
/**
169156
* Set the entity's `id` in cookies.
170-
*
171-
* @param {String} id
172157
*/
173158

174-
Entity.prototype._setIdInCookies = function(id) {
159+
Entity.prototype._setIdInCookies = function(id: string) {
175160
this.storage().set(this._options.cookie.key, id);
176161
};
177162

178163
/**
179164
* Set the entity's `id` in local storage.
180-
*
181-
* @param {String} id
182165
*/
183166

184-
Entity.prototype._setIdInLocalStorage = function(id) {
167+
Entity.prototype._setIdInLocalStorage = function(id: string) {
185168
if (!this._options.localStorageFallbackDisabled) {
186169
store.set(this._options.cookie.key, id);
187170
}
@@ -191,11 +174,11 @@ Entity.prototype._setIdInLocalStorage = function(id) {
191174
* Get or set the entity's `traits`.
192175
*
193176
* BACKWARDS COMPATIBILITY: aliased to `properties`
194-
*
195-
* @param {Object} traits
196177
*/
197178

198-
Entity.prototype.properties = Entity.prototype.traits = function(traits) {
179+
Entity.prototype.properties = Entity.prototype.traits = function(
180+
traits: object
181+
): object | undefined {
199182
switch (arguments.length) {
200183
case 0:
201184
return this._getTraits();
@@ -209,11 +192,9 @@ Entity.prototype.properties = Entity.prototype.traits = function(traits) {
209192
/**
210193
* Get the entity's traits. Always convert ISO date strings into real dates,
211194
* since they aren't parsed back from local storage.
212-
*
213-
* @return {Object}
214195
*/
215196

216-
Entity.prototype._getTraits = function() {
197+
Entity.prototype._getTraits = function(): object {
217198
var ret = this._options.persist
218199
? store.get(this._options.localStorage.key)
219200
: this._traits;
@@ -222,11 +203,9 @@ Entity.prototype._getTraits = function() {
222203

223204
/**
224205
* Set the entity's `traits`.
225-
*
226-
* @param {Object} traits
227206
*/
228207

229-
Entity.prototype._setTraits = function(traits) {
208+
Entity.prototype._setTraits = function(traits: object) {
230209
traits = traits || {};
231210
if (this._options.persist) {
232211
store.set(this._options.localStorage.key, traits);
@@ -238,12 +217,9 @@ Entity.prototype._setTraits = function(traits) {
238217
/**
239218
* Identify the entity with an `id` and `traits`. If we it's the same entity,
240219
* extend the existing `traits` instead of overwriting.
241-
*
242-
* @param {String} id
243-
* @param {Object} traits
244220
*/
245221

246-
Entity.prototype.identify = function(id, traits) {
222+
Entity.prototype.identify = function(id: string, traits: object) {
247223
traits = traits || {};
248224
var current = this.id();
249225
if (current === null || current === id)
@@ -256,11 +232,9 @@ Entity.prototype.identify = function(id, traits) {
256232

257233
/**
258234
* Save the entity to local storage and the cookie.
259-
*
260-
* @return {Boolean}
261235
*/
262236

263-
Entity.prototype.save = function() {
237+
Entity.prototype.save = function(): boolean {
264238
if (!this._options.persist) return false;
265239
this._setId(this.id());
266240
this._setTraits(this.traits());

0 commit comments

Comments
 (0)