Skip to content

Commit f6f4594

Browse files
committed
Refactor converter map
1 parent 7f0b5a4 commit f6f4594

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

lib/asbind-instance/bind-function.js

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,44 +44,44 @@ function putArrayBuffer(asbindInstance, value, typeName) {
4444
);
4545
}
4646

47-
const ascToJsConverters = new Map([
48-
[/void/, nop],
49-
[/(i|u)(8|16|32)/, nop],
50-
[/f(32|64)/, nop],
51-
[/[sS]tring/, getString],
52-
[/(Ui|I)nt(8|16|32)Array/, getArrayBufferView],
53-
[/Big(Ui|I)nt64Array/, getArrayBufferView],
54-
[/Uint8ClampedArray/, getArrayBufferView],
55-
[/Float(32|64)Array/, getArrayBufferView]
47+
const converters = new Map([
48+
[/void/, { ascToJs: nop, jsToAsc: nop }],
49+
[/(i|u)(8|16|32)/, { ascToJs: nop, jsToAsc: nop }],
50+
[/f(32|64)/, { ascToJs: nop, jsToAsc: nop }],
51+
[/[sS]tring/, { ascToJs: getString, jsToAsc: putString }],
52+
[
53+
/(Ui|I)nt(8|16|32)Array/,
54+
{ ascToJs: getArrayBufferView, jsToAsc: putArrayBuffer }
55+
],
56+
[
57+
/Big(Ui|I)nt64Array/,
58+
{ ascToJs: getArrayBufferView, jsToAsc: putArrayBuffer }
59+
],
60+
[
61+
/Uint8ClampedArray/,
62+
{ ascToJs: getArrayBufferView, jsToAsc: putArrayBuffer }
63+
],
64+
[
65+
/Float(32|64)Array/,
66+
{ ascToJs: getArrayBufferView, jsToAsc: putArrayBuffer }
67+
]
5668
]);
5769

58-
function getAscToJsConverterForType(typeName) {
59-
for (const [matcher, converter] of ascToJsConverters) {
70+
function getConverterForType(typeName) {
71+
for (const [matcher, converter] of converters) {
6072
if (matcher.test(typeName)) {
6173
return converter;
6274
}
6375
}
64-
throw Error(`No AscToJs converter for ${JSON.stringify(typeName)}`);
76+
throw Error(`No converter for ${JSON.stringify(typeName)}`);
6577
}
6678

67-
const jsToAscConverters = new Map([
68-
[/void/, nop],
69-
[/(i|u)(8|16|32)/, nop],
70-
[/f(32|64)/, nop],
71-
[/[sS]tring/, putString],
72-
[/(Ui|I)nt(8|16|32)Array/, putArrayBuffer],
73-
[/Big(Ui|I)nt64Array/, putArrayBuffer],
74-
[/Uint8ClampedArray/, putArrayBuffer],
75-
[/Float(32|64)Array/, putArrayBuffer]
76-
]);
79+
function getAscToJsConverterForType(typeName) {
80+
return getConverterForType(typeName)?.ascToJs;
81+
}
7782

7883
function getJsToAscConverterForType(typeName) {
79-
for (const [matcher, converter] of jsToAscConverters) {
80-
if (matcher.test(typeName)) {
81-
return converter;
82-
}
83-
}
84-
throw Error(`No JsToAsc converter for ${JSON.stringify(typeName)}`);
84+
return getConverterForType(typeName)?.jsToAsc;
8585
}
8686

8787
// Function that takes in an asbindInstance, and importObject, and the path to the import function on the object

0 commit comments

Comments
 (0)