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

[Expression] Remove dependency of Moment type in public methods #2232

Merged
merged 18 commits into from
Jul 3, 2020
Merged
Changes from 14 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
63 changes: 33 additions & 30 deletions libraries/adaptive-expressions/src/expressionFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ export class ExpressionFunctions {
* @param func Transformer.
* @returns Delegate for evaluating expression.
*/
public static timeTransform(type: string, func: (timestamp: Moment, numOfTransformation: any) => any): ExpressionEvaluator {
public static timeTransform(type: string, func: (timestamp: Date, numOfTransformation: any) => Date): ExpressionEvaluator {
return new ExpressionEvaluator(
type,
(expression: Expression, state: MemoryInterface, options: Options): {value: any; error: string} => {
Expand All @@ -678,7 +678,7 @@ export class ExpressionFunctions {
({value, error} = ExpressionFunctions.parseTimestamp(args[0]));
if (!error) {
if (args.length === 3 && typeof args[2] === 'string') {
result = func(value, args[1]).format(ExpressionFunctions.timestampFormatter(args[2]));
result = moment(func(value, args[1])).utc().format(ExpressionFunctions.timestampFormatter(args[2]));
} else {
result = func(value, args[1]).toISOString();
}
Expand Down Expand Up @@ -774,12 +774,11 @@ export class ExpressionFunctions {
return {value, error};
}

private static parseTimestamp(timeStamp: string, transform?: (arg0: Moment) => any): {value: any; error: string} {
private static parseTimestamp(timeStamp: string, transform?: (arg0: Date) => any): {value: any; error: string} {
let value: any;
const error: string = this.verifyISOTimestamp(timeStamp);
if (!error) {
const parsed: Moment = moment(timeStamp).utc();
value = transform !== undefined ? transform(parsed) : parsed;
value = transform !== undefined ? transform(new Date(timeStamp)) : timeStamp;
}

return {value, error};
Expand Down Expand Up @@ -1569,7 +1568,8 @@ export class ExpressionFunctions {
let parsed: any;
({value: parsed, error} = ExpressionFunctions.parseTimestamp(timeStamp));
if (!error) {
let addedTime: Moment = parsed;
let dt: any = moment(parsed).utc();
let addedTime = dt;
let timeUnitMark: string;
switch (timeUnit) {
case 'Second': {
Expand Down Expand Up @@ -1614,7 +1614,7 @@ export class ExpressionFunctions {
}

if (!error) {
addedTime = parsed.add(interval, timeUnitMark);
addedTime = dt.add(interval, timeUnitMark);
({value: result, error} = this.returnFormattedTimeStampStr(addedTime, format));
}
}
Expand Down Expand Up @@ -1678,7 +1678,7 @@ export class ExpressionFunctions {
error = this.verifyTimeStamp(timeStamp);
if (!error) {
try {
const sourceTime: Moment = tz(timeStamp, timeZone);
const sourceTime = tz(timeStamp, timeZone);
formattedSourceTime = sourceTime.format();
} catch (e) {
error = `${timeStamp} with ${timeZone} is not a valid timestamp with specified timeZone:`;
Expand All @@ -1703,7 +1703,7 @@ export class ExpressionFunctions {
let error: string;
({value: parsed, error} = ExpressionFunctions.parseTimestamp(timeStamp));
if (!error) {
const unixMilliSec: number = parseInt(parsed.format('x'), 10);
const unixMilliSec: number = parseInt(moment(parsed).utc().format('x'), 10);
result = this.UnixMilliSecondToTicksConstant.add(bigInt(unixMilliSec).times(this.MillisecondToTickConstant));
}

Expand All @@ -1713,10 +1713,10 @@ export class ExpressionFunctions {
private static startOfDay(timeStamp: string, format?: string): {value: any; error: string} {
let result: string;
let error: string;
let parsed: Moment;
let parsed: any;
({value: parsed, error} = ExpressionFunctions.parseTimestamp(timeStamp));
if (!error) {
const startOfDay: Moment = parsed.hours(0).minutes(0).second(0).millisecond(0);
const startOfDay = moment(parsed).utc().hours(0).minutes(0).second(0).millisecond(0);
({value: result, error} = ExpressionFunctions.returnFormattedTimeStampStr(startOfDay, format));
}

Expand All @@ -1726,10 +1726,10 @@ export class ExpressionFunctions {
private static startOfHour(timeStamp: string, format?: string): {value: any; error: string} {
let result: string;
let error: string;
let parsed: Moment;
let parsed: any;
({value: parsed, error} = ExpressionFunctions.parseTimestamp(timeStamp));
if (!error) {
const startofHour: Moment = parsed.minutes(0).second(0).millisecond(0);
const startofHour = moment(parsed).utc().minutes(0).second(0).millisecond(0);
({value: result, error} = ExpressionFunctions.returnFormattedTimeStampStr(startofHour, format));
}

Expand All @@ -1739,10 +1739,10 @@ export class ExpressionFunctions {
private static startOfMonth(timeStamp: string, format?: string): {value: any; error: string} {
let result: string;
let error: string;
let parsed: Moment;
let parsed: any;
({value: parsed, error} = ExpressionFunctions.parseTimestamp(timeStamp));
if (!error) {
const startofMonth: Moment = parsed.date(1).hours(0).minutes(0).second(0).millisecond(0);
const startofMonth = moment(parsed).utc().date(1).hours(0).minutes(0).second(0).millisecond(0);
({value: result, error} = ExpressionFunctions.returnFormattedTimeStampStr(startofMonth, format));
}

Expand Down Expand Up @@ -2429,56 +2429,56 @@ export class ExpressionFunctions {
ReturnType.String,
(expression: Expression): void => ExpressionFunctions.validateOrder(expression, [ReturnType.String], ReturnType.Array, ReturnType.String)),
// datetime
ExpressionFunctions.timeTransform(ExpressionType.AddDays, (ts: Moment, num: any): any => ts.add(num, 'd')),
ExpressionFunctions.timeTransform(ExpressionType.AddHours, (ts: Moment, num: any): any => ts.add(num, 'h')),
ExpressionFunctions.timeTransform(ExpressionType.AddMinutes, (ts: Moment, num: any): any => ts.add(num, 'minutes')),
ExpressionFunctions.timeTransform(ExpressionType.AddSeconds, (ts: Moment, num: any): any => ts.add(num, 'seconds')),
ExpressionFunctions.timeTransform(ExpressionType.AddDays, (ts: Date, num: any): Date => moment(ts).utc().add(num, 'd').toDate()),
ExpressionFunctions.timeTransform(ExpressionType.AddHours, (ts: Date, num: any): Date => moment(ts).utc().add(num, 'h').toDate()),
ExpressionFunctions.timeTransform(ExpressionType.AddMinutes, (ts: Date, num: any): Date => moment(ts).utc().add(num, 'minutes').toDate()),
ExpressionFunctions.timeTransform(ExpressionType.AddSeconds, (ts: Date, num: any): Date => moment(ts).utc().add(num, 'seconds').toDate()),
new ExpressionEvaluator(
ExpressionType.DayOfMonth,
ExpressionFunctions.applyWithError(
(args: any[]): any => ExpressionFunctions.parseTimestamp(args[0], (dt: Moment): number => dt.date()),
(args: any[]): any => ExpressionFunctions.parseTimestamp(args[0], (timestamp: Date): number => timestamp.getUTCDate()),
ExpressionFunctions.verifyString),
ReturnType.Number,
ExpressionFunctions.validateUnaryString),
new ExpressionEvaluator(
ExpressionType.DayOfWeek,
ExpressionFunctions.applyWithError(
(args: any[]): any => ExpressionFunctions.parseTimestamp(args[0], (dt: Moment): number => dt.days()),
(args: any[]): any => ExpressionFunctions.parseTimestamp(args[0], (timestamp: Date): number => timestamp.getUTCDay()),
ExpressionFunctions.verifyString),
ReturnType.Number,
ExpressionFunctions.validateUnaryString),
new ExpressionEvaluator(
ExpressionType.DayOfYear,
ExpressionFunctions.applyWithError(
(args: any[]): any => ExpressionFunctions.parseTimestamp(args[0], (dt: Moment): number => dt.dayOfYear()),
(args: any[]): any => ExpressionFunctions.parseTimestamp(args[0], (timestamp: Date): number => moment(timestamp).utc().dayOfYear()),
ExpressionFunctions.verifyString),
ReturnType.Number,
ExpressionFunctions.validateUnaryString),
new ExpressionEvaluator(
ExpressionType.Month,
ExpressionFunctions.applyWithError(
(args: any[]): any => ExpressionFunctions.parseTimestamp(args[0], (dt: Moment): number => dt.month() + 1),
(args: any[]): any => ExpressionFunctions.parseTimestamp(args[0], (timestamp: Date): number => timestamp.getUTCMonth() + 1),
ExpressionFunctions.verifyString),
ReturnType.Number,
ExpressionFunctions.validateUnaryString),
new ExpressionEvaluator(
ExpressionType.Date,
ExpressionFunctions.applyWithError(
(args: any[]): any => ExpressionFunctions.parseTimestamp(args[0], (dt: Moment): string => dt.format('M/DD/YYYY')),
(args: any[]): any => ExpressionFunctions.parseTimestamp(args[0], (timestamp: Date): string => moment(timestamp).utc().format('M/DD/YYYY')),
ExpressionFunctions.verifyString),
ReturnType.String,
ExpressionFunctions.validateUnaryString),
new ExpressionEvaluator(
ExpressionType.Year,
ExpressionFunctions.applyWithError(
(args: any[]): any => ExpressionFunctions.parseTimestamp(args[0], (dt: Moment): number => dt.year()),
(args: any[]): any => ExpressionFunctions.parseTimestamp(args[0], (timestamp: Date): number =>timestamp.getUTCFullYear()),
ExpressionFunctions.verifyString),
ReturnType.Number,
ExpressionFunctions.validateUnaryString),
new ExpressionEvaluator(
ExpressionType.UtcNow,
ExpressionFunctions.apply(
(args: any[]): string => args.length === 1 ? moment(new Date().toISOString()).utc().format(args[0]) : new Date().toISOString(),
(args: any[]): string => args.length === 1 ? moment(new Date()).utc().format(args[0]) : new Date().toISOString(),
ExpressionFunctions.verifyString),
ReturnType.String),
new ExpressionEvaluator(
Expand Down Expand Up @@ -2569,8 +2569,9 @@ export class ExpressionFunctions {
error = `${args[2]} is not a valid time unit.`;
} else {
const dur: any = duration;
({value, error} = ExpressionFunctions.parseTimestamp(args[0], (dt: Moment): string => args.length === 4 ?
dt.subtract(dur, tsStr).format(format) : dt.subtract(dur, tsStr).toISOString()));
({value, error} = ExpressionFunctions.parseTimestamp(args[0], (dt: Date): string => {
return args.length === 4 ?
moment(dt).utc().subtract(dur, tsStr).format(format) : moment(dt).utc().subtract(dur, tsStr).toISOString()}));
}
} else {
error = `${expr} can't evaluate.`;
Expand Down Expand Up @@ -2644,7 +2645,8 @@ export class ExpressionFunctions {
error = `${args[2]} is not a valid time unit.`;
} else {
const dur: any = duration;
({value, error} = ExpressionFunctions.parseTimestamp(new Date().toISOString(), (dt: Moment): string => dt.add(dur, tsStr).format(format)));
({value, error} = ExpressionFunctions.parseTimestamp(new Date().toISOString(), (dt: Date): string => {
return moment(dt).utc().add(dur, tsStr).format(format)}));
}
} else {
error = `${expr} can't evaluate.`;
Expand All @@ -2671,7 +2673,8 @@ export class ExpressionFunctions {
error = `${args[2]} is not a valid time unit.`;
} else {
const dur: any = duration;
({value, error} = ExpressionFunctions.parseTimestamp(new Date().toISOString(), (dt: Moment): string => dt.subtract(dur, tsStr).format(format)));
({value, error} = ExpressionFunctions.parseTimestamp(new Date().toISOString(), (dt: Date): string => {
return moment(dt).utc().subtract(dur, tsStr).format(format)}));
}
} else {
error = `${expr} can't evaluate.`;
Expand Down