Skip to content

Commit 92b3a4f

Browse files
committed
chore(ts): Convert src/sentry/static/sentry/app/utils.jsx to typescript
1 parent 2a491c8 commit 92b3a4f

File tree

2 files changed

+44
-40
lines changed

2 files changed

+44
-40
lines changed

src/sentry/static/sentry/app/types/index.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export type Project = {
1212
isMember: boolean;
1313
teams: Team[];
1414
features: string[];
15+
16+
isBookmarked: boolean;
1517
};
1618

1719
export type Team = {

src/sentry/static/sentry/app/utils.jsx renamed to src/sentry/static/sentry/app/utils.tsx

+42-40
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import _ from 'lodash';
2+
import {Project} from 'app/types/index';
23

3-
function arrayIsEqual(arr, other, deep) {
4+
function arrayIsEqual(arr?: any[], other?: any[], deep?: boolean): boolean {
45
// if the other array is a falsy value, return
56
if (!arr && !other) {
67
return true;
@@ -18,7 +19,7 @@ function arrayIsEqual(arr, other, deep) {
1819
return arr.every((val, idx) => valueIsEqual(val, other[idx], deep));
1920
}
2021

21-
export function valueIsEqual(value, other, deep) {
22+
export function valueIsEqual(value?: any, other?: any, deep?: boolean): boolean {
2223
if (value === other) {
2324
return true;
2425
} else if (_.isArray(value) || _.isArray(other)) {
@@ -33,8 +34,8 @@ export function valueIsEqual(value, other, deep) {
3334
return false;
3435
}
3536

36-
function objectMatchesSubset(obj, other, deep) {
37-
let k;
37+
function objectMatchesSubset(obj?: object, other?: object, deep?: boolean): boolean {
38+
let k: string;
3839

3940
if (obj === other) {
4041
return true;
@@ -61,21 +62,14 @@ function objectMatchesSubset(obj, other, deep) {
6162
return true;
6263
}
6364

64-
// XXX(dcramer): the previous mechanism of using _.map here failed
65-
// miserably if a param was named 'length'
66-
export function objectToArray(obj) {
67-
const result = [];
68-
for (const key in obj) {
69-
result.push([key, obj[key]]);
70-
}
71-
return result;
72-
}
65+
// TODO: instances of objectToArray should be refactored
66+
export const objectToArray = Object.entries;
7367

74-
export function intcomma(x) {
68+
export function intcomma(x: number): string {
7569
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
7670
}
7771

78-
export function sortArray(arr, score_fn) {
72+
export function sortArray<T>(arr: Array<T>, score_fn: (entry: T) => string): Array<T> {
7973
arr.sort((a, b) => {
8074
const a_score = score_fn(a),
8175
b_score = score_fn(b);
@@ -94,7 +88,7 @@ export function sortArray(arr, score_fn) {
9488
return arr;
9589
}
9690

97-
export function objectIsEmpty(obj) {
91+
export function objectIsEmpty(obj: object): boolean {
9892
for (const prop in obj) {
9993
if (obj.hasOwnProperty(prop)) {
10094
return false;
@@ -104,55 +98,55 @@ export function objectIsEmpty(obj) {
10498
return true;
10599
}
106100

107-
export function trim(str) {
101+
export function trim(str: string): string {
108102
return str.replace(/^\s+|\s+$/g, '');
109103
}
110104

111105
/**
112106
* Replaces slug special chars with a space
113107
*/
114-
export function explodeSlug(slug) {
108+
export function explodeSlug(slug: string): string {
115109
return trim(slug.replace(/[-_]+/g, ' '));
116110
}
117111

118-
export function defined(item) {
112+
export function defined(item: any): boolean {
119113
return !_.isUndefined(item) && item !== null;
120114
}
121115

122-
export function nl2br(str) {
116+
export function nl2br(str: string): string {
123117
return str.replace(/(?:\r\n|\r|\n)/g, '<br />');
124118
}
125119

126120
/**
127121
* This function has a critical security impact, make sure to check all usages before changing this function.
128122
* In some parts of our code we rely on that this only really is a string starting with http(s).
129123
*/
130-
export function isUrl(str) {
124+
export function isUrl(str: any): boolean {
131125
return (
132126
!!str &&
133127
_.isString(str) &&
134128
(str.indexOf('http://') === 0 || str.indexOf('https://') === 0)
135129
);
136130
}
137131

138-
export function escape(str) {
132+
export function escape(str: string): string {
139133
return str
140134
.replace(/&/g, '&amp;')
141135
.replace(/</g, '&lt;')
142136
.replace(/>/g, '&gt;');
143137
}
144138

145-
export function percent(value, totalValue, precise) {
139+
export function percent(value: number, totalValue: number): number {
146140
return (value / totalValue) * 100;
147141
}
148142

149-
export function toTitleCase(str) {
143+
export function toTitleCase(str: string): string {
150144
return str.replace(/\w\S*/g, txt => {
151145
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
152146
});
153147
}
154148

155-
export function formatBytes(bytes) {
149+
export function formatBytes(bytes: number): string {
156150
const units = ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
157151
const thresh = 1024;
158152
if (bytes < thresh) {
@@ -167,7 +161,7 @@ export function formatBytes(bytes) {
167161
return bytes.toFixed(1) + ' ' + units[u];
168162
}
169163

170-
export function getShortVersion(version) {
164+
export function getShortVersion(version: string): string {
171165
if (version.length < 12) {
172166
return version;
173167
}
@@ -184,37 +178,37 @@ export function getShortVersion(version) {
184178
return version;
185179
}
186180

187-
export function parseRepo(repo) {
188-
if (!repo) {
189-
return repo;
190-
} else {
181+
export function parseRepo<T>(repo: T): T {
182+
if (typeof repo === 'string') {
191183
const re = /(?:github\.com|bitbucket\.org)\/([^\/]+\/[^\/]+)/i;
192184
const match = repo.match(re);
193185
const parsedRepo = match ? match[1] : repo;
194-
return parsedRepo;
186+
return parsedRepo as any;
195187
}
188+
189+
return repo;
196190
}
197191

198192
/**
199193
* Converts a multi-line textarea input value into an array,
200194
* eliminating empty lines
201195
*/
202-
export function extractMultilineFields(value) {
196+
export function extractMultilineFields(value: string): Array<string> {
203197
return value
204198
.split('\n')
205199
.map(f => trim(f))
206200
.filter(f => f !== '');
207201
}
208202

209-
function projectDisplayCompare(a, b) {
203+
function projectDisplayCompare(a: Project, b: Project): number {
210204
if (a.isBookmarked !== b.isBookmarked) {
211205
return a.isBookmarked ? -1 : 1;
212206
}
213207
return a.slug.localeCompare(b.slug);
214208
}
215209

216210
// Sort a list of projects by bookmarkedness, then by id
217-
export function sortProjects(projects) {
211+
export function sortProjects(projects: Array<Project>): Array<Project> {
218212
return projects.sort(projectDisplayCompare);
219213
}
220214

@@ -225,21 +219,29 @@ export const buildTeamId = id => `team:${id}`;
225219
/**
226220
* Removes the organization / project scope prefix on feature names.
227221
*/
228-
export function descopeFeatureName(feature) {
229-
return typeof feature.match !== 'function'
230-
? feature
231-
: feature.match(/(?:^(?:projects|organizations):)?(.*)/).pop();
222+
export function descopeFeatureName<T>(feature: T): T | string {
223+
if (typeof feature !== 'string') {
224+
return feature;
225+
}
226+
227+
const results = feature.match(/(?:^(?:projects|organizations):)?(.*)/);
228+
229+
if (results && results.length > 0) {
230+
return results.pop()!;
231+
}
232+
233+
return feature;
232234
}
233235

234-
export function isWebpackChunkLoadingError(error) {
236+
export function isWebpackChunkLoadingError(error: Error): boolean {
235237
return (
236238
error &&
237239
typeof error.message === 'string' &&
238240
error.message.toLowerCase().includes('loading chunk')
239241
);
240242
}
241243

242-
export function deepFreeze(object) {
244+
export function deepFreeze(object: {[x: string]: any}) {
243245
// Retrieve the property names defined on object
244246
const propNames = Object.getOwnPropertyNames(object);
245247
// Freeze properties before freezing self

0 commit comments

Comments
 (0)