Skip to content

Commit 31c4131

Browse files
Use underscore in locals.js
1 parent c2573f6 commit 31c4131

File tree

5 files changed

+102
-77
lines changed

5 files changed

+102
-77
lines changed

examples/dist/demo.html

+46-7
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
<div class="row">
4949
<div class="col-lg-8">
5050
<h1 class="display-1 fw-bold mb-4">Demo</h1>
51-
<div>
51+
<div something="alocal">
5252
<h1>anObjectDefault</h1>
5353
<p>
5454
<strong>first</strong>
@@ -68,6 +68,18 @@ <h1>anObjectOverride</h1>
6868
: Fourth override item
6969
</p>
7070
<h1>anObjectMerged</h1>
71+
<p>
72+
<strong>first</strong>
73+
: First merged item
74+
</p>
75+
<p>
76+
<strong>second</strong>
77+
: Second merged item
78+
</p>
79+
<p>
80+
<strong>anObjectMerged1</strong>
81+
: {"first":"First1 merged item","second":"Second1 merged item","anObjectMerged2":{"first":"First2 merged item","second":"Second2 merged item"},"third":"Third merged item (UPDATED?)","fourth1":"Fourth merged item"}
82+
</p>
7183
<p>
7284
<strong>third</strong>
7385
: Third merged item
@@ -76,17 +88,21 @@ <h1>anObjectMerged</h1>
7688
<strong>fourth</strong>
7789
: Fourth merged item
7890
</p>
79-
<p>
80-
<strong>anObjectMerged1</strong>
81-
: {"third":"Third merged item (UPDATED?)","fourth1":"Fourth merged item"}
82-
</p>
8391
<h1>aStringDefault</h1>
8492
<p>My default string</p>
8593
<h1>aStringOverride</h1>
8694
<p>My override string changed</p>
8795
<hr>
8896
<h1>post</h1>
8997
My post title My post body
98+
<p>
99+
<strong>1</strong>
100+
: My comment 1
101+
</p>
102+
<p>
103+
<strong>2</strong>
104+
: My comment 2
105+
</p>
90106
<p>
91107
<strong>1</strong>
92108
: My comment 1 new
@@ -98,10 +114,18 @@ <h1>post</h1>
98114
<h1>anArrayOfObjects</h1>
99115
<p>
100116
<strong>0</strong>
101-
: {"id":1,"title":"My new title 1"}
117+
: {"id":1,"title":"My default title 1"}
102118
</p>
103119
<p>
104120
<strong>1</strong>
121+
: {"id":2,"title":"My default title 2"}
122+
</p>
123+
<p>
124+
<strong>2</strong>
125+
: {"id":1,"title":"My new title 1"}
126+
</p>
127+
<p>
128+
<strong>3</strong>
105129
: {"id":2,"title":"My new title 2","somethingnew":"New"}
106130
</p>
107131
<h1>anArray</h1>
@@ -111,12 +135,27 @@ <h1>anArray</h1>
111135
</p>
112136
<p>
113137
<strong>1</strong>
114-
: third
138+
: second
115139
</p>
116140
<p>
117141
<strong>2</strong>
142+
: first
143+
</p>
144+
<p>
145+
<strong>3</strong>
146+
: third
147+
</p>
148+
<p>
149+
<strong>4</strong>
118150
: fourth
119151
</p>
152+
<p>
153+
<strong>5</strong>
154+
: ten
155+
</p>
156+
<hr>
157+
<h1>aComputed</h1>
158+
<p>Yes</p>
120159
</div>
121160
</div>
122161
</div>

examples/src/components/demo.html

+7-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
title: "My default title 2"
4747
}
4848
],
49-
anArray: ['first', 'second']
49+
anArray: ['first', 'second'],
50+
aComputed: locals.aComputed === true ? 'Yes' : 'No',
5051
};
5152
</script>
5253
<div>
@@ -88,4 +89,9 @@ <h1>anArray</h1>
8889
<each loop="o,i in anArray">
8990
<p><strong>{{ i }}</strong>: {{ o }}</p>
9091
</each>
92+
93+
<hr>
94+
95+
<h1>aComputed</h1>
96+
<p>{{ aComputed }}</p>
9197
</div>

examples/src/pages/demo.html

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ <h1 class="display-1 fw-bold mb-4">Demo</h1>
1313
merge:anArrayOfObjects='[ { "id": 1, "title": "My new title 1" }, { "id": 2, "title": "My new title 2", "somethingnew": "New" } ]'
1414

1515
merge:anArray='[ "first", "third", "fourth" ]'
16+
17+
computed:aComputed="true"
18+
19+
merge:locals='{ "something": "alocal", "anArray": [ "ten" ] }'
1620
></x-demo>
1721
</div>
1822
</div>

