Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript Initial Conversion #2207

Merged
merged 9 commits into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 21 additions & 51 deletions js/src/common/utils/ItemList.js → js/src/common/utils/ItemList.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
class Item {
constructor(content, priority) {
content: any;
priority: number;
franzliedke marked this conversation as resolved.
Show resolved Hide resolved
key?: number;

constructor(content: any, priority?: number) {
this.content = content;
this.priority = priority;
}
Expand All @@ -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;
Expand All @@ -38,51 +34,36 @@ 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;
}

/**
* 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;
Expand All @@ -98,25 +79,17 @@ 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;
}

/**
* 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];
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
export default class RequestError {
constructor(status, responseText, options, xhr) {
status: string;
options: object;
xhr: XMLHttpRequest;

responseText: string | null;
franzliedke marked this conversation as resolved.
Show resolved Hide resolved
response: object | null;

alert: any;

constructor(status: string, responseText: string | null, options: object, xhr: XMLHttpRequest) {
this.status = status;
this.responseText = responseText;
this.options = options;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
15 changes: 0 additions & 15 deletions js/src/common/utils/extract.js

This file was deleted.

15 changes: 15 additions & 0 deletions js/src/common/utils/extract.ts
Original file line number Diff line number Diff line change
@@ -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<T, K extends keyof T>(object: T, property: K): T[K] {
franzliedke marked this conversation as resolved.
Show resolved Hide resolved
const value = object[property];

delete object[property];

return value;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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, ',');
}
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
22 changes: 4 additions & 18 deletions js/src/common/utils/string.js → js/src/common/utils/string.ts
Original file line number Diff line number Diff line change
@@ -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 ? '...' : '');
}

Expand All @@ -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, '-')
Expand All @@ -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>|<br>)/g, '$1 &nbsp;').replace(/<img\b[^>]*>/gi, ' ');

const dom = $('<div/>').html(html);
Expand All @@ -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);
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand Down