Skip to content

Commit 6ccf4c3

Browse files
authored
Merge pull request #6 from contexD/add-json-parsing
Add json parsing
2 parents 1cc769b + dff9772 commit 6ccf4c3

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
"lint": "tsdx lint",
2121
"prepare": "tsdx build"
2222
},
23-
2423
"husky": {
2524
"hooks": {
2625
"pre-commit": "tsdx lint"
@@ -42,6 +41,7 @@
4241
"devDependencies": {
4342
"@types/react": "^16.9.23",
4443
"@types/react-dom": "^16.9.5",
44+
"fast-check": "^2.13.0",
4545
"husky": "^4.2.5",
4646
"mutationobserver-shim": "^0.3.7",
4747
"tsdx": "^0.13.2",

src/abode.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,13 @@ export const getElementProps = (el: Element | HTMLScriptElement): Props => {
7979
const rawProps = Array.from(el.attributes).filter(attribute =>
8080
attribute.name.startsWith('data-prop-')
8181
);
82-
rawProps.forEach(prop => (props[getCleanPropName(prop.name)] = prop.value));
82+
rawProps.forEach(prop => {
83+
try {
84+
props[getCleanPropName(prop.name)] = JSON.parse(prop.value);
85+
} catch (e) {
86+
props[getCleanPropName(prop.name)] = prop.value;
87+
}
88+
});
8389
}
8490

8591
return props;

test/abode.test.tsx

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as fc from 'fast-check';
12
import {
23
getCleanPropName,
34
getAbodeElements,
@@ -62,10 +63,35 @@ describe('helper functions', () => {
6263
const abodeElement = document.createElement('div');
6364
abodeElement.setAttribute('data-component', 'TestComponent');
6465
abodeElement.setAttribute('data-prop-test-prop', 'testPropValue');
66+
abodeElement.setAttribute('data-prop-number-prop', '12345');
67+
abodeElement.setAttribute('data-prop-null-prop', 'null');
68+
abodeElement.setAttribute('data-prop-true-prop', 'true');
69+
abodeElement.setAttribute('data-prop-empty-prop', '');
70+
abodeElement.setAttribute(
71+
'data-prop-json-prop',
72+
'{"id": 12345, "product": "keyboard", "variant": {"color": "blue"}}'
73+
);
6574

6675
const props = getElementProps(abodeElement);
6776

68-
expect(props).toEqual({ testProp: 'testPropValue' });
77+
expect(props).toEqual({
78+
testProp: 'testPropValue',
79+
numberProp: 12345,
80+
nullProp: null,
81+
trueProp: true,
82+
emptyProp: '',
83+
jsonProp: { id: 12345, product: 'keyboard', variant: { color: 'blue' } },
84+
});
85+
});
86+
it('getElementProps parses JSON', () => {
87+
fc.assert(
88+
fc.property(fc.jsonObject({ maxDepth: 10 }), data => {
89+
const abodeElement = document.createElement('div');
90+
abodeElement.setAttribute('data-prop-test-prop', JSON.stringify(data));
91+
const props = getElementProps(abodeElement);
92+
expect(props.testProp).toEqual(data);
93+
})
94+
);
6995
});
7096

7197
it('setAttributes', () => {

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2612,6 +2612,13 @@ extsprintf@^1.2.0:
26122612
resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.4.0.tgz#e2689f8f356fad62cca65a3a91c5df5f9551692f"
26132613
integrity sha1-4mifjzVvrWLMplo6kcXfX5VRaS8=
26142614

2615+
fast-check@^2.13.0:
2616+
version "2.13.0"
2617+
resolved "https://registry.yarnpkg.com/fast-check/-/fast-check-2.13.0.tgz#92a50a6a39b58760d4b0b52b12f98f28a9f020f6"
2618+
integrity sha512-IOfzKm/SCA+jpUEgAfqAuxHYPmgtmpnnwljQmYPRGrqYczcTKApXKHza/SNxFxYkecWfZilYa0DJdBvqz1bcSw==
2619+
dependencies:
2620+
pure-rand "^4.1.1"
2621+
26152622
fast-deep-equal@^3.1.1:
26162623
version "3.1.3"
26172624
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz#3a7d56b559d6cbc3eb512325244e619a65c6c525"
@@ -4706,6 +4713,11 @@ punycode@^2.1.0, punycode@^2.1.1:
47064713
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec"
47074714
integrity sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==
47084715

4716+
pure-rand@^4.1.1:
4717+
version "4.1.2"
4718+
resolved "https://registry.yarnpkg.com/pure-rand/-/pure-rand-4.1.2.tgz#cbad2a3e3ea6df0a8d80d8ba204779b5679a5205"
4719+
integrity sha512-uLzZpQWfroIqyFWmX/pl0OL2JHJdoU3dbh0dvZ25fChHFJJi56J5oQZhW6QgbT2Llwh1upki84LnTwlZvsungA==
4720+
47094721
qs@~6.5.2:
47104722
version "6.5.2"
47114723
resolved "https://registry.yarnpkg.com/qs/-/qs-6.5.2.tgz#cb3ae806e8740444584ef154ce8ee98d403f3e36"

0 commit comments

Comments
 (0)