Skip to content

Commit

Permalink
feat(tim): custom tim implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveVanOpstal committed Apr 10, 2016
1 parent f8ec20e commit a3638e0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 34 deletions.
57 changes: 38 additions & 19 deletions src/app/build/ability-sequence.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,47 @@
import {provide} from 'angular2/core';
import {BaseRequestOptions, Http, Response, ResponseOptions} from 'angular2/http';
import {RouteParams} from 'angular2/router';
import {RootRouter} from 'angular2/src/router/router';
import {it, inject, beforeEachProviders, beforeEach} from 'angular2/testing';

import {it, inject, beforeEachProviders} from 'angular2/testing';
import {MockBackend, MockConnection} from 'angular2/http/testing';

import {LolApiService} from '../misc/lolapi.service';
import {AbilitySequenceComponent} from './ability-sequence.component';

describe('AbilitySequenceComponent', () => {
beforeEachProviders(() => [
provide(RouteParams, { useValue: new RouteParams({ region: 'euw' }) }),
BaseRequestOptions,
MockBackend,
provide(Http, {
useFactory: function(backend, defaultOptions) {
return new Http(backend, defaultOptions);
},
deps: [MockBackend, BaseRequestOptions]
}),

LolApiService,
AbilitySequenceComponent
]);

beforeEach(inject([AbilitySequenceComponent], (component) => {
component.champion = {
stats: {
attackrange: 175,
mpperlevel: 47,
mp: 334,
},
spells: [{
effect: [
null,
[50, 75, 100, 125, 150],
[35, 35, 35, 35, 35],
[0.3, 0.35, 0.4, 0.45, 0.5],
],
vars: [
{
link: 'spelldamage',
coeff: [0.6],
key: 'a1'
}
],
sanitizedTooltip: 'Test {{ e1 }} (+{{ f1 }}) (+{{ a1 }}) e3 {{ e2 } }%.'
}]
};
}));


it('should create a tooltip', inject([AbilitySequenceComponent], (component) => {
let extendedTooltip = component.getExtendedTooltip(0);
expect(extendedTooltip).toBe('Test 50 (+175) (+0.6) e3 35%.');
}));

it('should handle tooltip errors', inject([AbilitySequenceComponent], (component) => {
component.champion.spells[0].sanitizedTooltip = 'Test {{ e1 }} (+{{ f1 }}) (+{{ a1 }}) e3 {{ e2 } }%. {{f10}} {{f2}} {{e3}}';
let extendedTooltip = component.getExtendedTooltip(0);
expect(extendedTooltip).toBe('Test 50 (+175) (+0.6) e3 35%. [[error]] 47 0.3');
}));
});
6 changes: 1 addition & 5 deletions src/app/build/ability-sequence.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,7 @@ export class AbilitySequenceComponent {
effects[attrname] = stats[attrname];
}

try {
return tim(spell.sanitizedTooltip, effects);
} catch (e) {
return 'Something went wrong..';
}
return tim(spell.sanitizedTooltip, effects);
}

getStats() {
Expand Down
16 changes: 6 additions & 10 deletions src/app/misc/tim.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
'use strict';

var path = '[a-z0-9_][\\.a-z0-9_]*';

export function tim(template, data) {
var pattern = new RegExp(exports.start + '\\s*(' + path + ')\\s*' + exports.end, 'gi');
let pattern = new RegExp('{ ?{\\s*([a-z0-9_][\\.a-z0-9_]*)\\s*} ?}', 'gi');

// Merge data into the template string
return template.replace(pattern, function(tag, token) {
var path = token.split('.'),
len = path.length,
lookup = data,
i = 0;
let path = token.split('.');
let len = path.length;

for (; i < len; i++) {
lookup = lookup[path[i]];
for (let i = 0; i < len; i++) {
let lookup = data[path[i]];

// Property not found
if (lookup === undefined) {
console.error('tim: ' + path[i] + ' not found in ' + tag);
return false;
return '[[error]]';
}

// Return the required value
Expand Down

0 comments on commit a3638e0

Please sign in to comment.