From 77cdd532690d728917f638ab271d3bc0e2053e53 Mon Sep 17 00:00:00 2001 From: Luke Babich Date: Tue, 18 Sep 2018 16:24:48 +0100 Subject: [PATCH 1/4] Feature: added getAliasPath function This will retrieve an alias's path from the moduleAliases object --- .gitignore | 1 + index.js | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/.gitignore b/.gitignore index 3c3629e..bc3df71 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +.idea/* \ No newline at end of file diff --git a/index.js b/index.js index b0299f0..5edf903 100644 --- a/index.js +++ b/index.js @@ -183,9 +183,23 @@ function init (options) { } } +// Retrieve a path by specifying a registered alias +function getAliasPath(alias) { + if (!moduleAliases[alias]) { + throw new Error('[' + alias + ']: alias does not exist'); + } + + if (typeof alias === 'string') { + return moduleAliases[alias]; + } else { + throw new Error('[' + alias + ']: Is not of type string'); + } +} + 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 From b0640dcff331a1ad4f89494d485bc503d5cf229d Mon Sep 17 00:00:00 2001 From: Luke Babich Date: Tue, 18 Sep 2018 16:49:40 +0100 Subject: [PATCH 2/4] feature: added tests and cleaned up error checks --- index.js | 10 +++++----- test/specs.js | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 5edf903..0c0cbbf 100644 --- a/index.js +++ b/index.js @@ -185,15 +185,15 @@ 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'); } - if (typeof alias === 'string') { - return moduleAliases[alias]; - } else { - throw new Error('[' + alias + ']: Is not of type string'); - } + return moduleAliases[alias]; } module.exports = init diff --git a/test/specs.js b/test/specs.js index 4272d9b..35db330 100644 --- a/test/specs.js +++ b/test/specs.js @@ -174,4 +174,29 @@ 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); + }) }) From 785e83412df4cf04d47487b3ca2ad585dc371581 Mon Sep 17 00:00:00 2001 From: Luke Babich Date: Mon, 11 Feb 2019 09:36:40 +0000 Subject: [PATCH 3/4] fix: adjusted indents and single line statements --- test/specs.js | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/test/specs.js b/test/specs.js index 35db330..2e5fca1 100644 --- a/test/specs.js +++ b/test/specs.js @@ -175,28 +175,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); + 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); - }) + 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 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); - }) + 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); + }) }) From ce17ecbf01f187fd5b4ed982eb1b428127a0c6d1 Mon Sep 17 00:00:00 2001 From: Luke Babich Date: Mon, 11 Feb 2019 09:54:24 +0000 Subject: [PATCH 4/4] fix: adjusted quotes --- index.js | 4 ++-- test/specs.js | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 0c0cbbf..8cca483 100644 --- a/index.js +++ b/index.js @@ -186,11 +186,11 @@ 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'); + throw new Error('"' + alias + '": is not of type string'); } if (!moduleAliases[alias]) { - throw new Error('[' + alias + ']: alias does not exist'); + throw new Error('"' + alias + '": alias does not exist'); } return moduleAliases[alias]; diff --git a/test/specs.js b/test/specs.js index 2e5fca1..088102a 100644 --- a/test/specs.js +++ b/test/specs.js @@ -180,8 +180,7 @@ describe('Custom handler function', function () { moduleAlias.addAlias('@root', expected); var rootPath = moduleAlias.getAliasPath('@root'); - expect(rootPath) - .to.equal(expected); + expect(rootPath).to.equal(expected); }) it('should throw when alias does not exist', function () { @@ -192,7 +191,7 @@ describe('Custom handler function', function () { }) it('should throw when alias is not a string', function () { - var expected = '[123]: is not of type string'; + var expected = '"123": is not of type string'; expect(function () { moduleAlias.getAliasPath(123); }).to.throw(expected);