@verii/batch-issuer 0.0.0-stage → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Copyright 2026 Velocity Team
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ const { flow, get, has, isFinite, keys, toLower, trim } = require('lodash/fp');
18
+
19
+ // Decimal notation only, so "0x10", "Infinity" and "" aren't numbers.
20
+ const DECIMAL_NUMBER = /^[+-]?(\d+\.?\d*|\.\d+)([eE][+-]?\d+)?$/;
21
+
22
+ const BOOLEANS = { true: true, false: false };
23
+
24
+ const toString = (text) => ({ value: text });
25
+
26
+ const toNumber = (text) => {
27
+ const trimmed = trim(text);
28
+ const value = Number(trimmed);
29
+ return DECIMAL_NUMBER.test(trimmed) && isFinite(value)
30
+ ? { value }
31
+ : { reason: `"${text}" is not a number` };
32
+ };
33
+
34
+ const booleanKey = flow(trim, toLower);
35
+
36
+ const toBoolean = (text) => {
37
+ const key = booleanKey(text);
38
+ return has([key], BOOLEANS)
39
+ ? { value: get([key], BOOLEANS) }
40
+ : { reason: `"${text}" is not true or false` };
41
+ };
42
+
43
+ const converters = {
44
+ string: toString,
45
+ number: toNumber,
46
+ boolean: toBoolean,
47
+ };
48
+
49
+ const VALUE_TYPES = keys(converters);
50
+
51
+ /**
52
+ * Converts a CSV cell to a value type.
53
+ * @param {string} valueType `string`, `number` or `boolean`
54
+ * @param {string} text the cell
55
+ * @returns {{ value: string|number|boolean } | { reason: string }} the value, or why the cell doesn't convert
56
+ */
57
+ const convertValue = (valueType, text) => {
58
+ if (!has([valueType], converters)) {
59
+ return { reason: `"${valueType}" is not a value type` };
60
+ }
61
+ const convert = get([valueType], converters);
62
+ return convert(text);
63
+ };
64
+
65
+ module.exports = { convertValue, VALUE_TYPES };