Skip to content

Commit

Permalink
utils for mapping entries of objects
Browse files Browse the repository at this point in the history
SQUASHED: AUTO-COMMIT-src-client-lang-lang.js,AUTO-COMMIT-src-components-widgets-ubg-cards.js,AUTO-COMMIT-test-lang-test.js,
  • Loading branch information
onsetsu committed Oct 12, 2023
1 parent 5e35fcd commit 4aad106
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 23 deletions.
26 changes: 26 additions & 0 deletions src/client/lang/lang.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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
Expand Down
52 changes: 29 additions & 23 deletions src/components/widgets/ubg-cards.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -1581,32 +1581,38 @@ width: ${ruleTextBox.width}mm; min-height: ${ruleTextBox.height}mm;`}></div>;
}

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];
Expand Down
65 changes: 65 additions & 0 deletions test/lang-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 4aad106

Please sign in to comment.