src/index.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ module.exports = (options = {}) => tree => {
103103
function processTree(options) {
104104
const filledSlots = {};
105105

106-
let processCounter = 0;
106+
// let processCounter = 0;
107107

108108
return function (tree) {
109109
if (options.plugins.length > 0) {
@@ -115,10 +115,6 @@ function processTree(options) {
115115
currentNode.attrs = {};
116116
}
117117

118-
if (!currentNode.attrs[options.attribute]) {
119-
console.log(currentNode.tag);
120-
}
121-
122118
const componentFile = currentNode.attrs[options.attribute] || findPathFromTag(currentNode.tag, options);
123119

124120
if (!componentFile) {
@@ -133,7 +129,7 @@ function processTree(options) {
133129
return currentNode;
134130
}
135131

136-
console.log(`${++processCounter}) Processing component ${componentPath}`);
132+
// console.log(`${++processCounter}) Processing component ${componentPath}`);
137133

138134
let nextNode = parser(readFileSync(componentPath, 'utf8'));
139135

src/locals.js

+43-63
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,8 @@
22

33
const merge = require('deepmerge');
44
const scriptDataLocals = require('posthtml-expressions/lib/locals');
5-
const {pick, extend} = require('underscore');
6-
// const {inspect} = require('util');
7-
//
8-
// const debug = true;
9-
//
10-
// const log = (object, what, method) => {
11-
// if (debug === true || method === debug) {
12-
// console.log(what, inspect(object, false, null, true));
13-
// }
14-
// };
5+
const {pick, extend, keys, defaults, each} = require('underscore');
6+
const attributeTypes = ['merge', 'computed', 'aware'];
157

168
/**
179
* Parse locals from attributes, globals and via script
@@ -25,81 +17,69 @@ const {pick, extend} = require('underscore');
2517
module.exports = (currentNode, nextNode, slotContent, options) => {
2618
let attributes = {...currentNode.attrs};
2719

28-
const merged = [];
29-
const computed = [];
30-
const aware = [];
31-
32-
Object.keys(attributes).forEach(attributeName => {
33-
const newAttributeName = attributeName
34-
.replace('merge:', '')
35-
.replace('computed:', '')
36-
.replace('aware:', '');
37-
38-
switch (true) {
39-
case attributeName.startsWith('merge:'):
40-
attributes[newAttributeName] = attributes[attributeName];
41-
delete attributes[attributeName];
42-
merged.push(newAttributeName);
43-
break;
44-
45-
case attributeName.startsWith('computed:'):
46-
attributes[newAttributeName] = attributes[attributeName];
47-
delete attributes[attributeName];
48-
computed.push(newAttributeName);
49-
break;
20+
const attributesByTypeName = {};
21+
each(attributeTypes, type => {
22+
attributesByTypeName[type] = [];
23+
});
5024

51-
case attributeName.startsWith('aware:'):
52-
attributes[newAttributeName] = attributes[attributeName];
53-
delete attributes[attributeName];
54-
aware.push(newAttributeName);
55-
break;
25+
each(attributes, (value, key, attrs) => {
26+
let newKey = key;
27+
each(attributeTypes, type => {
28+
if (key.startsWith(`${type}:`)) {
29+
newKey = newKey.replace(`${type}:`, '');
30+
attributesByTypeName[type].push(newKey);
31+
}
32+
});
5633

57-
default: break;
34+
if (newKey !== key) {
35+
attrs[newKey] = value;
36+
delete attrs[key];
5837
}
5938
});
6039

6140
// Parse JSON attributes
62-
Object.keys(attributes).forEach(attributeName => {
41+
each(attributes, (value, key, attrs) => {
6342
try {
64-
const parsed = JSON.parse(attributes[attributeName]);
65-
if (attributeName === 'locals') {
66-
if (merged.includes(attributeName)) {
67-
attributes = merge(attributes, parsed);
68-
merged.splice(merged.indexOf('locals'), 1);
69-
} else {
70-
// Override with merge see https://www.npmjs.com/package/deepmerge#arraymerge-example-overwrite-target-array
71-
Object.assign(attributes, parsed);
72-
}
73-
} else {
74-
attributes[attributeName] = parsed;
75-
}
43+
attrs[key] = JSON.parse(value);
7644
} catch {}
7745
});
7846

79-
delete attributes.locals;
47+
// Merge or extend attribute locals
48+
if (attributes.locals) {
49+
if (attributesByTypeName.merge.includes('locals')) {
50+
attributesByTypeName.merge.splice(attributesByTypeName.merge.indexOf('locals'), 1);
51+
attributes = merge(attributes, attributes.locals);
52+
} else {
53+
extend(attributes, attributes.locals);
54+
}
55+
56+
delete attributes.locals;
57+
}
8058

8159
// Merge with global
8260
attributes = merge(options.expressions.locals, attributes);
8361

8462
// Retrieve default locals from <script props> and merge with attributes
8563
const {locals} = scriptDataLocals(nextNode, {localsAttr: options.localsAttr, removeScriptLocals: true, locals: {...attributes, $slots: slotContent}});
8664

87-
// Merge or overrides props and attributes
8865
if (locals) {
89-
if (merged.length > 0) {
90-
extend(attributes, merge(pick(locals, merged), pick(attributes, merged)));
66+
// Merge attributes
67+
if (attributesByTypeName.merge.length > 0) {
68+
extend(attributes, merge(pick(locals, attributesByTypeName.merge), pick(attributes, attributesByTypeName.merge)));
9169
}
9270

93-
// Override attributes with props when is computed or attribute is not defined
94-
Object.keys(locals).forEach(attributeName => {
95-
if (computed.includes(attributeName) || typeof attributes[attributeName] === 'undefined') {
96-
attributes[attributeName] = locals[attributeName];
97-
}
98-
});
71+
// Override attributes with computed locals
72+
if (attributesByTypeName.computed.length > 0) {
73+
extend(attributes, pick(locals, keys(pick(attributes, attributesByTypeName.computed))));
74+
}
75+
76+
// Set attributes not defined
77+
defaults(attributes, locals);
9978
}
10079

101-
if (aware.length > 0) {
102-
options.aware = Object.fromEntries(Object.entries(attributes).filter(([attributeName]) => aware.includes(attributeName)));
80+
// Set aware attributes
81+
if (attributesByTypeName.aware.length > 0) {
82+
options.aware = pick(attributes, attributesByTypeName.aware);
10383
}
10484

10585
return {attributes, locals};

0 commit comments

Comments
 (0)