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

Require Node.js 8, add TypeScript definition #8

Merged
merged 1 commit into from Apr 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .editorconfig
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
2 changes: 1 addition & 1 deletion .gitattributes
@@ -1 +1 @@
* text=auto
* text=auto eol=lf
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
node_modules
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
@@ -0,0 +1 @@
package-lock=false
8 changes: 3 additions & 5 deletions .travis.yml
@@ -1,7 +1,5 @@
sudo: false
language: node_js
node_js:
- '5'
- '4'
- '0.12'
- '0.10'
- '12'
- '10'
- '8'
14 changes: 14 additions & 0 deletions index.d.ts
@@ -0,0 +1,14 @@
/**
Convert an absolute path to a tilde path: `/Users/sindresorhus/dev` → `~/dev`.

@example
```
import tildify = require('tildify');

tildify('/Users/sindresorhus/dev');
//=> '~/dev'
```
*/
declare function tildify(string: string): string;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should try to do descriptive argument labels when possible.

Suggested change
declare function tildify(string: string): string;
declare function tildify(absolutePath: string): string;


export = tildify;
13 changes: 7 additions & 6 deletions index.js
@@ -1,9 +1,10 @@
'use strict';
var path = require('path');
var osHomedir = require('os-homedir');
var home = osHomedir();
const path = require('path');
const os = require('os');

module.exports = function (str) {
str = path.normalize(str) + path.sep;
return (str.indexOf(home) === 0 ? str.replace(home + path.sep, '~' + path.sep) : str).slice(0, -1);
const home = os.homedir();

module.exports = string => {
string = path.normalize(string) + path.sep;
return (string.indexOf(home) === 0 ? string.replace(home + path.sep, `~${path.sep}`) : string).slice(0, -1);
};
4 changes: 4 additions & 0 deletions index.test-d.ts
@@ -0,0 +1,4 @@
import {expectType} from 'tsd';
import tildify = require('.');

expectType<string>(tildify('/Users/sindresorhus/dev'));
20 changes: 4 additions & 16 deletions license
@@ -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.
77 changes: 38 additions & 39 deletions package.json
@@ -1,41 +1,40 @@
{
"name": "tildify",
"version": "1.2.0",
"description": "Convert an absolute path to a tilde path: `/Users/sindresorhus/dev` → `~/dev`",
"license": "MIT",
"repository": "sindresorhus/tildify",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"unexpand",
"homedir",
"tilde",
"tildify",
"collapse",
"path",
"home",
"dir",
"directory",
"user",
"expand"
],
"dependencies": {
"os-homedir": "^1.0.0"
},
"devDependencies": {
"ava": "*",
"xo": "*"
}
"name": "tildify",
"version": "1.2.0",
"description": "Convert an absolute path to a tilde path: `/Users/sindresorhus/dev` → `~/dev`",
"license": "MIT",
"repository": "sindresorhus/tildify",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=8"
},
"scripts": {
"test": "xo && ava && tsd"
},
"files": [
"index.js",
"index.d.ts"
],
"keywords": [
"unexpand",
"homedir",
"tilde",
"tildify",
"collapse",
"path",
"home",
"dir",
"directory",
"user",
"expand"
],
"devDependencies": {
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -6,7 +6,7 @@
## Install

```
$ npm install --save tildify
$ npm install tildify
```


Expand Down
21 changes: 11 additions & 10 deletions test.js
@@ -1,33 +1,34 @@
'use strict';
const path = require('path');
const os = require('os');
const test = require('ava');
const osHomedir = require('os-homedir');
const m = require('./');
const home = osHomedir();
const tildify = require('.');

const home = os.homedir();

test('tildify home', t => {
const fixture = home;
t.is(m(fixture), '~');
t.is(tildify(fixture), '~');
});

test('tildify path', t => {
const fixture = path.resolve(home, 'tildify');
t.is(m(fixture)[0], '~');
t.true(/tildify$/.test(m(fixture)));
t.not(m(fixture), fixture);
t.is(tildify(fixture)[0], '~');
t.true(tildify(fixture).endsWith('tildify'));
t.not(tildify(fixture), fixture);
});

test('ensure only a fully matching path is replaced', t => {
const fixture = path.resolve(`${home}foo`, 'tildify');
t.is(m(fixture), fixture);
t.is(tildify(fixture), fixture);
});

test('ignore relative paths', t => {
const fixture = 'tildify';
t.is(m(fixture), fixture);
t.is(tildify(fixture), fixture);
});

test('only tildify when home is at the start of a path', t => {
const fixture = path.join('tildify', home);
t.is(m(fixture), fixture);
t.is(tildify(fixture), fixture);
});