@abapify/asjson-parser 0.0.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.
package/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ ## 0.0.4 (2024-10-07)
2
+
3
+ This was a version bump only, there were no code changes.
4
+
5
+ ## 0.0.3 (2024-10-07)
6
+
7
+ This was a version bump only, there were no code changes.
8
+
9
+ ## 0.0.2 (2024-10-07)
10
+
11
+ This was a version bump only, there were no code changes.
package/index.cjs.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "./src/index";
package/index.cjs.js ADDED
@@ -0,0 +1,54 @@
1
+ 'use strict';
2
+
3
+ const HEAP = '%heap';
4
+ const REF = '%ref';
5
+ const VAL = '%val';
6
+ const AbapJsonProxyHandler = {
7
+ // Prevents '%heap' from showing up during iteration (e.g., Object.keys)
8
+ ownKeys (target) {
9
+ const ownKeys = Reflect.ownKeys(target);
10
+ // if (ownKeys.includes(VAL)) {
11
+ // const val = Reflect.get(target, VAL);
12
+ // return Reflect.ownKeys(val);
13
+ // }
14
+ return ownKeys.filter((key)=>![
15
+ HEAP
16
+ ].includes(key.toString()));
17
+ },
18
+ get: (target, prop, receiver)=>{
19
+ // Use Reflect to access the property, but exclude '%heap'
20
+ if (prop === HEAP) {
21
+ return undefined;
22
+ }
23
+ let value = Reflect.get(target, prop, receiver);
24
+ const heap = Reflect.get(target, HEAP, receiver);
25
+ if (typeof value === "object" && value !== null && REF in value && /^#.*$/.test(value[REF])) {
26
+ const refKey = value[REF].substring(1); // Remove leading "#"
27
+ value = Reflect.get(heap, refKey);
28
+ }
29
+ if (typeof value === "object" && value !== null && VAL in value) {
30
+ value = value[VAL];
31
+ }
32
+ if (typeof value === "object" && value !== null) {
33
+ value = fromAbapJsonProxy(Object.assign(value, {
34
+ [HEAP]: heap
35
+ }));
36
+ }
37
+ return value;
38
+ }
39
+ };
40
+ function fromAbapJsonProxy(target) {
41
+ return new Proxy(target, AbapJsonProxyHandler);
42
+ }
43
+ function fromAbapJson(target) {
44
+ const proxy = new Proxy(target, AbapJsonProxyHandler);
45
+ return Object.fromEntries(Object.entries(proxy));
46
+ }
47
+ function parse(asjson) {
48
+ return fromAbapJson(JSON.parse(asjson));
49
+ }
50
+
51
+ exports.AbapJsonProxyHandler = AbapJsonProxyHandler;
52
+ exports.fromAbapJson = fromAbapJson;
53
+ exports.fromAbapJsonProxy = fromAbapJsonProxy;
54
+ exports.parse = parse;
package/index.esm.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "./src/index";
package/index.esm.js ADDED
@@ -0,0 +1,49 @@
1
+ const HEAP = '%heap';
2
+ const REF = '%ref';
3
+ const VAL = '%val';
4
+ const AbapJsonProxyHandler = {
5
+ // Prevents '%heap' from showing up during iteration (e.g., Object.keys)
6
+ ownKeys (target) {
7
+ const ownKeys = Reflect.ownKeys(target);
8
+ // if (ownKeys.includes(VAL)) {
9
+ // const val = Reflect.get(target, VAL);
10
+ // return Reflect.ownKeys(val);
11
+ // }
12
+ return ownKeys.filter((key)=>![
13
+ HEAP
14
+ ].includes(key.toString()));
15
+ },
16
+ get: (target, prop, receiver)=>{
17
+ // Use Reflect to access the property, but exclude '%heap'
18
+ if (prop === HEAP) {
19
+ return undefined;
20
+ }
21
+ let value = Reflect.get(target, prop, receiver);
22
+ const heap = Reflect.get(target, HEAP, receiver);
23
+ if (typeof value === "object" && value !== null && REF in value && /^#.*$/.test(value[REF])) {
24
+ const refKey = value[REF].substring(1); // Remove leading "#"
25
+ value = Reflect.get(heap, refKey);
26
+ }
27
+ if (typeof value === "object" && value !== null && VAL in value) {
28
+ value = value[VAL];
29
+ }
30
+ if (typeof value === "object" && value !== null) {
31
+ value = fromAbapJsonProxy(Object.assign(value, {
32
+ [HEAP]: heap
33
+ }));
34
+ }
35
+ return value;
36
+ }
37
+ };
38
+ function fromAbapJsonProxy(target) {
39
+ return new Proxy(target, AbapJsonProxyHandler);
40
+ }
41
+ function fromAbapJson(target) {
42
+ const proxy = new Proxy(target, AbapJsonProxyHandler);
43
+ return Object.fromEntries(Object.entries(proxy));
44
+ }
45
+ function parse(asjson) {
46
+ return fromAbapJson(JSON.parse(asjson));
47
+ }
48
+
49
+ export { AbapJsonProxyHandler, fromAbapJson, fromAbapJsonProxy, parse };
package/package.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "name": "@abapify/asjson-parser",
3
+ "version": "0.0.4",
4
+ "dependencies": {},
5
+ "main": "./index.cjs.js",
6
+ "module": "./index.esm.js",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/abapify/js.git",
10
+ "directory": "packages/asjson-parser"
11
+ },
12
+ "types": "./index.esm.d.ts"
13
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './lib/asjson';
@@ -0,0 +1,4 @@
1
+ export declare const AbapJsonProxyHandler: ProxyHandler<object>;
2
+ export declare function fromAbapJsonProxy(target: object): object;
3
+ export declare function fromAbapJson(target: object): object;
4
+ export declare function parse(asjson: string): object;