From da8e206cd00212e19d7783a2de0153b42bc406a1 Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Mon, 22 Jul 2024 11:55:35 +0200 Subject: [PATCH 1/3] chore: pass biome on utils unittests --- umap/static/umap/unittests/utils.js | 220 ++++++++++++++-------------- 1 file changed, 113 insertions(+), 107 deletions(-) diff --git a/umap/static/umap/unittests/utils.js b/umap/static/umap/unittests/utils.js index 2e352dff..2726ecf7 100644 --- a/umap/static/umap/unittests/utils.js +++ b/umap/static/umap/unittests/utils.js @@ -8,137 +8,137 @@ const { assert, expect } = pkg import { JSDOM } from 'jsdom' global.JSDOM = JSDOM -describe('Utils', function () { - describe('#toHTML()', function () { - it('should handle title', function () { +describe('Utils', () => { + describe('#toHTML()', () => { + it('should handle title', () => { assert.equal(Utils.toHTML('# A title'), '

A title

') }) - it('should handle title followed by text', function () { + it('should handle title followed by text', () => { assert.equal(Utils.toHTML('# A title\nSome text.'), '

A title

Some text.') }) - it('should handle title in the middle of the content', function () { + it('should handle title in the middle of the content', () => { assert.equal(Utils.toHTML('A phrase\n## A title'), 'A phrase\n
A title
') }) - it('should handle hr', function () { + it('should handle hr', () => { assert.equal(Utils.toHTML('---'), '
') }) - it('should handle bold', function () { + it('should handle bold', () => { assert.equal(Utils.toHTML('Some **bold**'), 'Some bold') }) - it('should handle italic', function () { + it('should handle italic', () => { assert.equal(Utils.toHTML('Some *italic*'), 'Some italic') }) - it('should handle links without formatting', function () { + it('should handle links without formatting', () => { assert.equal( Utils.toHTML('A simple http://osm.org link'), 'A simple http://osm.org link' ) }) - it('should handle simple link in title', function () { + it('should handle simple link in title', () => { assert.equal( Utils.toHTML('# http://osm.org'), '

http://osm.org

' ) }) - it('should handle links with url parameter', function () { + it('should handle links with url parameter', () => { assert.equal( Utils.toHTML('A simple https://osm.org/?url=https%3A//anotherurl.com link'), 'A simple https://osm.org/?url=https%3A//anotherurl.com link' ) }) - it('should handle simple link inside parenthesis', function () { + it('should handle simple link inside parenthesis', () => { assert.equal( Utils.toHTML('A simple link (http://osm.org)'), 'A simple link (http://osm.org)' ) }) - it('should handle simple link with formatting', function () { + it('should handle simple link with formatting', () => { assert.equal( Utils.toHTML('A simple [[http://osm.org]] link'), 'A simple http://osm.org link' ) }) - it('should handle simple link with formatting and content', function () { + it('should handle simple link with formatting and content', () => { assert.equal( Utils.toHTML('A simple [[http://osm.org|link]]'), 'A simple link' ) }) - it('should handle simple link followed by a carriage return', function () { + it('should handle simple link followed by a carriage return', () => { assert.equal( Utils.toHTML('A simple link http://osm.org\nAnother line'), 'A simple link http://osm.org\nAnother line' ) }) - it('should handle target option', function () { + it('should handle target option', () => { assert.equal( Utils.toHTML('A simple http://osm.org link', { target: 'self' }), 'A simple http://osm.org link' ) }) - it('should handle image', function () { + it('should handle image', () => { assert.equal( Utils.toHTML('A simple image: {{http://osm.org/pouet.png}}'), 'A simple image: ' ) }) - it('should handle image without text', function () { + it('should handle image without text', () => { assert.equal( Utils.toHTML('{{http://osm.org/pouet.png}}'), '' ) }) - it('should handle image with width', function () { + it('should handle image with width', () => { assert.equal( Utils.toHTML('A simple image: {{http://osm.org/pouet.png|100}}'), 'A simple image: ' ) }) - it('should handle iframe', function () { + it('should handle iframe', () => { assert.equal( Utils.toHTML('A simple iframe: {{{http://osm.org/pouet.html}}}'), 'A simple iframe:
' ) }) - it('should handle iframe with height', function () { + it('should handle iframe with height', () => { assert.equal( Utils.toHTML('A simple iframe: {{{http://osm.org/pouet.html|200}}}'), 'A simple iframe:
' ) }) - it('should handle iframe with height and width', function () { + it('should handle iframe with height and width', () => { assert.equal( Utils.toHTML('A simple iframe: {{{http://osm.org/pouet.html|200*400}}}'), 'A simple iframe:
' ) }) - it('should handle iframe with height with px', function () { + it('should handle iframe with height with px', () => { assert.equal( Utils.toHTML('A simple iframe: {{{http://osm.org/pouet.html|200px}}}'), 'A simple iframe:
' ) }) - it('should handle iframe with url parameter', function () { + it('should handle iframe with url parameter', () => { assert.equal( Utils.toHTML( 'A simple iframe: {{{https://osm.org/?url=https%3A//anotherurl.com}}}' @@ -147,7 +147,7 @@ describe('Utils', function () { ) }) - it('should handle iframe with height with px', function () { + it('should handle iframe with height with px', () => { assert.equal( Utils.toHTML( 'A double iframe: {{{https://osm.org/pouet}}}{{{https://osm.org/boudin}}}' @@ -156,52 +156,56 @@ describe('Utils', function () { ) }) - it('http link with http link as parameter as variable', function () { + it('http link with http link as parameter as variable', () => { assert.equal( Utils.toHTML('A phrase with a [[http://iframeurl.com?to=http://another.com]].'), 'A phrase with a http://iframeurl.com?to=http://another.com.' ) }) - it('simple bullet points', function () { + it('simple bullet points', () => { assert.equal( Utils.toHTML('* First point\n* Second point\n* Last point'), '' ) }) - it('bullet points with bold and italic', function () { + it('bullet points with bold and italic', () => { assert.equal( - Utils.toHTML('* First *point*\n* Second **point**\n* Last [[https://here.org|point]]'), + Utils.toHTML( + '* First *point*\n* Second **point**\n* Last [[https://here.org|point]]' + ), '' ) }) - it('title followed by bullet points', function () { + it('title followed by bullet points', () => { assert.equal( - Utils.toHTML('## Some title\n* First *point*\n* Second **point**\n* Last [[https://here.org|point]]'), + Utils.toHTML( + '## Some title\n* First *point*\n* Second **point**\n* Last [[https://here.org|point]]' + ), '
Some title
' ) }) }) - describe('#escapeHTML', function () { - it('should escape HTML tags', function () { + describe('#escapeHTML', () => { + it('should escape HTML tags', () => { assert.equal(Utils.escapeHTML(''), '') }) - it('should not escape geo: links', function () { + it('should not escape geo: links', () => { assert.equal(Utils.escapeHTML(''), '') }) - it('should not escape dir and title attributes', function () { + it('should not escape dir and title attributes', () => { assert.equal( Utils.escapeHTML(''), '' ) }) - it('should not escape video tag with dedicated attributes', function () { + it('should not escape video tag with dedicated attributes', () => { assert.equal( Utils.escapeHTML( '' @@ -210,7 +214,7 @@ describe('Utils', function () { ) }) - it('should not escape audio tag with dedicated attributes', function () { + it('should not escape audio tag with dedicated attributes', () => { assert.equal( Utils.escapeHTML( '' @@ -219,38 +223,38 @@ describe('Utils', function () { ) }) - it('should not fail with int value', function () { + it('should not fail with int value', () => { assert.equal(Utils.escapeHTML(25), '25') }) - it('should not fail with null value', function () { + it('should not fail with null value', () => { assert.equal(Utils.escapeHTML(null), '') }) }) - describe('#greedyTemplate', function () { - it('should replace simple props', function () { + describe('#greedyTemplate', () => { + it('should replace simple props', () => { assert.equal( Utils.greedyTemplate('A phrase with a {variable}.', { variable: 'thing' }), 'A phrase with a thing.' ) }) - it('should not fail when missing key', function () { + it('should not fail when missing key', () => { assert.equal( Utils.greedyTemplate('A phrase with a {missing}', {}), 'A phrase with a ' ) }) - it('should process brakets in brakets', function () { + it('should process brakets in brakets', () => { assert.equal( Utils.greedyTemplate('A phrase with a {{{variable}}}.', { variable: 'value' }), 'A phrase with a {{value}}.' ) }) - it('should not process http links', function () { + it('should not process http links', () => { assert.equal( Utils.greedyTemplate('A phrase with a {{{http://iframeurl.com}}}.', { 'http://iframeurl.com': 'value', @@ -259,14 +263,14 @@ describe('Utils', function () { ) }) - it('should not accept dash', function () { + it('should not accept dash', () => { assert.equal( Utils.greedyTemplate('A phrase with a {var-iable}.', { 'var-iable': 'value' }), 'A phrase with a {var-iable}.' ) }) - it('should accept colon', function () { + it('should accept colon', () => { assert.equal( Utils.greedyTemplate('A phrase with a {variable:fr}.', { 'variable:fr': 'value', @@ -275,7 +279,7 @@ describe('Utils', function () { ) }) - it('should accept arobase', function () { + it('should accept arobase', () => { assert.equal( Utils.greedyTemplate('A phrase with a {@variable}.', { '@variable': 'value', @@ -284,7 +288,7 @@ describe('Utils', function () { ) }) - it('should accept space', function () { + it('should accept space', () => { assert.equal( Utils.greedyTemplate('A phrase with a {var iable}.', { 'var iable': 'value', @@ -293,7 +297,7 @@ describe('Utils', function () { ) }) - it('should accept non ascii chars', function () { + it('should accept non ascii chars', () => { assert.equal( Utils.greedyTemplate('A phrase with a {Accessibilité} and {переменная}.', { Accessibilité: 'value', @@ -303,7 +307,7 @@ describe('Utils', function () { ) }) - it('should replace even with ignore if key is found', function () { + it('should replace even with ignore if key is found', () => { assert.equal( Utils.greedyTemplate( 'A phrase with a {variable:fr}.', @@ -314,21 +318,21 @@ describe('Utils', function () { ) }) - it('should keep string when using ignore if key is not found', function () { + it('should keep string when using ignore if key is not found', () => { assert.equal( Utils.greedyTemplate('A phrase with a {variable:fr}.', {}, true), 'A phrase with a {variable:fr}.' ) }) - it('should replace nested variables', function () { + it('should replace nested variables', () => { assert.equal( Utils.greedyTemplate('A phrase with a {fr.var}.', { fr: { var: 'value' } }), 'A phrase with a value.' ) }) - it('should not fail if nested variable is missing', function () { + it('should not fail if nested variable is missing', () => { assert.equal( Utils.greedyTemplate('A phrase with a {fr.var.foo}.', { fr: { var: 'value' }, @@ -337,21 +341,21 @@ describe('Utils', function () { ) }) - it('should not fail with nested variables and no data', function () { + it('should not fail with nested variables and no data', () => { assert.equal( Utils.greedyTemplate('A phrase with a {fr.var.foo}.', {}), 'A phrase with a .' ) }) - it('should handle fallback value if any', function () { + it('should handle fallback value if any', () => { assert.equal( Utils.greedyTemplate('A phrase with a {fr.var.bar|"default"}.', {}), 'A phrase with a default.' ) }) - it('should handle fallback var if any', function () { + it('should handle fallback var if any', () => { assert.equal( Utils.greedyTemplate('A phrase with a {fr.var.bar|fallback}.', { fallback: 'default', @@ -360,14 +364,14 @@ describe('Utils', function () { ) }) - it('should handle multiple fallbacks', function () { + it('should handle multiple fallbacks', () => { assert.equal( Utils.greedyTemplate('A phrase with a {fr.var.bar|try.again|"default"}.', {}), 'A phrase with a default.' ) }) - it('should use the first defined value', function () { + it('should use the first defined value', () => { assert.equal( Utils.greedyTemplate('A phrase with a {fr.var.bar|try.again|"default"}.', { try: { again: 'please' }, @@ -376,7 +380,7 @@ describe('Utils', function () { ) }) - it('should use the first defined value', function () { + it('should use the first defined value', () => { assert.equal( Utils.greedyTemplate('A phrase with a {fr.var.bar|try.again|"default"}.', { try: { again: 'again' }, @@ -386,14 +390,14 @@ describe('Utils', function () { ) }) - it('should support the first example from #820 when translated to final syntax', function () { + it('should support the first example from #820 when translated to final syntax', () => { assert.equal( Utils.greedyTemplate('# {name} ({ele|"-"} m ü. M.)', { name: 'Portalet' }), '# Portalet (- m ü. M.)' ) }) - it('should support the first example from #820 when translated to final syntax when no fallback required', function () { + it('should support the first example from #820 when translated to final syntax when no fallback required', () => { assert.equal( Utils.greedyTemplate('# {name} ({ele|"-"} m ü. M.)', { name: 'Portalet', @@ -403,14 +407,14 @@ describe('Utils', function () { ) }) - it('should support white space in fallback', function () { + it('should support white space in fallback', () => { assert.equal( Utils.greedyTemplate('A phrase with {var|"white space in the fallback."}', {}), 'A phrase with white space in the fallback.' ) }) - it('should support empty string as fallback', function () { + it('should support empty string as fallback', () => { assert.equal( Utils.greedyTemplate( 'A phrase with empty string ("{var|""}") in the fallback.', @@ -420,7 +424,7 @@ describe('Utils', function () { ) }) - it('should support e.g. links as fallback', function () { + it('should support e.g. links as fallback', () => { assert.equal( Utils.greedyTemplate( 'A phrase with {var|"[[https://osm.org|link]]"} as fallback.', @@ -431,17 +435,17 @@ describe('Utils', function () { }) }) - describe('#flattenCoordinates()', function () { - it('should not alter already flat coords', function () { - var coords = [ + describe('#flattenCoordinates()', () => { + it('should not alter already flat coords', () => { + const coords = [ [1, 2], [3, 4], ] assert.deepEqual(Utils.flattenCoordinates(coords), coords) }) - it('should flatten nested coords', function () { - var coords = [ + it('should flatten nested coords', () => { + let coords = [ [ [1, 2], [3, 4], @@ -459,36 +463,36 @@ describe('Utils', function () { assert.deepEqual(Utils.flattenCoordinates(coords), coords[0][0]) }) - it('should not fail on empty coords', function () { - var coords = [] + it('should not fail on empty coords', () => { + const coords = [] assert.deepEqual(Utils.flattenCoordinates(coords), coords) }) }) - describe('#usableOption()', function () { - it('should consider false', function () { + describe('#usableOption()', () => { + it('should consider false', () => { assert.ok(Utils.usableOption({ key: false }, 'key')) }) - it('should consider 0', function () { + it('should consider 0', () => { assert.ok(Utils.usableOption({ key: 0 }, 'key')) }) - it('should not consider undefined', function () { + it('should not consider undefined', () => { assert.notOk(Utils.usableOption({}, 'key')) }) - it('should not consider empty string', function () { + it('should not consider empty string', () => { assert.notOk(Utils.usableOption({ key: '' }, 'key')) }) - it('should consider null', function () { + it('should consider null', () => { assert.ok(Utils.usableOption({ key: null }, 'key')) }) }) - describe('#normalize()', function () { - it('should remove accents', function () { + describe('#normalize()', () => { + it('should remove accents', () => { // French é assert.equal(Utils.normalize('aéroport'), 'aeroport') // American é @@ -496,35 +500,37 @@ describe('Utils', function () { }) }) - describe('#sortFeatures()', function () { - let feat1, feat2, feat3 - before(function () { + describe('#sortFeatures()', () => { + let feat1 + let feat2 + let feat3 + before(() => { feat1 = { properties: {} } feat2 = { properties: {} } feat3 = { properties: {} } }) - it('should sort feature from custom key', function () { + it('should sort feature from custom key', () => { feat1.properties.mykey = '13. foo' feat2.properties.mykey = '7. foo' feat3.properties.mykey = '111. foo' - let features = Utils.sortFeatures([feat1, feat2, feat3], 'mykey') + const features = Utils.sortFeatures([feat1, feat2, feat3], 'mykey') assert.equal(features[0], feat2) assert.equal(features[1], feat1) assert.equal(features[2], feat3) }) - it('should sort feature from multiple keys', function () { + it('should sort feature from multiple keys', () => { feat1.properties.mykey = '13. foo' feat2.properties.mykey = '111. foo' feat3.properties.mykey = '111. foo' feat1.properties.otherkey = 'C' feat2.properties.otherkey = 'B' feat3.properties.otherkey = 'A' - let features = Utils.sortFeatures([feat1, feat2, feat3], 'mykey,otherkey') + const features = Utils.sortFeatures([feat1, feat2, feat3], 'mykey,otherkey') assert.equal(features[0], feat1) assert.equal(features[1], feat3) assert.equal(features[2], feat2) }) - it('should sort feature from custom key reverse', function () { + it('should sort feature from custom key reverse', () => { feat1.properties.mykey = '13. foo' feat2.properties.mykey = '7. foo' feat3.properties.mykey = '111. foo' @@ -533,36 +539,36 @@ describe('Utils', function () { assert.equal(features[1], feat1) assert.equal(features[2], feat2) }) - it('should sort feature from multiple keys with reverse', function () { + it('should sort feature from multiple keys with reverse', () => { feat1.properties.mykey = '13. foo' feat2.properties.mykey = '111. foo' feat3.properties.mykey = '111. foo' feat1.properties.otherkey = 'C' feat2.properties.otherkey = 'B' feat3.properties.otherkey = 'A' - let features = Utils.sortFeatures([feat1, feat2, feat3], 'mykey,-otherkey') + const features = Utils.sortFeatures([feat1, feat2, feat3], 'mykey,-otherkey') assert.equal(features[0], feat1) assert.equal(features[1], feat2) assert.equal(features[2], feat3) }) - it('should sort feature with space first', function () { + it('should sort feature with space first', () => { feat1.properties.mykey = '1 foo' feat2.properties.mykey = '2 foo' feat3.properties.mykey = '1a foo' - let features = Utils.sortFeatures([feat1, feat2, feat3], 'mykey') + const features = Utils.sortFeatures([feat1, feat2, feat3], 'mykey') assert.equal(features[0], feat1) assert.equal(features[1], feat3) assert.equal(features[2], feat2) }) }) - describe('#copyJSON', function () { - it('should actually copy the JSON', function () { - let originalJSON = { some: 'json' } - let returned = Utils.CopyJSON(originalJSON) + describe('#copyJSON', () => { + it('should actually copy the JSON', () => { + const originalJSON = { some: 'json' } + const returned = Utils.CopyJSON(originalJSON) // Change the original JSON - originalJSON['anotherKey'] = 'value' + originalJSON.anotherKey = 'value' // ensure the two aren't the same object assert.notEqual(returned, originalJSON) @@ -570,9 +576,9 @@ describe('Utils', function () { }) }) - describe('#getImpactsFromSchema()', function () { - let getImpactsFromSchema = Utils.getImpactsFromSchema - it('should return an array', function () { + describe('#getImpactsFromSchema()', () => { + const getImpactsFromSchema = Utils.getImpactsFromSchema + it('should return an array', () => { expect(getImpactsFromSchema(['foo'], {})).to.be.an('array') expect(getImpactsFromSchema(['foo'], { foo: {} })).to.be.an('array') expect(getImpactsFromSchema(['foo'], { foo: { impacts: [] } })).to.be.an('array') @@ -581,8 +587,8 @@ describe('Utils', function () { ) }) - it('should return a list of unique impacted values', function () { - let schema = { + it('should return a list of unique impacted values', () => { + const schema = { foo: { impacts: ['A'] }, bar: { impacts: ['A', 'B'] }, baz: { impacts: ['B', 'C'] }, @@ -596,8 +602,8 @@ describe('Utils', function () { 'C', ]) }) - it('should return an empty list if nothing is found', function () { - let schema = { + it('should return an empty list if nothing is found', () => { + const schema = { foo: { impacts: ['A'] }, bar: { impacts: ['A', 'B'] }, baz: { impacts: ['B', 'C'] }, @@ -606,15 +612,15 @@ describe('Utils', function () { assert.deepEqual(getImpactsFromSchema(['bad'], schema), []) }) - it('should return an empty list if the schema key does not exist', function () { - let schema = { + it('should return an empty list if the schema key does not exist', () => { + const schema = { foo: { impacts: ['A'] }, } assert.deepEqual(getImpactsFromSchema(['bad'], schema), []) }) - it('should work if the "impacts" key is not defined', function () { - let schema = { + it('should work if the "impacts" key is not defined', () => { + const schema = { foo: {}, bar: { impacts: ['A'] }, baz: { impacts: ['B'] }, From 6b4b6ce53bc02fb7c45545bbbceb6e101359bb17 Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Mon, 22 Jul 2024 11:56:10 +0200 Subject: [PATCH 2/3] fix: do not consider null as value in greedyTemplate cf #820#issuecomment-2227821746 --- umap/static/umap/js/modules/utils.js | 8 +++++--- umap/static/umap/unittests/utils.js | 10 ++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/umap/static/umap/js/modules/utils.js b/umap/static/umap/js/modules/utils.js index 9b7b11f1..4a6cf680 100644 --- a/umap/static/umap/js/modules/utils.js +++ b/umap/static/umap/js/modules/utils.js @@ -242,10 +242,12 @@ export function greedyTemplate(str, data, ignore) { } for (let i = 0; i < vars.length; i++) { path = vars[i] - if (path.startsWith('"') && path.endsWith('"')) + if (path.startsWith('"') && path.endsWith('"')) { value = path.substring(1, path.length - 1) // static default value. - else value = getValue(data, path.split('.')) - if (value !== undefined) break + } else { + value = getValue(data, path.split('.')) + } + if (value !== undefined && value !== null) break } if (value === undefined) { if (ignore) value = str diff --git a/umap/static/umap/unittests/utils.js b/umap/static/umap/unittests/utils.js index 2726ecf7..b48c56e7 100644 --- a/umap/static/umap/unittests/utils.js +++ b/umap/static/umap/unittests/utils.js @@ -433,6 +433,16 @@ describe('Utils', () => { 'A phrase with [[https://osm.org|link]] as fallback.' ) }) + + it('should not consider null values', () => { + assert.equal( + Utils.greedyTemplate('A phrase with a {foo|fallback}.', { + foo: null, + fallback: 'default', + }), + 'A phrase with a default.' + ) + }) }) describe('#flattenCoordinates()', () => { From a8468cb4fa3f52662b66247afeb23f51073acc21 Mon Sep 17 00:00:00 2001 From: David Larlet <3556+davidbgk@users.noreply.github.com> Date: Mon, 22 Jul 2024 09:58:17 -0400 Subject: [PATCH 3/3] chore: modern JS --- umap/static/umap/js/modules/utils.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/umap/static/umap/js/modules/utils.js b/umap/static/umap/js/modules/utils.js index 4a6cf680..b117cf1a 100644 --- a/umap/static/umap/js/modules/utils.js +++ b/umap/static/umap/js/modules/utils.js @@ -240,8 +240,7 @@ export function greedyTemplate(str, data, ignore) { if (staticFallback !== undefined) { vars.push(staticFallback) } - for (let i = 0; i < vars.length; i++) { - path = vars[i] + for (const path of vars) { if (path.startsWith('"') && path.endsWith('"')) { value = path.substring(1, path.length - 1) // static default value. } else {