Skip to content

Commit 09f1fd3

Browse files
committed
fix: use a naming convention to identify getters
BREAKING CHANGE All methods that return a value are prefixed with “get”.
1 parent 0479dd7 commit 09f1fd3

File tree

5 files changed

+54
-53
lines changed

5 files changed

+54
-53
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,49 +30,49 @@ repository = gitinfo();
3030
/**
3131
* @returns {string} Absolute path to the .git/ directory.
3232
*/
33-
repository.gitPath();
33+
repository.getGitPath();
3434

3535
// /.git
3636

3737
/**
3838
* @returns {string} Username of the repository author.
3939
*/
40-
repository.username();
40+
repository.getUsername();
4141

4242
// gajus
4343

4444
/**
4545
* @returns {string} Repository name.
4646
*/
47-
repository.name();
47+
repository.getName();
4848

4949
// gitinfo
5050

5151
/**
5252
* @returns {string} Repository URL.
5353
*/
54-
repository.url();
54+
repository.getGithubUrl();
5555

5656
// https://github.com/gajus/gitinfo
5757

5858
/**
5959
* @returns {string} Name of the current branch.
6060
*/
61-
repository.branch();
61+
repository.getBranchName();
6262

6363
// master
6464

6565
/**
6666
* @returns {string} Remote URL of the current branch.
6767
*/
68-
repository.remoteURL();
68+
repository.getRemoteUrl();
6969

7070
// git@github.com:gajus/gitinfo.git
7171

7272
/**
7373
* @returns {string} Commit SHA of the current branch
7474
*/
75-
repository.sha();
75+
repository.getHeadSha();
7676

