@okf/ootils 1.3.2 → 1.3.4

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,34 @@
1
+ declare const deleteVal: (data: any, valuePath: string) => any;
2
+
3
+ type ValuePath = string | string[];
4
+ type DataValue = any;
5
+ interface GetValOptions {
6
+ flattenIfValueIsArray?: boolean;
7
+ }
8
+
9
+ declare const setVal: (data: any, valuePath: string, value: DataValue) => any;
10
+
11
+ /**
12
+ *
13
+ * @param {*} data
14
+ * @param {*} valuePath
15
+ * @returns
16
+ * - if target value is an array found inside an array, it will flatten them out so that there is a single array
17
+ * - if target value is an array NOT found inside an array, that will be returned as is.
18
+ * - if target value is a string/object/number/boolean found inside an array of arrays, then the inner arrays will be flattened so that there is a single array
19
+ * - if target value is a string/object/number/boolean NOT found inside an array, it will be returned as is.
20
+ */
21
+ declare const getVal: (data: any, valuePath: ValuePath, options?: GetValOptions, depthIdx?: number) => any;
22
+
23
+ interface RichTextBlock {
24
+ text: string;
25
+ [key: string]: any;
26
+ }
27
+ interface RichTextValue {
28
+ blocks: RichTextBlock[];
29
+ [key: string]: any;
30
+ }
31
+ type TagNameInput = string | number | RichTextValue | null | undefined;
32
+ declare const genTagId: (tagName: TagNameInput) => string;
33
+
34
+ export { deleteVal, genTagId, getVal, setVal };
@@ -0,0 +1,191 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/universal.ts
21
+ var universal_exports = {};
22
+ __export(universal_exports, {
23
+ deleteVal: () => deleteVal,
24
+ genTagId: () => genTagId,
25
+ getVal: () => getVal,
26
+ setVal: () => setVal
27
+ });
28
+ module.exports = __toCommonJS(universal_exports);
29
+
30
+ // src/utils/getterSetterDeleter/utils/set_deleteVal.ts
31
+ var set_deleteVal = (action, data, valuePath, value) => {
32
+ if (valuePath === void 0) return;
33
+ if (valuePath === null && action === "set") {
34
+ data = value;
35
+ return data;
36
+ }
37
+ let dataRef = data;
38
+ const keysArray = valuePath.split(".");
39
+ const len = keysArray.length;
40
+ let valIsUndefined = false;
41
+ for (let i = 0; i < len - 1; i++) {
42
+ const key = keysArray[i];
43
+ if (!dataRef[key]) {
44
+ if (action === "set") {
45
+ const nextKey = keysArray[i + 1];
46
+ if (nextKey && !isNaN(parseInt(nextKey))) {
47
+ dataRef[key] = [];
48
+ } else {
49
+ dataRef[key] = {};
50
+ }
51
+ } else {
52
+ valIsUndefined = true;
53
+ break;
54
+ }
55
+ }
56
+ dataRef = dataRef[key];
57
+ }
58
+ if (valIsUndefined) return void 0;
59
+ if (action === "set") {
60
+ dataRef[keysArray[len - 1]] = value;
61
+ return data;
62
+ } else if (action === "delete") {
63
+ if (Array.isArray(dataRef)) {
64
+ dataRef.splice(parseInt(keysArray[len - 1]), 1);
65
+ } else {
66
+ delete dataRef[keysArray[len - 1]];
67
+ }
68
+ return data;
69
+ }
70
+ };
71
+
72
+ // src/utils/getterSetterDeleter/deleteVal.ts
73
+ var deleteVal = (data, valuePath) => {
74
+ return set_deleteVal("delete", data, valuePath);
75
+ };
76
+
77
+ // src/utils/getterSetterDeleter/setVal.ts
78
+ var setVal = (data, valuePath, value) => {
79
+ return set_deleteVal("set", data, valuePath, value);
80
+ };
81
+
82
+ // src/utils/getterSetterDeleter/utils/flattenArrayOfArrays.ts
83
+ var flattenArrayOfArrays = ({
84
+ array,
85
+ flattenLastArray = true,
86
+ toReturn = []
87
+ }) => {
88
+ array.map((ary) => {
89
+ if (Array.isArray(ary)) {
90
+ if (flattenLastArray === true) {
91
+ if (ary.some((childAry) => !Array.isArray(childAry))) {
92
+ toReturn.push(ary);
93
+ } else {
94
+ flattenArrayOfArrays({ array: ary, flattenLastArray, toReturn });
95
+ }
96
+ } else {
97
+ flattenArrayOfArrays({ array: ary, flattenLastArray, toReturn });
98
+ }
99
+ } else {
100
+ toReturn.push(ary);
101
+ }
102
+ });
103
+ return toReturn;
104
+ };
105
+
106
+ // src/utils/getterSetterDeleter/getVal.ts
107
+ var getVal = (data, valuePath, options = {}, depthIdx = 0) => {
108
+ let value = null;
109
+ const getFinalVal = (data2, valuePath2, options2, depthIdx2) => {
110
+ return Array.isArray(data2) ? data2.map((R) => getValV2_getter(R, valuePath2, options2, depthIdx2)) : getValV2_getter(data2, valuePath2, options2, depthIdx2);
111
+ };
112
+ if (Array.isArray(valuePath)) {
113
+ for (let i = 0; i < valuePath.length; i++) {
114
+ value = getFinalVal(data, valuePath[i], options, depthIdx);
115
+ if (value) break;
116
+ }
117
+ } else {
118
+ value = getFinalVal(data, valuePath, options, depthIdx);
119
+ }
120
+ return value;
121
+ };
122
+ var getValV2_getter = (data, valuePath, options, depthIdx) => {
123
+ const { flattenIfValueIsArray = true } = options;
124
+ if (valuePath === null) return data;
125
+ let dataRef = data;
126
+ const keysArray = valuePath.split(".");
127
+ const len = keysArray.length;
128
+ let valIsUndefined = false;
129
+ let thisIterationFoundAry = false;
130
+ for (let i = 0; i < len - 1; i++) {
131
+ const key = keysArray[i];
132
+ if (!thisIterationFoundAry) {
133
+ if (!dataRef[key]) {
134
+ valIsUndefined = true;
135
+ break;
136
+ }
137
+ dataRef = dataRef[key];
138
+ } else {
139
+ const nestedAryResponse = dataRef.map((dataRefItem) => {
140
+ const remainingValuePath = keysArray.filter((dd, ii) => ii >= i).join(".");
141
+ return getVal(dataRefItem, remainingValuePath, options, depthIdx + 1);
142
+ });
143
+ return flattenArrayOfArrays({
144
+ array: nestedAryResponse,
145
+ flattenLastArray: depthIdx === 0 && flattenIfValueIsArray === false ? false : true
146
+ });
147
+ }
148
+ if (Array.isArray(dataRef) && Number.isNaN(parseInt(keysArray[i + 1]))) thisIterationFoundAry = true;
149
+ }
150
+ if (valIsUndefined) return void 0;
151
+ if (thisIterationFoundAry) {
152
+ const toReturn = dataRef.map((d) => d[keysArray[len - 1]]);
153
+ return flattenArrayOfArrays({
154
+ array: toReturn,
155
+ flattenLastArray: flattenIfValueIsArray
156
+ });
157
+ }
158
+ return dataRef[keysArray[len - 1]];
159
+ };
160
+
161
+ // src/utils/genTagId.ts
162
+ var convertFromRichText = (value) => {
163
+ if (!value) return "";
164
+ let val = "";
165
+ if (typeof value === "string" || typeof value === "number") {
166
+ return String(value);
167
+ }
168
+ if (typeof value === "object" && "blocks" in value && Array.isArray(value.blocks)) {
169
+ for (let i = 0; i < value.blocks.length; i++) {
170
+ const block = value.blocks[i];
171
+ if (block && block.text && block.text.length > 0) {
172
+ val = val + block.text;
173
+ }
174
+ }
175
+ }
176
+ return val;
177
+ };
178
+ var genTagId = (tagName) => {
179
+ let toReturn = convertFromRichText(tagName);
180
+ const regex = /[^\p{L}\p{N}\+]+/gui;
181
+ toReturn = toReturn.trim().toLowerCase().replace(regex, "_");
182
+ toReturn = toReturn.replace(/\+/g, "plus");
183
+ return toReturn.replace(/^_+|_+$/, "");
184
+ };
185
+ // Annotate the CommonJS export names for ESM import in node:
186
+ 0 && (module.exports = {
187
+ deleteVal,
188
+ genTagId,
189
+ getVal,
190
+ setVal
191
+ });
@@ -0,0 +1,161 @@
1
+ // src/utils/getterSetterDeleter/utils/set_deleteVal.ts
2
+ var set_deleteVal = (action, data, valuePath, value) => {
3
+ if (valuePath === void 0) return;
4
+ if (valuePath === null && action === "set") {
5
+ data = value;
6
+ return data;
7
+ }
8
+ let dataRef = data;
9
+ const keysArray = valuePath.split(".");
10
+ const len = keysArray.length;
11
+ let valIsUndefined = false;
12
+ for (let i = 0; i < len - 1; i++) {
13
+ const key = keysArray[i];
14
+ if (!dataRef[key]) {
15
+ if (action === "set") {
16
+ const nextKey = keysArray[i + 1];
17
+ if (nextKey && !isNaN(parseInt(nextKey))) {
18
+ dataRef[key] = [];
19
+ } else {
20
+ dataRef[key] = {};
21
+ }
22
+ } else {
23
+ valIsUndefined = true;
24
+ break;
25
+ }
26
+ }
27
+ dataRef = dataRef[key];
28
+ }
29
+ if (valIsUndefined) return void 0;
30
+ if (action === "set") {
31
+ dataRef[keysArray[len - 1]] = value;
32
+ return data;
33
+ } else if (action === "delete") {
34
+ if (Array.isArray(dataRef)) {
35
+ dataRef.splice(parseInt(keysArray[len - 1]), 1);
36
+ } else {
37
+ delete dataRef[keysArray[len - 1]];
38
+ }
39
+ return data;
40
+ }
41
+ };
42
+
43
+ // src/utils/getterSetterDeleter/deleteVal.ts
44
+ var deleteVal = (data, valuePath) => {
45
+ return set_deleteVal("delete", data, valuePath);
46
+ };
47
+
48
+ // src/utils/getterSetterDeleter/setVal.ts
49
+ var setVal = (data, valuePath, value) => {
50
+ return set_deleteVal("set", data, valuePath, value);
51
+ };
52
+
53
+ // src/utils/getterSetterDeleter/utils/flattenArrayOfArrays.ts
54
+ var flattenArrayOfArrays = ({
55
+ array,
56
+ flattenLastArray = true,
57
+ toReturn = []
58
+ }) => {
59
+ array.map((ary) => {
60
+ if (Array.isArray(ary)) {
61
+ if (flattenLastArray === true) {
62
+ if (ary.some((childAry) => !Array.isArray(childAry))) {
63
+ toReturn.push(ary);
64
+ } else {
65
+ flattenArrayOfArrays({ array: ary, flattenLastArray, toReturn });
66
+ }
67
+ } else {
68
+ flattenArrayOfArrays({ array: ary, flattenLastArray, toReturn });
69
+ }
70
+ } else {
71
+ toReturn.push(ary);
72
+ }
73
+ });
74
+ return toReturn;
75
+ };
76
+
77
+ // src/utils/getterSetterDeleter/getVal.ts
78
+ var getVal = (data, valuePath, options = {}, depthIdx = 0) => {
79
+ let value = null;
80
+ const getFinalVal = (data2, valuePath2, options2, depthIdx2) => {
81
+ return Array.isArray(data2) ? data2.map((R) => getValV2_getter(R, valuePath2, options2, depthIdx2)) : getValV2_getter(data2, valuePath2, options2, depthIdx2);
82
+ };
83
+ if (Array.isArray(valuePath)) {
84
+ for (let i = 0; i < valuePath.length; i++) {
85
+ value = getFinalVal(data, valuePath[i], options, depthIdx);
86
+ if (value) break;
87
+ }
88
+ } else {
89
+ value = getFinalVal(data, valuePath, options, depthIdx);
90
+ }
91
+ return value;
92
+ };
93
+ var getValV2_getter = (data, valuePath, options, depthIdx) => {
94
+ const { flattenIfValueIsArray = true } = options;
95
+ if (valuePath === null) return data;
96
+ let dataRef = data;
97
+ const keysArray = valuePath.split(".");
98
+ const len = keysArray.length;
99
+ let valIsUndefined = false;
100
+ let thisIterationFoundAry = false;
101
+ for (let i = 0; i < len - 1; i++) {
102
+ const key = keysArray[i];
103
+ if (!thisIterationFoundAry) {
104
+ if (!dataRef[key]) {
105
+ valIsUndefined = true;
106
+ break;
107
+ }
108
+ dataRef = dataRef[key];
109
+ } else {
110
+ const nestedAryResponse = dataRef.map((dataRefItem) => {
111
+ const remainingValuePath = keysArray.filter((dd, ii) => ii >= i).join(".");
112
+ return getVal(dataRefItem, remainingValuePath, options, depthIdx + 1);
113
+ });
114
+ return flattenArrayOfArrays({
115
+ array: nestedAryResponse,
116
+ flattenLastArray: depthIdx === 0 && flattenIfValueIsArray === false ? false : true
117
+ });
118
+ }
119
+ if (Array.isArray(dataRef) && Number.isNaN(parseInt(keysArray[i + 1]))) thisIterationFoundAry = true;
120
+ }
121
+ if (valIsUndefined) return void 0;
122
+ if (thisIterationFoundAry) {
123
+ const toReturn = dataRef.map((d) => d[keysArray[len - 1]]);
124
+ return flattenArrayOfArrays({
125
+ array: toReturn,
126
+ flattenLastArray: flattenIfValueIsArray
127
+ });
128
+ }
129
+ return dataRef[keysArray[len - 1]];
130
+ };
131
+
132
+ // src/utils/genTagId.ts
133
+ var convertFromRichText = (value) => {
134
+ if (!value) return "";
135
+ let val = "";
136
+ if (typeof value === "string" || typeof value === "number") {
137
+ return String(value);
138
+ }
139
+ if (typeof value === "object" && "blocks" in value && Array.isArray(value.blocks)) {
140
+ for (let i = 0; i < value.blocks.length; i++) {
141
+ const block = value.blocks[i];
142
+ if (block && block.text && block.text.length > 0) {
143
+ val = val + block.text;
144
+ }
145
+ }
146
+ }
147
+ return val;
148
+ };
149
+ var genTagId = (tagName) => {
150
+ let toReturn = convertFromRichText(tagName);
151
+ const regex = /[^\p{L}\p{N}\+]+/gui;
152
+ toReturn = toReturn.trim().toLowerCase().replace(regex, "_");
153
+ toReturn = toReturn.replace(/\+/g, "plus");
154
+ return toReturn.replace(/^_+|_+$/, "");
155
+ };
156
+ export {
157
+ deleteVal,
158
+ genTagId,
159
+ getVal,
160
+ setVal
161
+ };
package/package.json CHANGED
@@ -3,16 +3,43 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.3.2",
6
+ "version": "1.3.4",
7
7
  "description": "Utility functions for both browser and Node.js",
