Skip to content

Commit

Permalink
fix: match should enforce array input (#835)
Browse files Browse the repository at this point in the history
* fix: match should enforce array input

* chore(test): fix expected output
  • Loading branch information
dominique-pfister authored Aug 24, 2023
1 parent db253b2 commit 91874c9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
6 changes: 6 additions & 0 deletions packages/helix-shared-indexer/src/index-resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ const helpers = {
// todo: maybe base on function ?
const result = [];
const regex = new RegExp(re, 'g');

if (!Array.isArray(elements)) {
// eslint-disable-next-line no-param-reassign
elements = [elements];
}
elements.forEach((el) => {
let m;
const content = typeof el === 'string' ? el : toText(el);
Expand All @@ -77,6 +82,7 @@ const helpers = {
return [text.split(/\s+/g).slice(start, end).join(' ')];
},
replace: (s, searchValue, replaceValue) => [s.replace(searchValue, replaceValue)],
replaceAll: (s, searchValue, replaceValue) => [s.replaceAll(searchValue, replaceValue)],
};

function evaluate(expression, context) {
Expand Down
18 changes: 14 additions & 4 deletions packages/helix-shared-indexer/test/index-resource.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,18 @@ indices:
select: head > meta[name="x-source-hash"]
value: |
attribute(el, 'content')
external-path:
first-segment:
select: none
value: |
replace(path, '/test/specs/', '/')
match(path, '^/([^/]+)/')
replace-path:
select: none
value: |
replace(path, 'ab', 'z')
replaceAll-path:
select: none
value: |
replaceAll(path, 'ab', 'z')
paragraph:
select: main > div:nth-of-type(5)
value: |
Expand Down Expand Up @@ -151,14 +159,13 @@ describe('Index Resource Tests', () => {
it('indexing a resource', async () => {
const config = await new IndexConfig().withSource(INDEX).init();
const headers = new Headers({ 'last-modified': 'Mon, 22 Feb 2021 15:28:00 GMT' });
const record = indexResource('/path', { body: BODY, headers }, config.indices[0], console);
const record = indexResource('/abc/de/ab/fg/abcd', { body: BODY, headers }, config.indices[0], console);
assert.deepEqual(record, {
author: 'Max',
'bad-selector': '',
'call-unknown-function': '',
'condition-unsupported': '',
date: 44313,
'external-path': '/path',
'last-modified': 1614007680,
'last-modified-raw': 'Mon, 22 Feb 2021 15:28:00 GMT',
'match-simple': '',
Expand All @@ -167,6 +174,9 @@ describe('Index Resource Tests', () => {
'missing-header': '',
'non-array-words': 'Mon, 22 Feb 2021 15:28:00 GMT',
paragraph: '\n <p>consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua</p>\n ',
'first-segment': 'abc',
'replace-path': '/zc/de/ab/fg/abcd',
'replaceAll-path': '/zc/de/z/fg/zcd',
sourceHash: 'JJYxCM1NDG4ahJm9f',
teaser: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua Ut',
title: 'I feel good',
Expand Down

0 comments on commit 91874c9

Please sign in to comment.