Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | 11x 11x 11x 11x 11x 1085x 2103x 2453x 5x 2x 5x 340x 8x 4x 2x 323x 6x 4x 2x 801x 313x | import { TwitarrError } from '../api/TwitarrError';
import { Moment } from 'moment';
/** @hidden */
const moment = require('moment');
moment.fn.toJSON = () => {
return this.valueOf();
};
/** @hidden */
const dateFormat = 'YYYY-MM-DDTHH:mm:ss.SSSZZ';
/**
* A utility class for random stuff.
* @module Util
*/
export class Util {
/**
* Whether a string is empty.
* @param value
*/
public static isEmpty(...values: any[]) {
if (values.length === 0) {
return true;
}
for (const value of values) {
if (value !== undefined && value !== null) {
if (typeof value === 'string') {
if (value.trim().length > 0) {
return false;
}
} else {
return false;
}
}
}
return true;
}
public static assertHasProperties(obj?: any, ...expectedProperties: any[]) {
if (Util.isEmpty(obj)) {
throw new TwitarrError('Object is empty!');
}
if (expectedProperties.length === 0) {
throw new TwitarrError('At least one property must be specified!');
}
for (const prop of expectedProperties) {
if (Util.isEmpty(obj[prop])) {
throw new TwitarrError('Object property "' + prop + '" does not exist or is empty!');
}
}
return true;
}
/**
* Whether or not the passed object is already a date. (Either a [[Moment]] object, or
* a JavaScript [[Date]] object.)
*/
public static isDateObject(date: any) {
return moment.isMoment(date) || date instanceof Date;
}
/**
* Create a [[Moment]] from any form of date (JavaScript [[Date]], [[Moment]], or epoch).
* [[Moment]] dates in Twitarr.js will always be converted internally to UTC to avoid time
* zone issues.
*/
public static toMoment(date: Date | Moment | string | number): Moment {
if (date === undefined || date === null) {
return undefined;
} else if (moment.isMoment(date)) {
return (date as Moment).utc();
} else if (typeof date === 'number' || date instanceof Date || typeof date === 'string' || date instanceof String) {
return moment(date).utc();
} else {
throw new TwitarrError('Unable to parse type "' + typeof date + '" as a date.');
}
}
/**
* Create a date string from any form of date (JavaScript [[Date]], [[Moment]], or epoch).
* Dates in Twitarr.js will always be converted internally to UTC before stringifying to
* avoid time zone issues.
*/
public static toDateString(date: Date | Moment | number) {
const ret = Util.toMoment(date);
if (ret) {
return ret.utc().format(dateFormat);
} else {
return undefined;
}
}
/**
* Iterate over a set of (optional) properties on the source object, and apply them to the target.
*/
public static setProperties(target: any, source: any, ...props) {
if (!Util.isEmpty(source)) {
for (const prop of props) {
if (!Util.isEmpty(source[prop])) {
target[prop] = source[prop];
}
}
}
}
/**
* Iterate over a set of (optional) properties on the source object, and apply them to the target
* converting them to @Moment objects in the process.
*/
public static setDateProperties(target: any, source: any, ...props) {
if (!Util.isEmpty(source)) {
for (const prop of props) {
if (!Util.isEmpty(source[prop])) {
target[prop] = Util.toMoment(source[prop]);
}
}
}
}
}
|