Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieve Alias Path #33

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.idea/*
14 changes: 14 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,23 @@ function init (options) {
}
}

// Retrieve a path by specifying a registered alias
function getAliasPath(alias) {
if (typeof alias !== 'string') {
throw new Error('"' + alias + '": is not of type string');
}

if (!moduleAliases[alias]) {
throw new Error('"' + alias + '": alias does not exist');
}

return moduleAliases[alias];
}

module.exports = init
module.exports.addPath = addPath
module.exports.addAlias = addAlias
module.exports.addAliases = addAliases
module.exports.isPathMatchesAlias = isPathMatchesAlias
module.exports.reset = reset
module.exports.getAliasPath = getAliasPath
22 changes: 22 additions & 0 deletions test/specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,26 @@ describe('Custom handler function', function () {
})
.to.throw('[module-alias] Expecting custom handler function to return path.')
})

it('should return a path matching the alias', function () {
var expected = path.join(__dirname, 'src/bar/baz');
moduleAlias.addAlias('@root', expected);

var rootPath = moduleAlias.getAliasPath('@root');
expect(rootPath).to.equal(expected);
})

it('should throw when alias does not exist', function () {
var expected = '[@randomPath]: alias does not exist';
expect(function () {
moduleAlias.getAliasPath('@randomPath');
}).to.throw(expected);
})

it('should throw when alias is not a string', function () {
var expected = '"123": is not of type string';
expect(function () {
moduleAlias.getAliasPath(123);
}).to.throw(expected);
})
})