From bdb759c5585e20b7ca504653c10a6d6e47791e03 Mon Sep 17 00:00:00 2001 From: David Sevilla Martin Date: Sun, 21 Jun 2020 19:06:06 -0400 Subject: [PATCH 1/9] convert: common/utils/formatNumber --- js/src/common/utils/{formatNumber.js => formatNumber.ts} | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) rename js/src/common/utils/{formatNumber.js => formatNumber.ts} (70%) diff --git a/js/src/common/utils/formatNumber.js b/js/src/common/utils/formatNumber.ts similarity index 70% rename from js/src/common/utils/formatNumber.js rename to js/src/common/utils/formatNumber.ts index 939391029d..436a8131c1 100644 --- a/js/src/common/utils/formatNumber.js +++ b/js/src/common/utils/formatNumber.ts @@ -5,10 +5,7 @@ * @example * formatNumber(1234); * // 1,234 - * - * @param {Number} number - * @return {String} */ -export default function formatNumber(number) { +export default function formatNumber(number: number): string { return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','); } From 24a48310ffa18a638ca6385afed60cba4bba1f2f Mon Sep 17 00:00:00 2001 From: David Sevilla Martin Date: Sun, 21 Jun 2020 19:07:22 -0400 Subject: [PATCH 2/9] convert: common/utils/humanTime --- js/src/common/utils/{humanTime.js => humanTime.ts} | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) rename js/src/common/utils/{humanTime.js => humanTime.ts} (88%) diff --git a/js/src/common/utils/humanTime.js b/js/src/common/utils/humanTime.ts similarity index 88% rename from js/src/common/utils/humanTime.js rename to js/src/common/utils/humanTime.ts index e1529eb1e2..79216aa8a2 100644 --- a/js/src/common/utils/humanTime.js +++ b/js/src/common/utils/humanTime.ts @@ -1,11 +1,8 @@ /** * The `humanTime` utility converts a date to a localized, human-readable time- * ago string. - * - * @param {Date} time - * @return {String} */ -export default function humanTime(time) { +export default function humanTime(time: Date): string { let d = dayjs(time); const now = dayjs(); @@ -18,7 +15,7 @@ export default function humanTime(time) { const day = 864e5; const diff = d.diff(dayjs()); - let ago = null; + let ago: string; // If this date was more than a month ago, we'll show the name of the month // in the string. If it wasn't this year, we'll show the year as well. From 4869baea741baa006a3780827d152195c65fb0ee Mon Sep 17 00:00:00 2001 From: David Sevilla Martin Date: Sun, 21 Jun 2020 19:08:34 -0400 Subject: [PATCH 3/9] convert: common/utils/ItemList --- .../common/utils/{ItemList.js => ItemList.ts} | 72 ++++++------------- 1 file changed, 21 insertions(+), 51 deletions(-) rename js/src/common/utils/{ItemList.js => ItemList.ts} (67%) diff --git a/js/src/common/utils/ItemList.js b/js/src/common/utils/ItemList.ts similarity index 67% rename from js/src/common/utils/ItemList.js rename to js/src/common/utils/ItemList.ts index 880167b772..9cf51677eb 100644 --- a/js/src/common/utils/ItemList.js +++ b/js/src/common/utils/ItemList.ts @@ -1,5 +1,9 @@ class Item { - constructor(content, priority) { + content: any; + priority: number; + key?: number; + + constructor(content: any, priority?: number) { this.content = content; this.priority = priority; } @@ -10,23 +14,15 @@ class Item { * by priority. */ export default class ItemList { - constructor() { - /** - * The items in the list. - * - * @type {Object} - * @public - */ - this.items = {}; - } + /** + * The items in the list + */ + items: { [key: string]: Item } = {}; /** * Check whether the list is empty. - * - * @returns {boolean} - * @public */ - isEmpty() { + isEmpty(): boolean { for (const i in this.items) { if (this.items.hasOwnProperty(i)) { return false; @@ -38,36 +34,27 @@ export default class ItemList { /** * Check whether an item is present in the list. - * - * @param key - * @returns {boolean} */ - has(key) { + has(key: string): boolean { return !!this.items[key]; } /** * Get the content of an item. - * - * @param {String} key - * @return {*} - * @public */ - get(key) { + get(key: string): any { return this.items[key].content; } /** * Add an item to the list. * - * @param {String} key A unique key for the item. - * @param {*} content The item's content. - * @param {Integer} [priority] The priority of the item. Items with a higher + * @param key A unique key for the item. + * @param content The item's content. + * @param [priority] The priority of the item. Items with a higher * priority will be positioned before items with a lower priority. - * @return {ItemList} - * @public */ - add(key, content, priority = 0) { + add(key: string, content: any, priority: number = 0): this { this.items[key] = new Item(content, priority); return this; @@ -75,14 +62,8 @@ export default class ItemList { /** * Replace an item in the list, only if it is already present. - * - * @param {String} key - * @param {*} [content] - * @param {Integer} [priority] - * @return {ItemList} - * @public */ - replace(key, content = null, priority = null) { + replace(key: string, content: any = null, priority: number = null): this { if (this.items[key]) { if (content !== null) { this.items[key].content = content; @@ -98,12 +79,8 @@ export default class ItemList { /** * Remove an item from the list. - * - * @param {String} key - * @return {ItemList} - * @public */ - remove(key) { + remove(key: string): this { delete this.items[key]; return this; @@ -111,12 +88,8 @@ export default class ItemList { /** * Merge another list's items into this one. - * - * @param {ItemList} items - * @return {ItemList} - * @public */ - merge(items) { + merge(items: this): this { for (const i in items.items) { if (items.items.hasOwnProperty(i) && items.items[i] instanceof Item) { this.items[i] = items.items[i]; @@ -130,12 +103,9 @@ export default class ItemList { * Convert the list into an array of item content arranged by priority. Each * item's content will be assigned an `itemName` property equal to the item's * unique key. - * - * @return {Array} - * @public */ - toArray() { - const items = []; + toArray(): any[] { + const items: Item[] = []; for (const i in this.items) { if (this.items.hasOwnProperty(i) && this.items[i] instanceof Item) { From 286af7084b81e9775fbf08efc91efd3aacafa9de Mon Sep 17 00:00:00 2001 From: David Sevilla Martin Date: Sun, 21 Jun 2020 19:11:55 -0400 Subject: [PATCH 4/9] convert: common/utils/extract --- js/src/common/utils/extract.js | 15 --------------- js/src/common/utils/extract.ts | 15 +++++++++++++++ 2 files changed, 15 insertions(+), 15 deletions(-) delete mode 100644 js/src/common/utils/extract.js create mode 100644 js/src/common/utils/extract.ts diff --git a/js/src/common/utils/extract.js b/js/src/common/utils/extract.js deleted file mode 100644 index a6b7482107..0000000000 --- a/js/src/common/utils/extract.js +++ /dev/null @@ -1,15 +0,0 @@ -/** - * The `extract` utility deletes a property from an object and returns its - * value. - * - * @param {Object} object The object that owns the property - * @param {String} property The name of the property to extract - * @return {*} The value of the property - */ -export default function extract(object, property) { - const value = object[property]; - - delete object[property]; - - return value; -} diff --git a/js/src/common/utils/extract.ts b/js/src/common/utils/extract.ts new file mode 100644 index 0000000000..3e05da257e --- /dev/null +++ b/js/src/common/utils/extract.ts @@ -0,0 +1,15 @@ +/** + * The `extract` utility deletes a property from an object and returns its + * value. + * + * @param object The object that owns the property + * @param property The name of the property to extract + * @return The value of the property + */ +export default function extract(object: T, property: K): T[K] { + const value = object[property]; + + delete object[property]; + + return value; +} From dc738d68dc56c217a87386cfda32e64c4c84e5e0 Mon Sep 17 00:00:00 2001 From: David Sevilla Martin Date: Sun, 21 Jun 2020 19:12:22 -0400 Subject: [PATCH 5/9] convert: common/utils/abbreviateNumber --- .../utils/{abbreviateNumber.js => abbreviateNumber.ts} | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) rename js/src/common/utils/{abbreviateNumber.js => abbreviateNumber.ts} (82%) diff --git a/js/src/common/utils/abbreviateNumber.js b/js/src/common/utils/abbreviateNumber.ts similarity index 82% rename from js/src/common/utils/abbreviateNumber.js rename to js/src/common/utils/abbreviateNumber.ts index 360c6b82d1..4a691be968 100644 --- a/js/src/common/utils/abbreviateNumber.js +++ b/js/src/common/utils/abbreviateNumber.ts @@ -4,11 +4,8 @@ * @example * abbreviateNumber(1234); * // "1.2K" - * - * @param {Integer} number - * @return {String} */ -export default function abbreviateNumber(number) { +export default function abbreviateNumber(number: number): string { // TODO: translation if (number >= 1000000) { return Math.floor(number / 1000000) + app.translator.trans('core.lib.number_suffix.mega_text'); From 521cefbc2de416ec00bcbc90e5d783a41df653ba Mon Sep 17 00:00:00 2001 From: David Sevilla Martin Date: Tue, 23 Jun 2020 09:41:24 -0400 Subject: [PATCH 6/9] convert: common/utils/liveHumanTimes This file isn't used anywhere. We should be calling it at some point. It has existed for 5 years. Renamed function because it makes more sense for name to match file name (not that it matters when building) --- .../common/utils/{liveHumanTimes.js => liveHumanTimes.ts} | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) rename js/src/common/utils/{liveHumanTimes.js => liveHumanTimes.ts} (53%) diff --git a/js/src/common/utils/liveHumanTimes.js b/js/src/common/utils/liveHumanTimes.ts similarity index 53% rename from js/src/common/utils/liveHumanTimes.js rename to js/src/common/utils/liveHumanTimes.ts index 2eac7ef9dc..cea16fde59 100644 --- a/js/src/common/utils/liveHumanTimes.js +++ b/js/src/common/utils/liveHumanTimes.ts @@ -1,18 +1,18 @@ -import humanTimeUtil from './humanTime'; +import humanTime from './humanTime'; function updateHumanTimes() { $('[data-humantime]').each(function () { const $this = $(this); - const ago = humanTimeUtil($this.attr('datetime')); + const ago = humanTime($this.attr('datetime')); $this.html(ago); }); } /** - * The `humanTime` initializer sets up a loop every 1 second to update + * The `liveHumanTimes` initializer sets up a loop every 1 second to update * timestamps rendered with the `humanTime` helper. */ -export default function humanTime() { +export default function liveHumanTimes() { setInterval(updateHumanTimes, 10000); } From 4f34e326ef259d600accfbb44a7cd6cb2947bf68 Mon Sep 17 00:00:00 2001 From: David Sevilla Martin Date: Tue, 23 Jun 2020 10:00:47 -0400 Subject: [PATCH 7/9] convert: common/utils/RequestError --- .../common/utils/{RequestError.js => RequestError.ts} | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) rename js/src/common/utils/{RequestError.js => RequestError.ts} (55%) diff --git a/js/src/common/utils/RequestError.js b/js/src/common/utils/RequestError.ts similarity index 55% rename from js/src/common/utils/RequestError.js rename to js/src/common/utils/RequestError.ts index 3346e673f8..73e36f8549 100644 --- a/js/src/common/utils/RequestError.js +++ b/js/src/common/utils/RequestError.ts @@ -1,5 +1,14 @@ export default class RequestError { - constructor(status, responseText, options, xhr) { + status: string; + options: object; + xhr: XMLHttpRequest; + + responseText: string | null; + response: object | null; + + alert: any; + + constructor(status: string, responseText: string | null, options: object, xhr: XMLHttpRequest) { this.status = status; this.responseText = responseText; this.options = options; From 20baa93ca79c14a6ad063669636d5edefd96b6c0 Mon Sep 17 00:00:00 2001 From: David Sevilla Martin Date: Tue, 23 Jun 2020 10:02:59 -0400 Subject: [PATCH 8/9] convert: common/utils/string --- js/src/common/utils/{string.js => string.ts} | 22 ++++---------------- 1 file changed, 4 insertions(+), 18 deletions(-) rename js/src/common/utils/{string.js => string.ts} (75%) diff --git a/js/src/common/utils/string.js b/js/src/common/utils/string.ts similarity index 75% rename from js/src/common/utils/string.js rename to js/src/common/utils/string.ts index 73623d8a26..be7bc92d18 100644 --- a/js/src/common/utils/string.js +++ b/js/src/common/utils/string.ts @@ -1,12 +1,7 @@ /** * Truncate a string to the given length, appending ellipses if necessary. - * - * @param {String} string - * @param {Number} length - * @param {Number} [start=0] - * @return {String} */ -export function truncate(string, length, start = 0) { +export function truncate(string: string, length: number, start: number = 0): string { return (start > 0 ? '...' : '') + string.substring(start, start + length) + (string.length > start + length ? '...' : ''); } @@ -17,11 +12,8 @@ export function truncate(string, length, start = 0) { * NOTE: This method does not use the comparably sophisticated transliteration * mechanism that is employed in the backend. Therefore, it should only be used * to *suggest* slugs that can be overridden by the user. - * - * @param {String} string - * @return {String} */ -export function slug(string) { +export function slug(string: string): string { return string .toLowerCase() .replace(/[^a-z0-9]/gi, '-') @@ -32,11 +24,8 @@ export function slug(string) { /** * Strip HTML tags and quotes out of the given string, replacing them with * meaningful punctuation. - * - * @param {String} string - * @return {String} */ -export function getPlainContent(string) { +export function getPlainContent(string: string): string { const html = string.replace(/(<\/p>|
)/g, '$1  ').replace(/]*>/gi, ' '); const dom = $('
').html(html); @@ -55,10 +44,7 @@ getPlainContent.removeSelectors = ['blockquote', 'script']; /** * Make a string's first character uppercase. - * - * @param {String} string - * @return {String} */ -export function ucfirst(string) { +export function ucfirst(string: string): string { return string.substr(0, 1).toUpperCase() + string.substr(1); } From 455327cca1830c281932de474429badf2cc8b605 Mon Sep 17 00:00:00 2001 From: David Sevilla Martin Date: Tue, 23 Jun 2020 10:05:27 -0400 Subject: [PATCH 9/9] convert: common/utils/stringToColor --- .../common/utils/{stringToColor.js => stringToColor.ts} | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) rename js/src/common/utils/{stringToColor.js => stringToColor.ts} (87%) diff --git a/js/src/common/utils/stringToColor.js b/js/src/common/utils/stringToColor.ts similarity index 87% rename from js/src/common/utils/stringToColor.js rename to js/src/common/utils/stringToColor.ts index 2cb1788984..4edd87cd71 100644 --- a/js/src/common/utils/stringToColor.js +++ b/js/src/common/utils/stringToColor.ts @@ -1,4 +1,6 @@ -function hsvToRgb(h, s, v) { +type RGB = { r: number; g: number; b: number }; + +function hsvToRgb(h: number, s: number, v: number): RGB { let r; let g; let b; @@ -51,11 +53,8 @@ function hsvToRgb(h, s, v) { /** * Convert the given string to a unique color. - * - * @param {String} string - * @return {String} */ -export default function stringToColor(string) { +export default function stringToColor(string: string): string { let num = 0; // Convert the username into a number based on the ASCII value of each