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

Commit 344b9bc

Browse files
author
Julio Farah
authored
Convert group, memory, metrics, normalize, pageDefaults and store to .ts (#171)
* [TS] Move group and memory to .ts * migrate metrics * migrate normalize * migrate pageDefaults and store * update PR template
1 parent 679156e commit 344b9bc

File tree

8 files changed

+57
-65
lines changed

8 files changed

+57
-65
lines changed

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@ For change control purposes, use the following pattern:
2020
<!-- Update the HISTORY.md file if we need to generate a new version of AJS for your changes.
2121
2222
If not, just add the following line with an explanation:
23-
Release not necessary because <verbose explantaion - for example 'it's a dev-only change'>.
23+
New version is not required because <verbose explantaion - for example 'it's a dev-only change'>.
2424
-->
2525

2626
## Checklist
2727

2828
<!--
2929
Make sure you complete the following checklist to help get your PR merged:-->
3030

31-
3231
- [ ] Thorough explanation of the issue/solution, and a link to the related issue
3332
- [ ] CI tests are passing
3433
- [ ] Unit tests were written for any new code

lib/group.js renamed to lib/group.ts

Lines changed: 3 additions & 3 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
*/
@@ -25,11 +27,9 @@ Group.defaults = {
2527

2628
/**
2729
* Initialize a new `Group` with `options`.
28-
*
29-
* @param {Object} options
3030
*/
3131

32-
function Group(options) {
32+
function Group(options?: InitOptions) {
3333
this.defaults = Group.defaults;
3434
this.debug = debug;
3535
Entity.call(this, options);

lib/memory.js renamed to lib/memory.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,36 +29,27 @@ function Memory() {
2929

3030
/**
3131
* Set a `key` and `value`.
32-
*
33-
* @param {String} key
34-
* @param {Mixed} value
35-
* @return {Boolean}
3632
*/
3733

38-
Memory.prototype.set = function(key, value) {
34+
Memory.prototype.set = function(key: string, value: unknown): boolean {
3935
this.store[key] = clone(value);
4036
return true;
4137
};
4238

4339
/**
4440
* Get a `key`.
45-
*
46-
* @param {String} key
4741
*/
4842

49-
Memory.prototype.get = function(key) {
43+
Memory.prototype.get = function(key: string): unknown | undefined {
5044
if (!has.call(this.store, key)) return;
5145
return clone(this.store[key]);
5246
};
5347

5448
/**
5549
* Remove a `key`.
56-
*
57-
* @param {String} key
58-
* @return {Boolean}
5950
*/
6051

61-
Memory.prototype.remove = function(key) {
52+
Memory.prototype.remove = function(key: string): boolean {
6253
delete this.store[key];
6354
return true;
6455
};

lib/metrics.js renamed to lib/metrics.ts

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

3+
import { MetricsOptions } from './types';
4+
35
var bindAll = require('bind-all');
46
var send = require('@segment/send-json');
57
var debug = require('debug')('analytics.js:metrics');
68

7-
function Metrics(options) {
9+
function Metrics(options?: MetricsOptions) {
810
this.options(options);
911
}
1012

1113
/**
1214
* Set the metrics options.
13-
*
14-
* @param {Object} options
15-
* @field {String} host
16-
* @field {Number} sampleRate
17-
* @field {Number} flushTimer
1815
*/
1916

20-
Metrics.prototype.options = function(options) {
17+
Metrics.prototype.options = function(options: MetricsOptions) {
2118
options = options || {};
2219

2320
this.host = options.host || 'api.segment.io/v1';
@@ -37,11 +34,8 @@ Metrics.prototype.options = function(options) {
3734

3835
/**
3936
* Increments the counter identified by name and tags by one.
40-
*
41-
* @param {String} metric Name of the metric to increment.
42-
* @param {Object} tags Dimensions associated with the metric.
4337
*/
44-
Metrics.prototype.increment = function(metric, tags) {
38+
Metrics.prototype.increment = function(metric: string, tags: object) {
4539
if (Math.random() > this.sampleRate) {
4640
return;
4741
}

lib/normalize.js renamed to lib/normalize.ts

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

3+
import { Message } from './types';
4+
35
/**
46
* Module Dependencies.
57
*/
@@ -33,33 +35,39 @@ var toplevel = ['integrations', 'anonymousId', 'timestamp', 'context'];
3335

3436
/**
3537
* Normalize `msg` based on integrations `list`.
36-
*
37-
* @param {Object} msg
38-
* @param {Array} list
39-
* @return {Function}
4038
*/
4139

42-
function normalize(msg, list) {
40+
interface NormalizedMessage {
41+
integrations?: {
42+
[key: string]: string;
43+
};
44+
context?: unknown;
45+
}
46+
47+
function normalize(msg: Message, list: Array<any>): NormalizedMessage {
4348
var lower = map(function(s) {
4449
return s.toLowerCase();
4550
}, list);
46-
var opts = msg.options || {};
51+
var opts: Message = msg.options || {};
4752
var integrations = opts.integrations || {};
4853
var providers = opts.providers || {};
4954
var context = opts.context || {};
50-
var ret = {};
55+
var ret: {
56+
integrations?: { [key: string]: string };
57+
context?: unknown;
58+
} = {};
5159
debug('<-', msg);
5260

5361
// integrations.
54-
each(function(value, key) {
62+
each(function(value: string, key: string) {
5563
if (!integration(key)) return;
5664
if (!has.call(integrations, key)) integrations[key] = value;
5765
delete opts[key];
5866
}, opts);
5967

6068
// providers.
6169
delete opts.providers;
62-
each(function(value, key) {
70+
each(function(value: string, key: string) {
6371
if (!integration(key)) return;
6472
if (type(integrations[key]) === 'object') return;
6573
if (has.call(integrations, key) && typeof providers[key] === 'boolean')
@@ -69,7 +77,7 @@ function normalize(msg, list) {
6977

7078
// move all toplevel options to msg
7179
// and the rest to context.
72-
each(function(value, key) {
80+
each(function(_value: any, key: string | number) {
7381
if (includes(key, toplevel)) {
7482
ret[key] = opts[key];
7583
} else {
@@ -88,7 +96,7 @@ function normalize(msg, list) {
8896
debug('->', ret);
8997
return ret;
9098

91-
function integration(name) {
99+
function integration(name: string) {
92100
return !!(
93101
includes(name, list) ||
94102
name.toLowerCase() === 'all' ||

lib/pageDefaults.js renamed to lib/pageDefaults.ts

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

3+
import { PageDefaults } from './types';
4+
35
/*
46
* Module dependencies.
57
*/
@@ -12,11 +14,9 @@ var url = require('component-url');
1214
* Return a default `options.context.page` object.
1315
*
1416
* https://segment.com/docs/spec/page/#properties
15-
*
16-
* @return {Object}
1717
*/
1818

19-
function pageDefaults() {
19+
function pageDefaults(): PageDefaults {
2020
return {
2121
path: canonicalPath(),
2222
referrer: document.referrer,
@@ -28,11 +28,9 @@ function pageDefaults() {
2828

2929
/**
3030
* Return the canonical path for the page.
31-
*
32-
* @return {string}
3331
*/
3432

35-
function canonicalPath() {
33+
function canonicalPath(): string {
3634
var canon = canonical();
3735
if (!canon) return window.location.pathname;
3836
var parsed = url.parse(canon);
@@ -42,12 +40,9 @@ function canonicalPath() {
4240
/**
4341
* Return the canonical URL for the page concat the given `search`
4442
* and strip the hash.
45-
*
46-
* @param {string} search
47-
* @return {string}
4843
*/
4944

50-
function canonicalUrl(search) {
45+
function canonicalUrl(search: string): string {
5146
var canon = canonical();
5247
if (canon) return includes('?', canon) ? canon : canon + search;
5348
var url = window.location.href;

lib/store.js renamed to lib/store.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,15 @@ var store = require('@segment/store');
1414
* @param {Object} options
1515
*/
1616

17-
function Store(options) {
17+
function Store(options?: { enabled: boolean }) {
1818
this.options(options);
1919
}
2020

2121
/**
2222
* Set the `options` for the store.
23-
*
24-
* @param {Object} options
25-
* @field {Boolean} enabled (true)
2623
*/
2724

28-
Store.prototype.options = function(options) {
25+
Store.prototype.options = function(options: { enabled?: boolean }) {
2926
if (arguments.length === 0) return this._options;
3027

3128
options = options || {};
@@ -37,35 +34,27 @@ Store.prototype.options = function(options) {
3734

3835
/**
3936
* Set a `key` and `value` in local storage.
40-
*
41-
* @param {string} key
42-
* @param {Object} value
4337
*/
4438

45-
Store.prototype.set = function(key, value) {
39+
Store.prototype.set = function(key: string, value: object) {
4640
if (!this.enabled) return false;
4741
return store.set(key, value);
4842
};
4943

5044
/**
5145
* Get a value from local storage by `key`.
52-
*
53-
* @param {string} key
54-
* @return {Object}
5546
*/
5647

57-
Store.prototype.get = function(key) {
48+
Store.prototype.get = function(key: string): object {
5849
if (!this.enabled) return null;
5950
return store.get(key);
6051
};
6152

6253
/**
6354
* Remove a value from local storage by `key`.
64-
*
65-
* @param {string} key
6655
*/
6756

68-
Store.prototype.remove = function(key) {
57+
Store.prototype.remove = function(key: string) {
6958
if (!this.enabled) return false;
7059
return store.remove(key);
7160
};

lib/types.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,19 @@ export interface SegmentOpts {
7373
anonymousId?: string;
7474
context?: object;
7575
}
76+
77+
export interface Message {
78+
options?: unknown;
79+
integrations?: { [key: string]: string };
80+
providers?: { [key: string]: string };
81+
context?: unknown;
82+
messageId?: string;
83+
}
84+
85+
export interface PageDefaults {
86+
path: string;
87+
referrer: string;
88+
search: string;
89+
title: string;
90+
url: string;
91+
}

0 commit comments

Comments
 (0)