Skip to content

Commit

Permalink
ref: lift static type normalizer functions
Browse files Browse the repository at this point in the history
  • Loading branch information
coolaj86 committed Mar 13, 2024
1 parent 6bfdc6a commit c5f0871
Showing 1 changed file with 37 additions and 37 deletions.
74 changes: 37 additions & 37 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,40 @@ RpcClient.callspec = {
getUser: 'str',
};

RpcClient.typeNormalizers = {
array(arg) {
if (typeof arg === 'string') {
return JSON.parse(arg);
}
return arg;
},
bool(arg) {
return (arg === true || arg == '1' || arg == 'true' || arg.toString().toLowerCase() == 'true');
},
float(arg) {
return parseFloat(arg);
},
int(arg) {
return parseFloat(arg);
},
int_str(arg) {
if (typeof arg === 'number') {
return parseFloat(arg)
}

return arg.toString()
},
obj(arg) {
if (typeof arg === 'string') {
return JSON.parse(arg);
}
return arg;
},
str(arg) {
return arg.toString();
},
};

const slice = function (arr, start, end) {
return Array.prototype.slice.call(arr, start, end);
};
Expand Down Expand Up @@ -360,47 +394,13 @@ function generateRPCMethods(constructor, apiCalls, rpc) {
};
}

const types = {
str(arg) {
return arg.toString();
},
int(arg) {
return parseFloat(arg);
},
int_str(arg) {
if (typeof arg === 'number') {
return parseFloat(arg)
}

return arg.toString()
},
float(arg) {
return parseFloat(arg);
},
bool(arg) {
return (arg === true || arg == '1' || arg == 'true' || arg.toString().toLowerCase() == 'true');
},
obj(arg) {
if (typeof arg === 'string') {
return JSON.parse(arg);
}
return arg;
},
array(arg) {
if (typeof arg === 'string') {
return JSON.parse(arg);
}
return arg;
},
};

for (const k in apiCalls) {
const spec = apiCalls[k].split(' ');
for (let i = 0; i < spec.length; i++) {
if (types[spec[i]]) {
spec[i] = types[spec[i]];
if (RpcClient.typeNormalizers[spec[i]]) {
spec[i] = RpcClient.typeNormalizers[spec[i]];
} else {
spec[i] = types.str;
spec[i] = RpcClient.typeNormalizers.str;
}
}
const methodName = k.toLowerCase();
Expand Down

0 comments on commit c5f0871

Please sign in to comment.