├── .editorconfig ├── .eslintrc.json ├── .gitattributes ├── .gitignore ├── .travis.yml ├── .verb.md ├── LICENSE ├── README.md ├── index.js ├── package.json └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | end_of_line = lf 6 | charset = utf-8 7 | indent_size = 2 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [{**/{actual,fixtures,expected,templates}/**,*.md}] 12 | trim_trailing_whitespace = false 13 | insert_final_newline = false -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "ecmaFeatures": { 3 | "modules": true, 4 | "experimentalObjectRestSpread": true 5 | }, 6 | 7 | "env": { 8 | "browser": false, 9 | "es6": true, 10 | "node": true, 11 | "mocha": true 12 | }, 13 | 14 | "globals": { 15 | "document": false, 16 | "navigator": false, 17 | "window": false 18 | }, 19 | 20 | "rules": { 21 | "accessor-pairs": 2, 22 | "arrow-spacing": [2, { "before": true, "after": true }], 23 | "block-spacing": [2, "always"], 24 | "brace-style": [2, "1tbs", { "allowSingleLine": true }], 25 | "comma-dangle": [2, "never"], 26 | "comma-spacing": [2, { "before": false, "after": true }], 27 | "comma-style": [2, "last"], 28 | "constructor-super": 2, 29 | "curly": [2, "multi-line"], 30 | "dot-location": [2, "property"], 31 | "eol-last": 2, 32 | "eqeqeq": [2, "allow-null"], 33 | "generator-star-spacing": [2, { "before": true, "after": true }], 34 | "handle-callback-err": [2, "^(err|error)$" ], 35 | "indent": [2, 2, { "SwitchCase": 1 }], 36 | "key-spacing": [2, { "beforeColon": false, "afterColon": true }], 37 | "keyword-spacing": [2, { "before": true, "after": true }], 38 | "new-cap": [2, { "newIsCap": true, "capIsNew": false }], 39 | "new-parens": 2, 40 | "no-array-constructor": 2, 41 | "no-caller": 2, 42 | "no-class-assign": 2, 43 | "no-cond-assign": 2, 44 | "no-const-assign": 2, 45 | "no-control-regex": 2, 46 | "no-debugger": 2, 47 | "no-delete-var": 2, 48 | "no-dupe-args": 2, 49 | "no-dupe-class-members": 2, 50 | "no-dupe-keys": 2, 51 | "no-duplicate-case": 2, 52 | "no-empty-character-class": 2, 53 | "no-eval": 2, 54 | "no-ex-assign": 2, 55 | "no-extend-native": 2, 56 | "no-extra-bind": 2, 57 | "no-extra-boolean-cast": 2, 58 | "no-extra-parens": [2, "functions"], 59 | "no-fallthrough": 2, 60 | "no-floating-decimal": 2, 61 | "no-func-assign": 2, 62 | "no-implied-eval": 2, 63 | "no-inner-declarations": [2, "functions"], 64 | "no-invalid-regexp": 2, 65 | "no-irregular-whitespace": 2, 66 | "no-iterator": 2, 67 | "no-label-var": 2, 68 | "no-labels": 2, 69 | "no-lone-blocks": 2, 70 | "no-mixed-spaces-and-tabs": 2, 71 | "no-multi-spaces": 2, 72 | "no-multi-str": 2, 73 | "no-multiple-empty-lines": [2, { "max": 1 }], 74 | "no-native-reassign": 0, 75 | "no-negated-in-lhs": 2, 76 | "no-new": 2, 77 | "no-new-func": 2, 78 | "no-new-object": 2, 79 | "no-new-require": 2, 80 | "no-new-wrappers": 2, 81 | "no-obj-calls": 2, 82 | "no-octal": 2, 83 | "no-octal-escape": 2, 84 | "no-proto": 0, 85 | "no-redeclare": 2, 86 | "no-regex-spaces": 2, 87 | "no-return-assign": 2, 88 | "no-self-compare": 2, 89 | "no-sequences": 2, 90 | "no-shadow-restricted-names": 2, 91 | "no-spaced-func": 2, 92 | "no-sparse-arrays": 2, 93 | "no-this-before-super": 2, 94 | "no-throw-literal": 2, 95 | "no-trailing-spaces": 0, 96 | "no-undef": 2, 97 | "no-undef-init": 2, 98 | "no-unexpected-multiline": 2, 99 | "no-unneeded-ternary": [2, { "defaultAssignment": false }], 100 | "no-unreachable": 2, 101 | "no-unused-vars": [2, { "vars": "all", "args": "none" }], 102 | "no-useless-call": 0, 103 | "no-with": 2, 104 | "one-var": [0, { "initialized": "never" }], 105 | "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }], 106 | "padded-blocks": [0, "never"], 107 | "quotes": [2, "single", "avoid-escape"], 108 | "radix": 2, 109 | "semi": [2, "always"], 110 | "semi-spacing": [2, { "before": false, "after": true }], 111 | "space-before-blocks": [2, "always"], 112 | "space-before-function-paren": [2, "never"], 113 | "space-in-parens": [2, "never"], 114 | "space-infix-ops": 2, 115 | "space-unary-ops": [2, { "words": true, "nonwords": false }], 116 | "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }], 117 | "use-isnan": 2, 118 | "valid-typeof": 2, 119 | "wrap-iife": [2, "any"], 120 | "yoda": [2, "never"] 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Enforce Unix newlines 2 | *.* text eol=lf 3 | *.css text eol=lf 4 | *.html text eol=lf 5 | *.js text eol=lf 6 | *.json text eol=lf 7 | *.less text eol=lf 8 | *.md text eol=lf 9 | *.yml text eol=lf 10 | 11 | *.jpg binary 12 | *.gif binary 13 | *.png binary 14 | *.jpeg binary -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # always ignore files 2 | *.DS_Store 3 | *.sublime-* 4 | 5 | # test related, or directories generated by tests 6 | test/actual 7 | actual 8 | coverage 9 | 10 | # npm 11 | node_modules 12 | npm-debug.log 13 | 14 | # misc 15 | _gh_pages 16 | benchmark 17 | bower_components 18 | vendor 19 | temp 20 | tmp 21 | TODO.md 22 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - node 5 | - '6' 6 | - '5' 7 | - '0.12' 8 | - '0.10' 9 | -------------------------------------------------------------------------------- /.verb.md: -------------------------------------------------------------------------------- 1 | ## Usage 2 | 3 | See the [Bash documentation for Tilde Expansion][docs]. 4 | 5 | ```js 6 | var expandTilde = require('{%= name %}'); 7 | 8 | expandTilde('~') 9 | //=> '/Users/jonschlinkert' 10 | 11 | expandTilde('~+') 12 | //=> process.cwd() 13 | ``` 14 | 15 | ## Run tests 16 | 17 | Install dev dependencies: 18 | 19 | ```bash 20 | npm i -d && npm test 21 | ``` 22 | 23 | [docs]: https://www.gnu.org/software/bash/manual/html_node/Tilde-Expansion.html 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015-2016, Jon Schlinkert. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # expand-tilde [![NPM version](https://img.shields.io/npm/v/expand-tilde.svg?style=flat)](https://www.npmjs.com/package/expand-tilde) [![NPM downloads](https://img.shields.io/npm/dm/expand-tilde.svg?style=flat)](https://npmjs.org/package/expand-tilde) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/expand-tilde.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/expand-tilde) 2 | 3 | > Bash-like tilde expansion for node.js. Expands a leading tilde in a file path to the user home directory, or `~+` to the cwd. 4 | 5 | ## Install 6 | 7 | Install with [npm](https://www.npmjs.com/): 8 | 9 | ```sh 10 | $ npm install --save expand-tilde 11 | ``` 12 | 13 | ## Usage 14 | 15 | See the [Bash documentation for Tilde Expansion](https://www.gnu.org/software/bash/manual/html_node/Tilde-Expansion.html). 16 | 17 | ```js 18 | var expandTilde = require('expand-tilde'); 19 | 20 | expandTilde('~') 21 | //=> '/Users/jonschlinkert' 22 | 23 | expandTilde('~+') 24 | //=> process.cwd() 25 | ``` 26 | 27 | ## Run tests 28 | 29 | Install dev dependencies: 30 | 31 | ```bash 32 | npm i -d && npm test 33 | ``` 34 | 35 | ## About 36 | 37 | ### Related projects 38 | 39 | * [braces](https://www.npmjs.com/package/braces): Fast, comprehensive, bash-like brace expansion implemented in JavaScript. Complete support for the Bash 4.3 braces… [more](https://github.com/jonschlinkert/braces) | [homepage](https://github.com/jonschlinkert/braces "Fast, comprehensive, bash-like brace expansion implemented in JavaScript. Complete support for the Bash 4.3 braces specification, without sacrificing speed.") 40 | * [expand-brackets](https://www.npmjs.com/package/expand-brackets): Expand POSIX bracket expressions (character classes) in glob patterns. | [homepage](https://github.com/jonschlinkert/expand-brackets "Expand POSIX bracket expressions (character classes) in glob patterns.") 41 | * [is-glob](https://www.npmjs.com/package/is-glob): Returns `true` if the given string looks like a glob pattern or an extglob pattern… [more](https://github.com/jonschlinkert/is-glob) | [homepage](https://github.com/jonschlinkert/is-glob "Returns `true` if the given string looks like a glob pattern or an extglob pattern. This makes it easy to create code that only uses external modules like node-glob when necessary, resulting in much faster code execution and initialization time, and a bet") 42 | * [micromatch](https://www.npmjs.com/package/micromatch): Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch. | [homepage](https://github.com/jonschlinkert/micromatch "Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch.") 43 | 44 | ### Contributing 45 | 46 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). 47 | 48 | ### Building docs 49 | 50 | _(This document was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme) (a [verb](https://github.com/verbose/verb) generator), please don't edit the readme directly. Any changes to the readme must be made in [.verb.md](.verb.md).)_ 51 | 52 | To generate the readme and API documentation with [verb](https://github.com/verbose/verb): 53 | 54 | ```sh 55 | $ npm install -g verb verb-generate-readme && verb 56 | ``` 57 | 58 | ### Running tests 59 | 60 | Install dev dependencies: 61 | 62 | ```sh 63 | $ npm install -d && npm test 64 | ``` 65 | 66 | ### Author 67 | 68 | **Jon Schlinkert** 69 | 70 | * [github/jonschlinkert](https://github.com/jonschlinkert) 71 | * [twitter/jonschlinkert](http://twitter.com/jonschlinkert) 72 | 73 | ### License 74 | 75 | Copyright © 2016, [Jon Schlinkert](https://github.com/jonschlinkert). 76 | Released under the [MIT license](LICENSE). 77 | 78 | *** 79 | 80 | _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.2.0, on December 08, 2016._ -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * expand-tilde 3 | * 4 | * Copyright (c) 2015 Jon Schlinkert. 5 | * Licensed under the MIT license. 6 | */ 7 | 8 | var homedir = require('homedir-polyfill'); 9 | var path = require('path'); 10 | 11 | module.exports = function expandTilde(filepath) { 12 | var home = homedir(); 13 | 14 | if (filepath.charCodeAt(0) === 126 /* ~ */) { 15 | if (filepath.charCodeAt(1) === 43 /* + */) { 16 | return path.join(process.cwd(), filepath.slice(2)); 17 | } 18 | return home ? path.join(home, filepath.slice(1)) : filepath; 19 | } 20 | 21 | return filepath; 22 | }; 23 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "expand-tilde", 3 | "description": "Bash-like tilde expansion for node.js. Expands a leading tilde in a file path to the user home directory, or `~+` to the cwd.", 4 | "version": "2.0.2", 5 | "homepage": "https://github.com/jonschlinkert/expand-tilde", 6 | "author": "Jon Schlinkert (https://github.com/jonschlinkert)", 7 | "repository": "jonschlinkert/expand-tilde", 8 | "bugs": { 9 | "url": "https://github.com/jonschlinkert/expand-tilde/issues" 10 | }, 11 | "license": "MIT", 12 | "files": [ 13 | "index.js" 14 | ], 15 | "main": "index.js", 16 | "engines": { 17 | "node": ">=0.10.0" 18 | }, 19 | "scripts": { 20 | "test": "mocha" 21 | }, 22 | "devDependencies": { 23 | "gulp-format-md": "^0.1.9", 24 | "is-windows": "^0.2.0", 25 | "mocha": "^2.5.3" 26 | }, 27 | "keywords": [ 28 | "cwd", 29 | "expand", 30 | "expansion", 31 | "filepath", 32 | "home", 33 | "path", 34 | "pwd", 35 | "tilde", 36 | "user", 37 | "userhome" 38 | ], 39 | "verb": { 40 | "run": true, 41 | "toc": false, 42 | "layout": "default", 43 | "tasks": [ 44 | "readme" 45 | ], 46 | "plugins": [ 47 | "gulp-format-md" 48 | ], 49 | "related": { 50 | "list": [ 51 | "braces", 52 | "expand-brackets", 53 | "is-glob", 54 | "micromatch" 55 | ] 56 | }, 57 | "reflinks": [ 58 | "verb" 59 | ], 60 | "lint": { 61 | "reflinks": true 62 | } 63 | }, 64 | "dependencies": { 65 | "homedir-polyfill": "^1.0.1" 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | require('mocha'); 4 | var isWindows = require('is-windows'); 5 | var homedir = require('homedir-polyfill'); 6 | var assert = require('assert'); 7 | var path = require('path'); 8 | path.sep = '/'; 9 | 10 | var expandTilde = require('./'); 11 | 12 | if (!isWindows()) { 13 | it('should expand a tilde to the user home directory', function() { 14 | assert.equal(expandTilde('~'), homedir()); 15 | }); 16 | 17 | it('should expand `~+` to process.cwd, per bash spec', function() { 18 | assert.equal(expandTilde('~+'), process.cwd()); 19 | assert.equal(expandTilde('~+/foo/bar'), path.join(process.cwd(), 'foo/bar')); 20 | }); 21 | } 22 | --------------------------------------------------------------------------------