@xuda.io/xuda-get-cast-util-module 1.0.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.
Files changed (2) hide show
  1. package/index.mjs +149 -0
  2. package/package.json +8 -0
package/index.mjs ADDED
@@ -0,0 +1,149 @@
1
+ export const cast = function (
2
+ typeP,
3
+ valP,
4
+ report_conversion_error_callback,
5
+ report_conversion_warn_callback
6
+ ) {
7
+ if (typeof _ === "undefined") {
8
+ console.error("error: _ library not defined in cast");
9
+ return valP;
10
+ }
11
+
12
+ var ret = valP;
13
+
14
+ const report_conversion_error = function (res) {
15
+ if (report_conversion_error_callback) {
16
+ report_conversion_error_callback(res, typeP, valP);
17
+ }
18
+ };
19
+ const report_conversion_warn = function (res) {
20
+ if (report_conversion_warn_callback) {
21
+ report_conversion_warn_callback(res, typeP, valP);
22
+ }
23
+ };
24
+ switch (typeP) {
25
+ case "boolean":
26
+ case "bool":
27
+ const true_bool_opt = ["Y", 1, true, "TRUE", "ON", "YES", "1"];
28
+ const false_bool_opt = ["N", 0, false, "FALSE", "OFF", "NO", "0"];
29
+ if (true_bool_opt.includes(_.toUpper(valP))) {
30
+ ret = true;
31
+ break;
32
+ }
33
+
34
+ if (false_bool_opt.includes(_.toUpper(valP)) || !valP) {
35
+ ret = false;
36
+ }
37
+
38
+ break;
39
+
40
+ case "number":
41
+ if (
42
+ typeof valP === "undefined" ||
43
+ typeof valP === "NaN" ||
44
+ valP === null
45
+ ) {
46
+ ret = 0;
47
+ } else {
48
+ if (typeof valP !== "number") {
49
+ try {
50
+ if (typeof valP === "boolean") {
51
+ ret = valP ? 1 : 0;
52
+ } else {
53
+ ret = _.toNumber(valP);
54
+ }
55
+ // normal behavior studio always store values as string
56
+ // report_conversion_warn();
57
+ } catch (e) {
58
+ ret = 0;
59
+ report_conversion_error(0);
60
+ }
61
+ }
62
+ }
63
+ break;
64
+ case "string":
65
+ if (
66
+ typeof valP === "undefined" ||
67
+ typeof valP === "NaN" ||
68
+ valP === null
69
+ ) {
70
+ ret = "";
71
+ } else {
72
+ if (typeof valP !== "string") {
73
+ try {
74
+ ret = _.toString(valP);
75
+ report_conversion_warn();
76
+ } catch (e) {
77
+ ret = "";
78
+ report_conversion_error("blanc");
79
+ }
80
+ }
81
+ }
82
+ break;
83
+ case "object":
84
+ if (
85
+ typeof valP === "undefined" ||
86
+ typeof valP === "NaN" ||
87
+ valP === null
88
+ ) {
89
+ ret = {};
90
+ } else {
91
+ if (typeof valP !== "object") {
92
+ try {
93
+ if (valP) {
94
+ ret = JSON5.parse(valP);
95
+ } else {
96
+ ret = {};
97
+ }
98
+ } catch (e) {
99
+ try {
100
+ ret = JSON5.parse(valP);
101
+ } catch (error) {
102
+ ret = {};
103
+ report_conversion_error("{}");
104
+ }
105
+ }
106
+ }
107
+ }
108
+ break;
109
+ case "array":
110
+ if (
111
+ typeof valP === "undefined" ||
112
+ typeof valP === "NaN" ||
113
+ valP === null ||
114
+ valP === ""
115
+ ) {
116
+ ret = [];
117
+ } else {
118
+ if (!Array.isArray(valP)) {
119
+ try {
120
+ ret = _.toArray(eval(valP));
121
+ } catch (e) {
122
+ ret = [];
123
+ report_conversion_error("[]");
124
+ }
125
+ }
126
+ }
127
+ break;
128
+ default:
129
+ if (
130
+ typeof valP === "undefined" ||
131
+ typeof valP === "NaN" ||
132
+ valP === null
133
+ ) {
134
+ ret = "";
135
+ } else {
136
+ if (typeof valP !== "string") {
137
+ try {
138
+ ret = valP.toString();
139
+ report_conversion_warn();
140
+ } catch (e) {
141
+ ret = "";
142
+ report_conversion_error("blanc");
143
+ }
144
+ }
145
+ }
146
+ break;
147
+ }
148
+ return ret;
149
+ };
package/package.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "name": "@xuda.io/xuda-get-cast-util-module",
3
+ "version": "1.0.0",
4
+ "main": "index.mjs",
5
+ "type": "module",
6
+ "description": "Auto-generated build for xuda-get-cast-util-module.mjs",
7
+ "author": "Auto Publisher"
8
+ }