Skip to content

Commit

Permalink
fix(basic.gblib): NOW keyword is now formatting values with two zeros.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigorodriguez committed Aug 16, 2021
1 parent 5fceb97 commit 1ed7cfa
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 51 deletions.
7 changes: 6 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"ibm-watson": "6.1.1",
"js-beautify": "1.13.13",
"marked": "2.0.7",
"momentjs": "^2.0.0",
"ms-rest-azure": "3.0.0",
"nexmo": "2.9.1",
"node-cron": "3.0.0",
Expand Down
34 changes: 26 additions & 8 deletions packages/basic.gblib/services/DialogKeywords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,23 @@ export class DialogKeywords {
*
*/
public getWeekFromDate(date) {
if (!(date instanceof Date)) {
date = new Date(date);
}

const contentLocale = this.min.core.getParam<string>(
this.min.instance,
'Default Content Language',
GBConfigService.get('DEFAULT_CONTENT_LANGUAGE')
);

return date.toLocaleString(this.getContentLocaleWithCulture(contentLocale), { weekday: 'short' });
let dt = SystemKeywords.getDateFromLocaleString(date, contentLocale);

if (dt) {
if (!(dt instanceof Date)) {
dt = new Date(dt);
}
let week = dt.toLocaleString(this.getContentLocaleWithCulture(contentLocale), { weekday: 'short' });
return week.substr(0,3);
}
return 'NULL';
}

/**
Expand Down Expand Up @@ -241,16 +247,28 @@ export class DialogKeywords {
*
*/
public getHourFromDate(date) {
if (!(date instanceof Date)) {
date = new Date(date);
}
function addZero(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
return addZero(date.getHours()) + ':' + addZero(date.getMinutes());

const contentLocale = this.min.core.getParam<string>(
this.min.instance,
'Default Content Language',
GBConfigService.get('DEFAULT_CONTENT_LANGUAGE')
);

let dt = SystemKeywords.getDateFromLocaleString(date, contentLocale);

if (dt) {
if (!(dt instanceof Date)) {
dt = new Date(dt);
}
return addZero(dt.getHours()) + ':' + addZero(dt.getMinutes());
}
return 'NULL';
}

/**
Expand Down
12 changes: 7 additions & 5 deletions packages/basic.gblib/services/GBVMService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class GBVMService extends GBService {
}
else {
await s.deleteScheduleIfAny(min, mainName);
}
}
text = text.replace(/SET SCHEDULE (.*)/gi, '');
fs.writeFileSync(urlJoin(folder, vbsFile), text);
}
Expand Down Expand Up @@ -246,12 +246,12 @@ export class GBVMService extends GBService {

code = code.replace(/(\w+)\s*=\s*find\s*(.*)\s*or talk\s*(.*)/gi, ($0, $1, $2, $3) => {
return `${$1} = sys().find(${$2})\n
if (!${$1}) {=
if (!${$1}) {
if (resolve){
resolve();
}
talk (${$3})\n;
return;
return -1;
}
`;
});
Expand Down Expand Up @@ -792,7 +792,7 @@ export class GBVMService extends GBService {
// Creates a class DialogKeywords which is the *this* pointer
// in BASIC.

const user =step? await min.userProfile.get(step.context, {}): null;
const user = step ? await min.userProfile.get(step.context, {}) : null;
const sandbox: DialogKeywords = new DialogKeywords(min, deployer, step, user);

// Injects the .gbdialog generated code into the VM.
Expand Down Expand Up @@ -821,7 +821,9 @@ export class GBVMService extends GBService {
let ret = null;
try {
ret = await sandbox[mainMethod](step);

if (ret == -1) {
await step.endDialog();
}
} catch (error) {
throw new Error(`BASIC ERROR: ${error.message ? error.message : error}\n Stack:${error.stack}`);
}
Expand Down
103 changes: 66 additions & 37 deletions packages/basic.gblib/services/SystemKeywords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,18 @@ export class SystemKeywords {
}

public async sortBy(array, memberName) {
return array ? array.sort(p => { if (p) { return p[memberName]; } }) :
null;
return array;
// return array ? array.sort(p => {

// var c = new Date(a.date);
// var d = new Date(b.date);
// return c - d;
// });

// if (p) {
// return ;
// }
// }): null;
}

/**
Expand Down Expand Up @@ -276,6 +286,35 @@ export class SystemKeywords {
return val;
}

public isValidDate(dt) {
const contentLocale = this.min.core.getParam<string>(
this.min.instance,
'Default Content Language',
GBConfigService.get('DEFAULT_CONTENT_LANGUAGE')
);

let date = SystemKeywords.getDateFromLocaleString(dt, contentLocale);
if (!date) {
return false;
}

if (!(date instanceof Date)) {
date = new Date(date);
}

return !isNaN(date.valueOf());
}

public isValidNumber(number) {
if (number === '') { return false }
return !isNaN(number);
}

public isValidHour(value) {
return /^([01]?[0-9]|2[0-3]):[0-5][0-9]$/.test(value);
}


/**
* Finds a value or multi-value results in a tabular file.
*
Expand Down Expand Up @@ -349,28 +388,6 @@ export class SystemKeywords {
GBConfigService.get('DEFAULT_CONTENT_LANGUAGE')
);

function isValidDate(dt) {
let date = SystemKeywords.getDateFromLocaleString(dt, contentLocale);
if (!date) {

return false;
}

if (!(date instanceof Date)) {
date = new Date(date);
}

return !isNaN(date.valueOf());
}

function isValidNumber(number) {
if (number === '') { return false }
return !isNaN(number);
}

function isValidHour(value) {
return /^([01]?[0-9]|2[0-3]):[0-5][0-9]$/.test(value);
}
// Increments columnIndex by looping until find a column match.

const filters = [];
Expand All @@ -389,12 +406,12 @@ export class SystemKeywords {
}
filter.columnIndex = columnIndex;

if (isValidHour(filter.value)) {
if (this.isValidHour(filter.value)) {
filter.dataType = 'hourInterval';
} else if (isValidDate(filter.value)) {
} else if (this.isValidDate(filter.value)) {
filter.value = SystemKeywords.getDateFromLocaleString(filter.value, contentLocale);
filter.dataType = 'date';
} else if (isValidNumber(filter.value)) {
} else if (this.isValidNumber(filter.value)) {
filter.value = Number.parseInt(filter.value);
filter.dataType = 'number';
} else {
Expand Down Expand Up @@ -451,6 +468,11 @@ export class SystemKeywords {

case 'hourInterval':
switch (filter.operator) {
case '=':
if (result && result.toLowerCase().trim() === filter.value.toLowerCase().trim()) {
filterAcceptCount++;
}
break;
case 'in':
const e = result.split(';');
const hr = Number.parseInt(filter.value.split(':')[0]);
Expand Down Expand Up @@ -522,22 +544,29 @@ export class SystemKeywords {
}
}

private static getDateFromLocaleString(date: any, contentLocale: any) {
const parts = /^([0-3]?[0-9]).([0-3]?[0-9]).((?:[0-9]{2})?[0-9]{2})$/gi.exec(date);
if (parts && parts[3]) {
public static getDateFromLocaleString(date: any, contentLocale: any) {
let parts = /^([0-3]?[0-9]).([0-3]?[0-9]).((?:[0-9]{2})?[0-9]{2})\s*(10|11|12|[1-9]):([0-5][0-9])/gi.exec(date);
if (parts && parts[5]) {
switch (contentLocale) {
case 'pt':
date = new Date(Number.parseInt(parts[2]), Number.parseInt(parts[1]), Number.parseInt(parts[3]), 0, 0, 0, 0);
break;
return new Date(Number.parseInt(parts[3]), Number.parseInt(parts[2]) - 1, Number.parseInt(parts[1]),
Number.parseInt(parts[4]), Number.parseInt(parts[5]), 0, 0);
case 'en':
date = new Date(Number.parseInt(parts[1]), Number.parseInt(parts[2]), Number.parseInt(parts[3]), 0, 0, 0, 0);
break;
return new Date(Number.parseInt(parts[3]), Number.parseInt(parts[1]) - 1, Number.parseInt(parts[2]),
Number.parseInt(parts[4]), Number.parseInt(parts[5]), 0, 0);
}
return date;
}
else {
return null;

parts = /^([0-3]?[0-9]).([0-3]?[0-9]).((?:[0-9]{2})?[0-9]{2})$/gi.exec(date);
if (parts && parts[3]) {
switch (contentLocale) {
case 'pt':
return new Date(Number.parseInt(parts[2]), Number.parseInt(parts[1]) - 1, Number.parseInt(parts[3]), 0, 0, 0, 0);
case 'en':
return new Date(Number.parseInt(parts[1]), Number.parseInt(parts[2]) - 1, Number.parseInt(parts[3]), 0, 0, 0, 0);
}
}
return null;
}

/**
Expand Down

0 comments on commit 1ed7cfa

Please sign in to comment.