Skip to content

Commit

Permalink
fix(utils/parseArguments): resolve null parameters in sparse arrays
Browse files Browse the repository at this point in the history
Resolves #245.
  • Loading branch information
thelindat committed Oct 6, 2024
1 parent 8e79833 commit e6140e8
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/utils/parseArguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,34 @@ export const parseArguments = (query: string, parameters?: CFXParameters): [stri

if (convertNamedPlaceholders && parameters && typeof parameters === 'object' && !Array.isArray(parameters))
if (query.includes(':') || query.includes('@')) {
const placeholders = convertNamedPlaceholders(query, parameters);
query = placeholders[0];
parameters = placeholders[1];
[query, parameters] = convertNamedPlaceholders(query, parameters);
}

if (!parameters || typeof parameters === 'function') parameters = [];

const placeholders = query.match(/\?(?!\?)/g)?.length ?? 0;

if (parameters && !Array.isArray(parameters)) {
let arr: unknown[] = [];
Object.entries(parameters).forEach((entry) => (arr[parseInt(entry[0]) - 1] = entry[1]));

for (let i = 0; i < placeholders; i++) {
arr[i] = parameters[i + 1] ?? null;
}

parameters = arr;
} else {
const queryParams = query.match(/\?(?!\?)/g);

if (queryParams !== null) {
if (placeholders) {
if (parameters.length === 0) {
for (let i = 0; i < queryParams.length; i++) parameters[i] = null;
for (let i = 0; i < placeholders; i++) parameters[i] = null;
return [query, parameters];
}
const diff = queryParams.length - parameters.length;

const diff = placeholders - parameters.length;

if (diff > 0) {
for (let i = 0; i < diff; i++) parameters[queryParams.length + i] = null;
for (let i = 0; i < diff; i++) parameters[placeholders + i] = null;
} else if (diff < 0) {
throw new Error(`Expected ${queryParams.length} parameters, but received ${parameters.length}.`);
throw new Error(`Expected ${placeholders} parameters, but received ${parameters.length}.`);
}
}
}
Expand Down

0 comments on commit e6140e8

Please sign in to comment.