diff --git a/Logic/Tags/ComparingTag.ts b/Logic/Tags/ComparingTag.ts index 67ac5a9cf..20d5ffbde 100644 --- a/Logic/Tags/ComparingTag.ts +++ b/Logic/Tags/ComparingTag.ts @@ -31,6 +31,15 @@ export default class ComparingTag implements TagsFilter { return false; } + /** + * Checks if the properties match + * + * const t = new ComparingTag("key", (x => Number(x) < 42)) + * t.matchesProperties({key: 42}) // => false + * t.matchesProperties({key: 41}) // => true + * t.matchesProperties({key: 0}) // => true + * t.matchesProperties({differentKey: 42}) // => false + */ matchesProperties(properties: any): boolean { return this._predicate(properties[this._key]); } diff --git a/Logic/Tags/Tag.ts b/Logic/Tags/Tag.ts index 7189bb73c..9bc1b1eba 100644 --- a/Logic/Tags/Tag.ts +++ b/Logic/Tags/Tag.ts @@ -35,6 +35,8 @@ export class Tag extends TagsFilter { * isEmpty.matchesProperties({"key": ""}) // => true * isEmpty.matchesProperties({"other_key": ""}) // => true * isEmpty.matchesProperties({"other_key": "value"}) // => true + * isEmpty.matchesProperties({"key": undefined}) // => true + * */ matchesProperties(properties: any): boolean { const foundValue = properties[this.key] diff --git a/UI/OpeningHours/OpeningHours.ts b/UI/OpeningHours/OpeningHours.ts index 94f42b50f..ff98cfe04 100644 --- a/UI/OpeningHours/OpeningHours.ts +++ b/UI/OpeningHours/OpeningHours.ts @@ -220,6 +220,23 @@ export class OH { } } + /** + * Converts an OH-syntax rule into an object + * + * + * const rules = OH.ParsePHRule("PH 12:00-17:00") + * rules.mode // => " " + * rules.start // => "12:00" + * rules.end // => "17:00" + * + * OH.ParseRule("PH 12:00-17:00") // => null + * OH.ParseRule("Th[-1] off") // => null + * + * const rules = OH.Parse("24/7"); + * rules.length // => 7 + * rules[0].startHour // => 0 + * OH.ToString(rules) // => "24/7" + */ public static ParseRule(rule: string): OpeningHour[] { try { if (rule.trim() == "24/7") { diff --git a/UI/i18n/Translations.ts b/UI/i18n/Translations.ts index dde9601d3..4f23c8908 100644 --- a/UI/i18n/Translations.ts +++ b/UI/i18n/Translations.ts @@ -43,6 +43,14 @@ export default class Translations { return new Translation(t, context); } + /** + * 'Wrap Translation': given an object containing translations OR a string, returns a translation object + * + * const json: any = {"en": "English", "nl": "Nederlands"}; + * const translation = Translations.WT(new Translation(json)); + * translation.textFor("en") // => "English" + * translation.textFor("nl") // => "Nederlands" + */ public static WT(s: string | Translation): Translation { if (s === undefined || s === null) { return undefined; diff --git a/Utils.ts b/Utils.ts index c3b05a61e..96a5e0ef7 100644 --- a/Utils.ts +++ b/Utils.ts @@ -109,7 +109,22 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be return "" + i; } - public static Round(i: number) { + /** + * Converts a number to a string with one number after the comma + * + * Utils.Round(15) // => "15.0" + * Utils.Round(1) // => "1.0" + * Utils.Round(1.5) // => "1.5" + * Utils.Round(0.5) // => "0.5" + * Utils.Round(1.6) // => "1.6" + * Utils.Round(-15) // => "-15.0" + * Utils.Round(-1) // => "-1.0" + * Utils.Round(-1.5) // => "-1.5" + * Utils.Round(-0.5) // => "-0.5" + * Utils.Round(-1.6) // => "-1.6" + * Utils.Round(-1531.63543) // =>"-1531.6" + */ + public static Round(i: number): string { if (i < 0) { return "-" + Utils.Round(-i); } diff --git a/testLegacy/Tag.spec.ts b/testLegacy/Tag.spec.ts index 18dbde4ce..90b526ed3 100644 --- a/testLegacy/Tag.spec.ts +++ b/testLegacy/Tag.spec.ts @@ -1,9 +1,6 @@ -import {Utils} from "../Utils"; import {equal} from "assert"; import T from "./TestHelper"; import Locale from "../UI/i18n/Locale"; -import Translations from "../UI/i18n/Translations"; -import {Translation} from "../UI/i18n/Translation"; import {OH, OpeningHour} from "../UI/OpeningHours/OpeningHours"; import {Tag} from "../Logic/Tags/Tag"; import {And} from "../Logic/Tags/And"; @@ -97,15 +94,6 @@ export default class TagSpec extends T { equal(false, t2.isEquivalent(t0)) equal(false, t2.isEquivalent(t1)) })], - ["Parse translation map", (() => { - - const json: any = {"en": "English", "nl": "Nederlands"}; - const translation = Translations.WT(new Translation(json)); - Locale.language.setData("en"); - equal(translation.txt, "English"); - Locale.language.setData("nl"); - equal(translation.txt, "Nederlands"); - })], ["Parse tag rendering", (() => { Locale.language.setData("nl"); const tr = new TagRenderingConfig({ @@ -130,13 +118,6 @@ export default class TagSpec extends T { equal(undefined, tr.GetRenderValue({"foo": "bar"})); })], - [ - "Empty match test", - () => { - const t = new Tag("key", ""); - equal(false, t.matchesProperties({"key": "somevalue"})) - } - ], [ "Test not with overpass", () => { @@ -385,42 +366,6 @@ export default class TagSpec extends T { ])); equal(rules, "Tu 23:00-00:00"); }], - ["OH 24/7", () => { - const rules = OH.Parse("24/7"); - equal(rules.length, 7); - equal(rules[0].startHour, 0); - const asStr = OH.ToString(rules); - equal(asStr, "24/7"); - }], - ["OH Th[-1] off", () => { - const rules = OH.ParseRule("Th[-1] off"); - equal(rules, null); - }], - ["OHNo parsePH 12:00-17:00", () => { - const rules = OH.ParseRule("PH 12:00-17:00"); - equal(rules, null); - }], - ["OH Parse PH 12:00-17:00", () => { - const rules = OH.ParsePHRule("PH 12:00-17:00"); - equal(rules.mode, " "); - equal(rules.start, "12:00") - equal(rules.end, "17:00") - }], - ["Round", () => { - equal(Utils.Round(15), "15.0") - equal(Utils.Round(1), "1.0") - equal(Utils.Round(1.5), "1.5") - equal(Utils.Round(0.5), "0.5") - equal(Utils.Round(1.6), "1.6") - - equal(Utils.Round(-15), "-15.0") - equal(Utils.Round(-1), "-1.0") - equal(Utils.Round(-1.5), "-1.5") - equal(Utils.Round(-0.5), "-0.5") - equal(Utils.Round(-1.6), "-1.6") - - } - ], ["Regression", () => { const config = {