8
8
  "main": "dist/index.js",
9
9
  "module": "dist/index.mjs",
10
10
  "types": "dist/index.d.ts",
11
11
  "exports": {
12
12
  ".": {
13
- "import": "./dist/index.mjs",
14
- "require": "./dist/index.js",
15
- "types": "./dist/index.d.ts"
13
+ "browser": {
14
+ "import": "./dist/browser.mjs",
15
+ "require": "./dist/browser.js",
16
+ "types": "./dist/browser.d.ts"
17
+ },
18
+ "node": {
19
+ "import": "./dist/node.mjs",
20
+ "require": "./dist/node.js",
21
+ "types": "./dist/node.d.ts"
22
+ },
23
+ "default": {
24
+ "import": "./dist/universal.mjs",
25
+ "require": "./dist/universal.js",
26
+ "types": "./dist/universal.d.ts"
27
+ }
28
+ },
29
+ "./browser": {
30
+ "import": "./dist/browser.mjs",
31
+ "require": "./dist/browser.js",
32
+ "types": "./dist/browser.d.ts"
33
+ },
34
+ "./node": {
35
+ "import": "./dist/node.mjs",
36
+ "require": "./dist/node.js",
37
+ "types": "./dist/node.d.ts"
38
+ },
39
+ "./universal": {
40
+ "import": "./dist/universal.mjs",
41
+ "require": "./dist/universal.js",
42
+ "types": "./dist/universal.d.ts"
16
43
  }
17
44
  },
18
45
  "files": [
@@ -24,7 +51,10 @@
24
51
  }
25
52
  },
26
53
  "scripts": {
27
- "build": "tsup src/index.ts --format cjs,esm --dts",
54
+ "build": "npm run build:universal && npm run build:browser && npm run build:node",
55
+ "build:universal": "tsup src/universal.ts --format cjs,esm --dts --out-dir dist",
56
+ "build:browser": "tsup src/browser.ts --format cjs,esm --dts --out-dir dist",
57
+ "build:node": "tsup src/node.ts --format cjs,esm --dts --out-dir dist",
28
58
  "prepublishOnly": "npm run build",
29
59
  "semantic-release": "semantic-release"
30
60
  },