Skip to content
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- A widget settings Fetch marks a blocked private address as a warning, the
same as a badge test, instead of a hard error.

- A badge extract path ending in `count` now reads a JSON field of that
name. It only counts an array when the value is an array.

## [1.6.0] - 2026-08-13

### Added
Expand Down
5 changes: 4 additions & 1 deletion api/src/badge-extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ function extractPath(obj, dotPath) {
let cur = obj;
for (const seg of segments) {
if (cur == null) return undefined;
if (seg === '$count' || seg === 'count') return Array.isArray(cur) ? cur.length : undefined;
if (seg === '$count') return Array.isArray(cur) ? cur.length : undefined;
/* `count` is the array-length token only on an array. On an object it is
the field of that name. */
if (seg === 'count' && Array.isArray(cur)) return cur.length;
const fM = seg.match(filterRe);
if (fM) {
const [, field, rawVal] = fM;
Expand Down
6 changes: 6 additions & 0 deletions api/test/badge-extract.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ test('extractPath resolves $count and count on arrays', () => {
assert.equal(extractPath({ x: 5 }, 'x.count'), undefined); // not an array
});

test('extractPath reads a field named count on an object', () => {
assert.equal(extractPath({ count: 99 }, 'count'), 99);
assert.equal(extractPath({ queue: { count: 4 } }, 'queue.count'), 4);
assert.equal(computeBadgeValue({ count: 99 }, { extract: 'count' }), 99);
});

test('extractPath filters an array by a boolean field then counts', () => {
const data = { list: [{ on: true }, { on: false }, { on: true }] };
assert.equal(extractPath(data, 'list.filter(on==true).count'), 2);
Expand Down
Loading