Skip to content

Commit

Permalink
fix(ns): ns key resolver
Browse files Browse the repository at this point in the history
The ns resolver had a bug, where it cannot distinguish between namespaces that
contain one or another in their key. E.g. setting two namespaces `config` and
`conf` it would always resolve to the former when using the latter.
  • Loading branch information
ds82 committed Dec 11, 2018
1 parent 25e221f commit 3f19767
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
20 changes: 18 additions & 2 deletions src/__tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,6 @@ describe('tiny-di', function() {
expect(fakeLoader).toHaveBeenCalledWith('some');
});

// SHOULD WORK WITH DEEPER NS STRUCTURE

it('should work with $inject', function() {
tiny.ns('test').to('some');

Expand All @@ -281,6 +279,24 @@ describe('tiny-di', function() {
expect(bar).toEqual(FAKE_MAP['some/foo/bar']);
expect(fakeLoader).toHaveBeenCalledWith('some/foo/bar');
});

it('should work with other extensions', () => {
tiny.ns('assets').to('/root/assets');
tiny.get('assets/foo/some.json');

expect(fakeLoader).toHaveBeenCalledWith('/root/assets/foo/some.json');
});

it('should work with similiar ns ids', () => {
tiny.ns('config').to('/root/config');
tiny.ns('conf').to('/root/conf');

tiny.get('config/foo');
expect(fakeLoader).toHaveBeenCalledWith('/root/config/foo');

tiny.get('conf/bar');
expect(fakeLoader).toHaveBeenCalledWith('/root/conf/bar');
});
});

describe('provide-by', function() {
Expand Down
3 changes: 2 additions & 1 deletion src/binder/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
*/
'use strict';

import {AbstractBase} from '../base';
import { AbstractBase } from '../base';

export class PathBinder extends AbstractBase {
to(dir) {
// console.log('new pathBinding', this.key, dir);
this.injector.setNsBinding(this.key, dir);
}
}
8 changes: 4 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ class TinyDi {
return key;
}

var prefix = this.nsBindings.find(prefix => {
var re = new RegExp('^' + escapeRegExp(prefix.ns) + '.*');
return re.test(prefix.ns);
const prefix = this.nsBindings.find(prefix => {
const re = new RegExp('^' + escapeRegExp(prefix.ns) + '/');
return key === prefix.ns || !!key.match(re);
});

if (prefix) {
var re = new RegExp('^' + escapeRegExp(prefix.ns));
const re = new RegExp('^' + escapeRegExp(prefix.ns));
return key.replace(re, prefix.path);
}
return key;
Expand Down

0 comments on commit 3f19767

Please sign in to comment.