diff --git a/biome.json b/biome.json index 522c2fc..6807b9f 100644 --- a/biome.json +++ b/biome.json @@ -46,7 +46,8 @@ "noDynamicNamespaceImportAccess": "off" }, "suspicious": { - "noAssignInExpressions": "off" + "noAssignInExpressions": "off", + "useIterableCallbackReturn": "off" } } } diff --git a/lib/caching/index.js b/lib/caching/index.js index 0449404..d594a43 100644 --- a/lib/caching/index.js +++ b/lib/caching/index.js @@ -13,3 +13,21 @@ export function getStrategy(caching, object) { } return caching.evaluate(object); } + +function getValues(caching, values = []) { + if (typeof caching === 'string') { + values.push(caching); + return values; + } + if (Array.isArray(caching)) { + caching.forEach(item => getValues(item, values)); + } + return values; +} + +export function isCacheable(caching) { + if (!caching) { + return; + } + return getValues(caching).some(key => key.startsWith('cache-')); +} diff --git a/lib/map-style.js b/lib/map-style.js index 9ae1545..67f56e1 100644 --- a/lib/map-style.js +++ b/lib/map-style.js @@ -1,3 +1,4 @@ +import { isCacheable } from './caching/index.js'; import loadGlyphs from './glyphs.js'; import { init as initLayers, reset as resetLayers } from './layers.js'; import loader from './loader.js'; @@ -114,14 +115,3 @@ function cacheableOnly(style) { return cached; } - -function isCacheable(caching) { - if (!caching) { - return; - } - const keys = Object.keys(caching); - if (caching.default) { - keys.push(caching.default); - } - return keys.some(key => key.startsWith('cache-')); -} diff --git a/test/caching.js b/test/caching.js index 311944f..93e8053 100644 --- a/test/caching.js +++ b/test/caching.js @@ -1,6 +1,6 @@ import assert from 'node:assert/strict'; import test from 'node:test'; -import { getStrategy, initCaching } from '../lib/caching/index.js'; +import { getStrategy, initCaching, isCacheable } from '../lib/caching/index.js'; test('caching default', () => { const caching = initCaching('do-nothing'); @@ -72,3 +72,29 @@ test('caching multiple fields', () => { strategy = getStrategy(caching, {}); assert.equal(strategy, 'do-nothing'); }); + +test('isCacheable', () => { + assert.equal(isCacheable('do-nothing'), false); + assert.equal( + isCacheable([ + 'case', + ['all', ['to-boolean', ['global-state', 'field1']], ['to-boolean', ['global-state', 'field2']]], + 'network-only', + 'do-nothing' + ]), + false + ); + + assert.equal(isCacheable('cache-only'), true); + assert.equal( + isCacheable([ + 'case', + ['to-boolean', ['global-state', 'field1']], + 'network-only', + ['to-boolean', ['global-state', 'field2']], + 'cache-only', + 'do-nothing' + ]), + true + ); +});