Skip to content

Commit

Permalink
refactor(utils): setCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
thelindat committed Jul 27, 2023
1 parent 68f0dc5 commit bcb703a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 26 deletions.
18 changes: 6 additions & 12 deletions src/utils/parseArguments.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
import type { CFXCallback, CFXParameters } from '../types';
import type { CFXParameters } from '../types';
import { convertNamedPlaceholders } from '../config';

export const parseArguments = (
invokingResource: string,
query: string,
parameters?: CFXParameters,
cb?: CFXCallback
): [string, CFXParameters, CFXCallback | undefined] => {
parameters?: CFXParameters
): [string, CFXParameters] => {
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];
}

if (cb && typeof cb !== 'function') cb = undefined;

if (parameters && typeof parameters === 'function') {
cb = parameters;
parameters = [];
} else if (parameters === null || parameters === undefined) parameters = [];
if (!parameters || typeof parameters === 'function') parameters = [];

if (parameters && !Array.isArray(parameters)) {
let arr: unknown[] = [];
Expand All @@ -31,7 +25,7 @@ export const parseArguments = (
if (queryParams !== null) {
if (parameters.length === 0) {
for (let i = 0; i < queryParams.length; i++) parameters[i] = null;
return [query, parameters, cb];
return [query, parameters];
}
const diff = queryParams.length - parameters.length;

Expand All @@ -47,5 +41,5 @@ export const parseArguments = (
}
}

return [query, parameters, cb];
return [query, parameters];
};
4 changes: 4 additions & 0 deletions src/utils/parseExecute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export const executeType = (query: string) => {
};

export const parseExecute = (placeholders: number, parameters: CFXParameters) => {
const parametersType = typeof parameters;

if (!parameters || parametersType !== 'object') return [];

if (!Array.isArray(parameters)) {
if (typeof parameters === 'object') {
const arr: unknown[] = [];
Expand Down
19 changes: 5 additions & 14 deletions src/utils/parseTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
import { CFXCallback, CFXParameters, TransactionQuery } from '../types';
import { CFXParameters, TransactionQuery } from '../types';
import { parseArguments } from './parseArguments';

const isTransactionQuery = (query: TransactionQuery | string): query is TransactionQuery =>
(query as TransactionQuery).query !== undefined;

export const parseTransaction = (
invokingResource: string,
queries: TransactionQuery,
parameters: CFXParameters,
cb?: CFXCallback
) => {
export const parseTransaction = (invokingResource: string, queries: TransactionQuery, parameters: CFXParameters) => {
if (!Array.isArray(queries)) throw new Error(`Transaction queries must be array, received '${typeof queries}'.`);

if (cb && typeof cb !== 'function') cb = undefined;

if (parameters && typeof parameters === 'function') cb = parameters;

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

if (Array.isArray(queries[0])) {
const transactions = queries.map((query) => {
Expand All @@ -27,7 +18,7 @@ export const parseTransaction = (
return { query: parsedQuery, params: parsedParameters };
});

return { transactions, cb };
return transactions;
}

const transactions = queries.map((query) => {
Expand All @@ -44,5 +35,5 @@ export const parseTransaction = (
return { query: parsedQuery, params: parsedParameters };
});

return { transactions, cb };
return transactions;
};
6 changes: 6 additions & 0 deletions src/utils/setCallback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { CFXCallback, CFXParameters } from '../types';

export const setCallback = (parameters?: CFXParameters | CFXCallback, cb?: CFXCallback) => {
if (cb && typeof cb === 'function') return cb;
if (parameters && typeof parameters === 'function') return parameters;
};

0 comments on commit bcb703a

Please sign in to comment.