Skip to content

Commit 7d1b7fb

Browse files
author
Jacob Groß
committed
use bound function for arraySerializer
1 parent 99f2161 commit 7d1b7fb

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

src/_utils.mjs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,43 +29,40 @@ const __find = (path) => {
2929
return eval(`(obj=>${str})`);
3030
};
3131

32+
function _arraySerializer (serializer, array) {
33+
// Stringifying more complex array using the provided sjs schema
34+
let acc = '';
35+
const len = array.length - 1;
36+
for (let i = 0; i < len; ++i) {
37+
acc += `${serializer(array[i])},`;
38+
}
39+
40+
// Prevent slice for removing unnecessary comma.
41+
acc += serializer(array[len]);
42+
return `[${acc}]`;
43+
}
44+
3245
/**
3346
* `_makeArraySerializer` is simply a wrapper of another `sjs` schema
3447
* used for the serialization of arrais.
35-
*
36-
* @param {array} array - Array to serialize.
37-
* @param {any} method - `sjs` serializer.
3848
*/
3949
const _makeArraySerializer = (serializer) => {
40-
if (serializer instanceof Function) {
41-
return (array) => {
42-
// Stringifying more complex array using the provided sjs schema
43-
let acc = '';
44-
const len = array.length - 1;
45-
for (let i = 0; i < len; ++i) {
46-
acc += `${serializer(array[i])},`;
47-
}
48-
49-
// Prevent slice for removing unnecessary comma.
50-
acc += serializer(array[len]);
51-
return `[${acc}]`;
52-
};
53-
}
54-
55-
return array => JSON.stringify(array);
50+
if (typeof serializer === 'function') return _arraySerializer.bind(null, serializer);
51+
return JSON.stringify;
5652
};
5753

5854
const TYPES = ['number', 'string', 'boolean', 'array', 'null'];
5955

60-
/*#__PURE__*/
61-
function checkType(type) {
56+
/*#__PURE__*/function checkType(type) {
6257
if (typeof process !== 'undefined' && process.env.NODE_ENV !== 'production' && !TYPES.includes(type)) {
6358
throw new Error(
6459
`Expected one of: "number", "string", "boolean", "array", "null". received "${type}" instead`,
6560
);
6661
}
6762
}
6863

64+
const fnUser = (value) => value;
65+
6966
/**
7067
* @param type number|string|boolean|array|null
7168
* @param serializer
@@ -74,7 +71,7 @@ function checkType(type) {
7471
const attr = (type, serializer) => {
7572
/*#__PURE__*/checkType(type);
7673

77-
const usedSerializer = serializer || (value => value);
74+
const usedSerializer = serializer || fnUser;
7875

7976
return {
8077
isSJS: true,

0 commit comments

Comments
 (0)