@fixefy/fixefy-ui-utils 0.0.334 → 0.0.335

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,165 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ getJPart: function() {
13
+ return getJPart;
14
+ },
15
+ getJToken: function() {
16
+ return getJToken;
17
+ },
18
+ isHasKey: function() {
19
+ return isHasKey;
20
+ },
21
+ isJson: function() {
22
+ return isJson;
23
+ },
24
+ isNested: function() {
25
+ return isNested;
26
+ },
27
+ isResultValid: function() {
28
+ return isResultValid;
29
+ },
30
+ setJToken: function() {
31
+ return setJToken;
32
+ }
33
+ });
34
+ const _ = require("..");
35
+ const getJToken = (jToken, jsonPath, defaultValue)=>{
36
+ // input check - if key is invalid - return error
37
+ if ((0, _.isObjectValid)(jToken) === false) return defaultValue;
38
+ // convert indexes to properties
39
+ jsonPath = jsonPath.replace(/\[(\w+)\]/g, '.$1');
40
+ // strip a leading dot
41
+ jsonPath = jsonPath.replace(/^\./, '');
42
+ // split path by '.'
43
+ let isContinueLoop = true;
44
+ let currentToken;
45
+ const jsonPathParts = jsonPath.split('.');
46
+ try {
47
+ //for ( let i = 0, n = jsonPathParts.length && isContinueLoop; i < n; ++i ) {
48
+ for(let i = 0, n = jsonPathParts.length; isContinueLoop && i < n; ++i){
49
+ currentToken = jsonPathParts[i];
50
+ if (typeof jToken === 'string') {
51
+ jToken = JSON.parse(jToken);
52
+ }
53
+ if (currentToken in jToken) {
54
+ jToken = jToken[currentToken];
55
+ } else {
56
+ // if something in the process failed:
57
+ // 1. set the rv as the default value
58
+ // 2. stop the loop
59
+ jToken = defaultValue;
60
+ isContinueLoop = false;
61
+ }
62
+ }
63
+ } catch (e) {
64
+ jToken = 'No Value Found';
65
+ }
66
+ return jToken;
67
+ };
68
+ const getJPart = (j, jsonPath, defaultValue)=>{
69
+ // if key is invalid - return error
70
+ if (j == null) return defaultValue;
71
+ // convert indexes to properties
72
+ jsonPath = jsonPath.replace(/\[(\w+)\]/g, '.$1');
73
+ // strip a leading dot
74
+ jsonPath = jsonPath.replace(/^\./, '');
75
+ // split path by '.'
76
+ let _isArrayValid = false, _isStringValid = false, currentPathPart, isLoop = true;
77
+ const pathParts = jsonPath.split('.');
78
+ for(let i = 0, n = pathParts.length; isLoop && i < n; ++i){
79
+ currentPathPart = pathParts[i];
80
+ _isStringValid = (0, _.isStringValid)(j);
81
+ _isArrayValid = (0, _.isArrayValid)(j);
82
+ if (_isStringValid === true || _isArrayValid === true) {
83
+ if (_isStringValid === true) {
84
+ j = JSON.parse(j);
85
+ } else {
86
+ j = j.reduce((acc, cur)=>{
87
+ const innerJ = getJPart(cur, pathParts.slice(i).join('.'), defaultValue);
88
+ if ((0, _.isArrayValid)(innerJ)) {
89
+ acc.push(...innerJ);
90
+ } else {
91
+ acc.push(innerJ);
92
+ }
93
+ return acc;
94
+ }, []);
95
+ isLoop = false;
96
+ }
97
+ } else {
98
+ if (j[currentPathPart] != null && currentPathPart in j) {
99
+ j = j[currentPathPart];
100
+ } else {
101
+ // if something in the process failed:
102
+ // 1. set the rv as the default value
103
+ // 2. stop the loop`
104
+ j = defaultValue;
105
+ isLoop = false;
106
+ }
107
+ }
108
+ }
109
+ return j;
110
+ };
111
+ const isHasKey = (array, key)=>{
112
+ let rv = false, isContinueLoop = true, current_keys;
113
+ for(let i = 0; i < array.length && isContinueLoop; ++i){
114
+ current_keys = Object.keys(array[i]);
115
+ if ((current_keys || []).indexOf(key) > -1) {
116
+ rv = true;
117
+ isContinueLoop = false;
118
+ }
119
+ }
120
+ return rv;
121
+ };
122
+ const isJson = (obj)=>{
123
+ let objAsString;
124
+ try {
125
+ objAsString = JSON.stringify(obj);
126
+ } catch (e) {
127
+ objAsString = null;
128
+ }
129
+ if (!objAsString || (0, _.isStringValid)(objAsString) === false) return false;
130
+ const rv = (objAsString === null || objAsString === void 0 ? void 0 : objAsString.indexOf('{')) === 0 && (objAsString === null || objAsString === void 0 ? void 0 : objAsString.indexOf('}')) === (objAsString === null || objAsString === void 0 ? void 0 : objAsString.length) - 1;
131
+ return rv;
132
+ };
133
+ const isNested = (obj)=>{
134
+ const rv = Object.keys(obj).some((key)=>{
135
+ return obj[key] && typeof obj[key] === 'object';
136
+ });
137
+ return rv;
138
+ };
139
+ const isResultValid = (jResult)=>{
140
+ if ((0, _.isObjectValid)(jResult) === false) return false;
141
+ const rv = getJPart(jResult, 'ok', 0);
142
+ return !!rv;
143
+ };
144
+ const setJToken = (jEntity, fieldPathsAndValues)=>{
145
+ let jCurrentToken, currentPath, pathParts, fieldName;
146
+ for(let i = 0; i < fieldPathsAndValues.length; ++i){
147
+ currentPath = fieldPathsAndValues[i]['json_path'];
148
+ if ((0, _.isStringValid)(currentPath)) {
149
+ fieldName = currentPath;
150
+ pathParts = currentPath.split('.');
151
+ if ((0, _.isArrayValid)(pathParts)) {
152
+ fieldName = pathParts[pathParts.length - 1];
153
+ pathParts = pathParts.splice(0, Math.max(0, pathParts.length - 1));
154
+ if ((0, _.isArrayValid)(pathParts)) {
155
+ currentPath = pathParts.join('.');
156
+ }
157
+ }
158
+ jCurrentToken = getJPart(jEntity, currentPath, null);
159
+ if ((0, _.isObjectValid)(jCurrentToken) || isJson(jCurrentToken) === false) {
160
+ jCurrentToken = jEntity;
161
+ }
162
+ jCurrentToken[fieldName] = fieldPathsAndValues[i]['value'];
163
+ }
164
+ }
165
+ };