From 4aad106611b461ad3c416d78bac48db9d633177a Mon Sep 17 00:00:00 2001 From: onsetsu Date: Thu, 12 Oct 2023 23:00:04 +0200 Subject: [PATCH] utils for mapping entries of objects SQUASHED: AUTO-COMMIT-src-client-lang-lang.js,AUTO-COMMIT-src-components-widgets-ubg-cards.js,AUTO-COMMIT-test-lang-test.js, --- src/client/lang/lang.js | 26 ++++++++++++ src/components/widgets/ubg-cards.js | 52 +++++++++++++---------- test/lang-test.js | 65 +++++++++++++++++++++++++++++ 3 files changed, 120 insertions(+), 23 deletions(-) diff --git a/src/client/lang/lang.js b/src/client/lang/lang.js index d67d5555d..6c0343320 100644 --- a/src/client/lang/lang.js +++ b/src/client/lang/lang.js @@ -54,6 +54,12 @@ extendFromLodash(Object.prototype, [ 'toPairs' ]); +function mapEntries(object, iterator) { + const entries = Object.entries(object); + const mapped = entries.map(iterator); + return Object.fromEntries(mapped); +} + extend(Object.prototype, { deepProperty(paths) { if (Array.isArray(paths)) { @@ -86,6 +92,26 @@ extend(Object.prototype, { this.__proto__ = NewClass.prototype; }, + mapKeys(iterator) { + return mapEntries(this, ([key, value]) => [iterator(key, value, this), value]); + }, + + mapValues(iterator) { + return mapEntries(this, ([key, value]) => [key, iterator(value, key, this)]); + }, + + mapEntries(iterator) { + return mapEntries(this, ([key, value]) => iterator([key, value], this)); + }, + + mapKeysToEntries(iterator) { + return mapEntries(this, ([key, value]) => iterator(key, value, this)); + }, + + mapValuesToEntries(iterator) { + return mapEntries(this, ([key, value]) => iterator(value, key, this)); + }, + }); /*MD diff --git a/src/components/widgets/ubg-cards.js b/src/components/widgets/ubg-cards.js index ca532e7c7..3c730eedc 100644 --- a/src/components/widgets/ubg-cards.js +++ b/src/components/widgets/ubg-cards.js @@ -1313,7 +1313,7 @@ ${smallElementIcon(others[2], lively.pt(11, 7))} printedRules = printedRules.replace(/(fire|water|earth|wind|gray)/gmi, function replacer(match, pElement, offset, string, groups) { return element(pElement); }); - printedRules = printedRules.replace(/(\d+|\*|d+\*|\d+x|x|\b)VP\b/gmi, function replacer(match, vp, offset, string, groups) { + printedRules = printedRules.replace(/(\-?\+?(?:\d+|\*|d+\*|\d+x|x|\b))VP\b/gmi, function replacer(match, vp, offset, string, groups) { return printVP(vp); }); printedRules = printedRules.replace(/\(([*0-9x+-]*)\)/gmi, function replacer(match, p1, offset, string, groups) { @@ -1581,32 +1581,38 @@ width: ${ruleTextBox.width}mm; min-height: ${ruleTextBox.height}mm;`}>; } card.setName(match[1]) - card.setType(match[3]) card.setText(match[6]) - let type = '' - let element; - const typeElement = match[3].split(' ').forEach(te => { - if (['gadget', 'goal', 'spell', 'trap'].includes(te.toLowerCase())) { - type += te - return + const typesAndElements = match[3]; + if (typesAndElements) { + let type = '' + let element; + const typeElement = match[3].split(' ').forEach(te => { + if (!te) { + return; + } + + if (['gadget', 'goal', 'spell', 'trap'].includes(te.toLowerCase())) { + type += te + return + } + + if (!element) { + element = te + } else if (Array.isArray(element)) { + element.push(te) + } else { + element = [element, te] + } + }) + + if (type) { + card.setType(type) } - - if (!element) { - element = te - } else if (Array.isArray(element)) { - element.push(te) - } else { - element = [element, te] + + if (element) { + card.setElement(element) } - }) - - if (type) { - card.setType(type) - } - - if (element) { - card.setElement(element) } const cost = match[4]; diff --git a/test/lang-test.js b/test/lang-test.js index f518504da..be1a8c4ab 100644 --- a/test/lang-test.js +++ b/test/lang-test.js @@ -102,6 +102,71 @@ class MyClass {}`.toAST() }); + describe('Object.mapKeys/Values/Entries/KeysToEntries/ValuesToEntries', function() { + + const obj = { + one: 1, + two: 2, + three: 3, + }; + + it('mapKeys', () => { + expect(obj.mapKeys((key, value, o) => { + expect(o).to.equal(obj) + return key.upperFirst() + value + })).to.deep.equal({ + One1: 1, + Two2: 2, + Three3: 3, + }); + }); + + it('mapValues', () => { + expect(obj.mapValues((value, key, o) => { + expect(o).to.equal(obj) + return value + key.length + })).to.deep.equal({ + one: 4, + two: 5, + three: 8, + }); + }); + + it('mapEntries', () => { + expect(obj.mapEntries(([key, value], o) => { + expect(o).to.equal(obj) + return [value, key] + })).to.deep.equal({ + 1: 'one', + 2: 'two', + 3: 'three', + }); + }); + + it('mapKeysToEntries', () => { + expect(obj.mapKeysToEntries((key, value, o) => { + expect(o).to.equal(obj) + return [key.upperFirst(), value + 1] + })).to.deep.equal({ + One: 2, + Two: 3, + Three: 4, + }); + }); + + it('mapValuesToEntries', () => { + expect(obj.mapValuesToEntries((value, key, o) => { + expect(o).to.equal(obj) + return [value + 1, key.upperFirst()] + })).to.deep.equal({ + 2: 'One', + 3: 'Two', + 4: 'Three', + }); + }); + + }); + describe('computeDiff', function() { describe('Object diff', function() {