Skip to content

Commit

Permalink
Add depth option and require Node.js 8 (#6)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
yaodingyd and sindresorhus committed Nov 22, 2019
1 parent 609d0ba commit 2f3525d
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{package.json,*.yml}]
[*.yml]
indent_style = space
indent_size = 2
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
sudo: false
language: node_js
node_js:
- '6'
- '4'
- '12'
- '10'
- '8'
6 changes: 3 additions & 3 deletions fixture.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

function foo() {
return require('./')();
function foo({depth = 0} = {}) {
return require('.')({depth});
}

module.exports = () => foo();
module.exports = ({depth = 0} = {}) => foo({depth});
2 changes: 1 addition & 1 deletion fixture2.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
'use strict';
module.exports = () => require('./fixture')();
module.exports = ({depth = 0} = {}) => require('./fixture')({depth});
6 changes: 3 additions & 3 deletions fixture3.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

function foo() {
return require('./fixture2')();
function foo({depth = 0} = {}) {
return require('./fixture2')({depth});
}

module.exports = () => foo();
module.exports = ({depth = 0} = {}) => foo({depth});
59 changes: 59 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
declare namespace callerPath {
interface Options {
/**
The caller path depth, meaning how many levels we follow back on the stack trace.
@default 0
@example
```
// foo.ts
import callerPath = require('caller-path');
module.exports = () => {
console.log(callerPath());
//=> '/Users/sindresorhus/dev/unicorn/foobar.ts'
console.log(callerPath({depth: 1}));
//=> '/Users/sindresorhus/dev/unicorn/bar.ts'
console.log(callerPath({depth: 2}));
//=> '/Users/sindresorhus/dev/unicorn/foo.ts'
}
// bar.ts
import foo = require('./foo');
module.exports = () => {
foo();
}
// foobar.ts
import bar = require('./bar');
bar();
```
*/
readonly depth?: number;
}
}

/**
Get the path of the caller function.
@example
```
// foo.ts
import callerPath = require('caller-path');
export default () => {
console.log(callerPath());
//=> '/Users/sindresorhus/dev/unicorn/bar.ts'
}
// bar.ts
import foo from './foo';
foo();
```
If the caller's [callsite](https://github.com/sindresorhus/callsites#api) object `getFileName` was not defined, it will return `undefined`.
*/
declare function callerPath(options?: callerPath.Options): string | undefined;

export = callerPath;
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';
const callerCallsite = require('caller-callsite');

module.exports = () => callerCallsite().getFileName();
module.exports = ({depth = 0} = {}) => callerCallsite({depth}).getFileName();
6 changes: 6 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {expectType, expectError} from 'tsd';
import callerPath = require('.');

expectType<string | undefined>(callerPath());
expectType<string | undefined>(callerPath({depth: 1}));
expectError<string>(callerPath());
20 changes: 4 additions & 16 deletions license
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
The MIT License (MIT)
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17 changes: 8 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
"node": ">=8"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"caller",
Expand All @@ -33,13 +34,11 @@
"file"
],
"dependencies": {
"caller-callsite": "^2.0.0"
"caller-callsite": "^4.1.0"
},
"devDependencies": {
"ava": "*",
"xo": "*"
},
"xo": {
"esnext": true
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}
45 changes: 45 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,51 @@ const foo = require('./foo');
foo();
```

If the caller's [callsite](https://github.com/sindresorhus/callsites#api) object `getFileName` was not defined, it will return `undefined`.

## API

### callerPath(options?)

Get the path of the caller function.

##### depth

Type: `number`<br>
Default: `0`

The caller path depth, meaning how many levels we follow back on the stack trace.

For example:

```js
// foo.js
const callerPath = require('caller-path');

module.exports = () => {
console.log(callerPath());
//=> '/Users/sindresorhus/dev/unicorn/foobar.js'
console.log(callerPath({depth: 1}));
//=> '/Users/sindresorhus/dev/unicorn/bar.js'
console.log(callerPath({depth: 2}));
//=> '/Users/sindresorhus/dev/unicorn/foo.js'
}
```

```js
// bar.js
const foo = require('./foo');

module.exports = () => {
foo();
}
```

```js
// foobar.js
const bar = require('./bar');
bar();
```

---

Expand Down
7 changes: 6 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ import fixture from './fixture';
import fixture2 from './fixture2';
import fixture3 from './fixture3';

test(t => {
test('main', t => {
t.is(path.basename(fixture()), 'test.js');
t.is(path.basename(fixture2()), 'test.js');
t.is(path.basename(fixture3()), 'test.js');
t.is(path.basename(fixture3({depth: 1})), 'test.js');
t.is(path.basename(fixture3({depth: 2})), 'fixture3.js');
t.is(path.basename(fixture3({depth: 3})), 'fixture2.js');
t.is(path.basename(fixture3({depth: 4})), 'fixture.js');
t.is(path.basename(fixture3({depth: 5})), 'index.js');
});

0 comments on commit 2f3525d

Please sign in to comment.