7777
// dcc075287eb8f6eb4ef34133a4747d2b50b28306
7878
```

src/gitinfo.js

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import fs from 'fs';
22
import path from 'path';
33
import {
4-
parseRemoteOriginURL,
4+
parseRemoteOriginUrl,
55
trim,
6-
parseINI,
7-
isGitDirectory
6+
parseIni,
7+
isGitDirectory,
8+
findGitPath
89
} from './utils';
910

1011
/**
@@ -22,16 +23,16 @@ export default (config: TypeConfig = {}): Object => {
2223
/**
2324
* @returns GitHub repository URL.
2425
*/
25-
gitinfo.url = (): string => {
26-
return 'https://github.com/' + gitinfo.username() + '/' + gitinfo.name();
26+
gitinfo.getGithubUrl = (): string => {
27+
return 'https://github.com/' + gitinfo.getUsername() + '/' + gitinfo.getName();
2728
};
2829

2930
/**
3031
* Gets name of the current branch.
3132
*
3233
* @see http://stackoverflow.com/a/12142066/368691
3334
*/
34-
gitinfo.branch = (): string => {
35+
gitinfo.getBranchName = (): string => {
3536
const name = gitPath + '/HEAD';
3637

3738
/* istanbul ignore next */
@@ -54,9 +55,9 @@ export default (config: TypeConfig = {}): Object => {
5455
/**
5556
* @returns Remote URL of the current branch.
5657
*/
57-
gitinfo.remoteURL = (): string => {
58-
const branchName = gitinfo.branch();
59-
const gitConfig = gitinfo.config();
58+
gitinfo.getRemoteUrl = (): string => {
59+
const branchName = gitinfo.getBranchName();
60+
const gitConfig = gitinfo.getConfig();
6061
const branch = gitConfig['branch "' + branchName + '"'];
6162

6263
/* istanbul ignore next */
@@ -81,31 +82,31 @@ export default (config: TypeConfig = {}): Object => {
8182
/**
8283
* @returns Absolute path to the .git/ directory.
8384
*/
84-
gitinfo.gitPath = (): string => {
85+
gitinfo.getGitPath = (): string => {
8586
return gitPath;
8687
};
8788

8889
/**
8990
* @returns Username of the repository author.
9091
*/
91-
gitinfo.username = (): string => {
92-
return parseRemoteOriginURL(gitinfo.remoteURL()).username;
92+
gitinfo.getUsername = (): string => {
93+
return parseRemoteOriginUrl(gitinfo.getRemoteUrl()).username;
9394
};
9495

9596
/**
9697
* @returns Repository name.
9798
*/
98-
gitinfo.name = (): string => {
99-
return parseRemoteOriginURL(gitinfo.remoteURL()).name;
99+
gitinfo.getName = (): string => {
100+
return parseRemoteOriginUrl(gitinfo.getRemoteUrl()).name;
100101
};
101102

102103
/**
103-
* @returns Commit SHA of the current branch
104+
* @returns Commit SHA of the current branch.
104105
*/
105-
gitinfo.sha = (): string => {
106+
gitinfo.getHeadSha = (): string => {
106107
let sha;
107108

108-
const branch = gitinfo.branch();
109+
const branch = gitinfo.getBranchName();
109110
const shaFile = path.join(gitPath, 'refs', 'heads', branch);
110111

111112
try {
@@ -121,8 +122,8 @@ export default (config: TypeConfig = {}): Object => {
121122
/**
122123
* @returns Representation of the .git/config file.
123124
*/
124-
gitinfo.config = (): Object => {
125-
return parseINI(gitPath + '/config');
125+
gitinfo.getConfig = (): Object => {
126+
return parseIni(gitPath + '/config');
126127
};
127128

128129
config.gitPath = config.gitPath || __dirname;
@@ -132,7 +133,7 @@ export default (config: TypeConfig = {}): Object => {
132133
if (isGitDirectory(config.gitPath)) {
133134
gitPath = config.gitPath;
134135
} else {
135-
gitPath = gitPath(config.gitPath);
136+
gitPath = findGitPath(config.gitPath);
136137
}
137138

138139
/* istanbul ignore next */

src/utils.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import ini from 'ini';
77
*
88
* @access protected
99
*/
10-
export const parseINI = (name: string): Object => {
10+
export const parseIni = (name: string): Object => {
1111
let config;
1212

1313
/* istanbul ignore next */
@@ -30,7 +30,7 @@ type TypeRepository = {
3030
* @access protected
3131
* @param input Supported Git remote origin URL (https, git or SVN).
3232
*/
33-
export const parseRemoteOriginURL = (input: string): TypeRepository => {
33+
export const parseRemoteOriginUrl = (input: string): TypeRepository => {
3434
let url;
3535

3636
// git@github.com:gajus/gitdown.git
@@ -82,7 +82,7 @@ export const isGitDirectory = (path: string): boolean => {
8282
* @access protected
8383
* @param startPath The path where start the search.
8484
*/
85-
export const gitPath = (startPath: string): string => {
85+
export const findGitPath = (startPath: string): string => {
8686
let dirname,
8787
gitpath;
8888

tests/gitinfo.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,39 @@ describe('gitinfo', () => {
1313
gitPath: path.resolve(__dirname, './dummy_git')
1414
});
1515
});
16-
describe('.gitPath()', () => {
16+
describe('.getGitPath()', () => {
1717
it('returns absolute path to the .git directory', () => {
18-
expect(repository.gitPath()).to.equal(fs.realpathSync(path.resolve(__dirname, './dummy_git/')));
18+
expect(repository.getGitPath()).to.equal(fs.realpathSync(path.resolve(__dirname, './dummy_git/')));
1919
});
2020
});
21-
describe('.branch()', () => {
21+
describe('.getBranchName()', () => {
2222
it('returns name of the current branch', () => {
23-
expect(repository.branch()).to.equal('master');
23+
expect(repository.getBranchName()).to.equal('master');
2424
});
2525
});
26-
describe('.remoteURL()', () => {
26+
describe('.getRemoteUrl()', () => {
2727
it('gets the remote URL of the current branch.', () => {
28-
expect(repository.remoteURL()).to.equal('git@github.com:foo/bar.git');
28+
expect(repository.getRemoteUrl()).to.equal('git@github.com:foo/bar.git');
2929
});
3030
});
31-
describe('.username()', () => {
31+
describe('.getUsername()', () => {
3232
it('returns the username of the repository author', () => {
33-
expect(repository.username()).to.equal('foo');
33+
expect(repository.getUsername()).to.equal('foo');
3434
});
3535
});
36-
describe('.name()', () => {
36+
describe('.getName()', () => {
3737
it('returns name of the repository', () => {
38-
expect(repository.name()).to.equal('bar');
38+
expect(repository.getName()).to.equal('bar');
3939
});
4040
});
41-
describe('.url()', () => {
41+
describe('.getGithubUrl()', () => {
4242
it('returns URL of the repository', () => {
43-
expect(repository.url()).to.equal('https://github.com/foo/bar');
43+
expect(repository.getGithubUrl()).to.equal('https://github.com/foo/bar');
4444
});
4545
});
46-
describe('.sha()', () => {
46+
describe('.getHeadSha()', () => {
4747
it('returns commit SHA of the current HEAD', () => {
48-
expect(repository.sha()).to.equal('dcc075287eb8f6eb4ef34133a4747d2b50b28306');
48+
expect(repository.getHeadSha()).to.equal('dcc075287eb8f6eb4ef34133a4747d2b50b28306');
4949
});
5050
});
5151
});

tests/utils.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,42 @@ import {
55
expect
66
} from 'chai';
77
import {
8-
gitPath,
9-
parseRemoteOriginURL,
8+
findGitPath,
9+
parseRemoteOriginUrl,
1010
trim
1111
} from './../src/utils';
1212

1313
describe('utils', () => {
14-
describe('parseRemoteOriginURL()', () => {
14+
describe('parseRemoteOriginUrl()', () => {
1515
it('parses HTTPS URL', () => {
16-
expect(parseRemoteOriginURL('https://github.com/gajus/gitinfo.git')).to.deep.equal({
16+
expect(parseRemoteOriginUrl('https://github.com/gajus/gitinfo.git')).to.deep.equal({
1717
name: 'gitinfo',
1818
username: 'gajus'
1919
});
2020
});
2121
it('parses SSH URL', () => {
22-
expect(parseRemoteOriginURL('git@github.com:gajus/gitinfo.git')).to.deep.equal({
22+
expect(parseRemoteOriginUrl('git@github.com:gajus/gitinfo.git')).to.deep.equal({
2323
name: 'gitinfo',
2424
username: 'gajus'
2525
});
2626
});
2727
it('parses Subeversion URL', () => {
28-
expect(parseRemoteOriginURL('https://github.com/gajus/gitinfo')).to.deep.equal({
28+
expect(parseRemoteOriginUrl('https://github.com/gajus/gitinfo')).to.deep.equal({
2929
name: 'gitinfo',
3030
username: 'gajus'
3131
});
3232
});
3333
it('throws an if URL cannot be broken into username and name', () => {
3434
expect(() => {
35-
parseRemoteOriginURL('http://gajus.com/blog/some/post');
35+
parseRemoteOriginUrl('http://gajus.com/blog/some/post');
3636
}).to.throw(Error, 'Invalid remote origin URL ("http://gajus.com/blog/some/post").');
3737
});
3838
});
39-
describe('gitPath()', () => {
39+
describe('findGitPath()', () => {
4040
context('a path of a descendant directory that has a parent directory that includes .git directory', () => {
4141
it('resolves path of the .git directory', () => {
4242
const targetPath = path.resolve(__dirname, './../.git');
43-
const resolvedPath = gitPath(__dirname);
43+
const resolvedPath = findGitPath(__dirname);
4444

4545
expect(resolvedPath).to.equal(targetPath);
4646
});

0 commit comments

Comments
 (0)