Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions packages/helix-shared-indexer/src/index-resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,22 @@ const helpers = {
return [text.split(/\s+/g).slice(start, end).join(' ')];
},
characters,
replace: (s, searchValue, replaceValue) => [s.replace(searchValue, replaceValue)],
replaceAll: (s, searchValue, replaceValue) => [s.replaceAll(searchValue, replaceValue)],
replace: (s, searchValue, replaceValue) => {
if (Array.isArray(s)) {
return s
.filter((item) => item != null)
.map((item) => String(item).replace(searchValue, replaceValue));
}
Comment on lines +129 to +133

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me as well, just one thing: should we rather filter items that are null (or undefined) instead of returning them in the array?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done - nulls are now filtered out before mapping; updated the test expectations accordingly.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, and using the loose un-equality, this will filter out both null and undefined array values, which is fine for me.

return [s.replace(searchValue, replaceValue)];
},
replaceAll: (s, searchValue, replaceValue) => {
if (Array.isArray(s)) {
return s
.filter((item) => item != null)
.map((item) => String(item).replaceAll(searchValue, replaceValue));
}
return [s.replaceAll(searchValue, replaceValue)];
},
};

function evaluate(expression, context) {
Expand Down
20 changes: 20 additions & 0 deletions packages/helix-shared-indexer/test/index-resource.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@ indices:
select: none
value: |
replaceAll(path, 'ab', 'z')
replace-attribute:
select: head > meta[name="x-source-hash"]
value: |
replace(attribute(el, 'content'), 'JJYxCM1NDG4', 'PREFIX')
replaceAll-attribute:
select: head > meta[name="x-source-hash"]
value: |
replaceAll(attribute(el, 'content'), 'J', 'j')
replace-attribute-missing:
select: head > meta[name="x-source-hash"]
value: |
replace(attribute(el, 'data-missing'), 'x', 'y')
replaceAll-attribute-missing:
select: head > meta[name="x-source-hash"]
value: |
replaceAll(attribute(el, 'data-missing'), 'x', 'y')
paragraph:
select: main > div:nth-of-type(5)
value: |
Expand Down Expand Up @@ -232,6 +248,10 @@ describe('Index Resource Tests', () => {
'parse-timestamp': 1614007680,
'replace-path': '/zc/de/ab/fg/abcd',
'replaceAll-path': '/zc/de/z/fg/zcd',
'replace-attribute': 'PREFIXahJm9f',
'replaceAll-attribute': 'jjYxCM1NDG4ahjm9f',
'replace-attribute-missing': '',
'replaceAll-attribute-missing': '',
'second-alternate': 'before<br>after',
sourceHash: 'JJYxCM1NDG4ahJm9f',
title: 'I feel good',
Expand Down