fix(basic.gbapp): FIND now return case insensitive BASIC objects.

This commit is contained in:
Rodrigo Rodriguez 2023-08-20 19:21:35 -03:00
parent 81c7fc3972
commit 701bbae2f2
3 changed files with 24 additions and 17 deletions

View file

@ -243,6 +243,26 @@ export class GBVMService extends GBService {
let col = 1;
let index = 1
// Makes objects in BASIC insensitive.
const caseInsensitive = (listOrRow) => {
const lowercase = (oldKey) => typeof oldKey === 'string' ? oldKey.toLowerCase() : oldKey;
const createCaseInsensitiveProxy = (obj) => {
const propertiesMap = new Map(Object.keys(obj).map(propKey => [lowercase(propKey), obj[propKey]]));
const caseInsensitiveGetHandler = {
get: (target, property) => propertiesMap.get(lowercase(property))
};
return new Proxy(obj, caseInsensitiveGetHandler);
};
if (listOrRow.length) {
return listOrRow.map(row => createCaseInsensitiveProxy(row));
} else {
return createCaseInsensitiveProxy(listOrRow);
}
};
// Transfers NLP auto variables into global object.
for(i in this.variables) {

View file

@ -466,11 +466,12 @@ export class KeywordsExpressions {
keywords[i++] = [
/^\s*((?:[a-z]+.?)(?:(?:\w+).)(?:\w+)*)\s*=\s*find\s*(.*)\s*or talk\s*(.*)/gim,
($0, $1, $2, $3) => {
return `${$1} = await sys.find({pid: pid, handle: page, args:[${$2}]})\n
`${$1} = caseInsensitive(await sys.find({pid: pid, handle: page, args:[${$2}]}))\n
if (!${$1}) {
await dk.talk ({pid: pid, text: ${$3}})\n;
return -1;
}
return ${$1};
`;
}
];
@ -486,7 +487,7 @@ export class KeywordsExpressions {
/^\s*((?:[a-z]+.?)(?:(?:\w+).)(?:\w+)*)\s*=\s*find\s*(.*)/gim,
($0, $1, $2, $3) => {
return `
${$1} = await sys.find({pid: pid, handle: page, args: [${$2}]})`;
${$1} = caseInsensitive(await sys.find({pid: pid, handle: page, args: [${$2}]}))`;
}
];

View file

@ -1153,27 +1153,13 @@ export class SystemKeywords {
row['ordinal'] = rowCount;
row['line'] = foundIndex + 1;
const lowercase = (oldKey) => {
// Check that it's a string.
return typeof oldKey === 'string' ? oldKey.toLowerCase() : oldKey;
}
const propertiesMap = new Map(
Object.keys(row).map(propKey => [lowercase(propKey), row[propKey]])
);
const caseInsensitiveGetHandler = {
get: function(target, property, receiver) {
return propertiesMap.get(lowercase(property));
}
};
row = new Proxy(row, caseInsensitiveGetHandler);
table.push(row);
}
}
const outputArray = await DialogKeywords.getOption({ pid, name: 'output' });
if (table.length === 1) {
if (table.length === 1) {
GBLog.info(`BASIC: FIND returned no results (zero rows).`);
return null;
} else if (table.length === 2 && !outputArray) {