-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2780813
Showing
15 changed files
with
2,355 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
indent_size = 2 | ||
indent_style = space | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/node_modules | ||
/cjs | ||
/esm | ||
/umd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/mocharc", | ||
"require": ["ts-node/register"], | ||
"spec": "index.spec.ts", | ||
"extension": ["ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/.* | ||
/scripts | ||
/cjs/*.spec.* | ||
/esm/*.spec.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"quoteProps": "consistent", | ||
"singleQuote": true, | ||
"printWidth": 160 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"editor.formatOnSave": true, | ||
"editor.codeActionsOnSave": { | ||
"source.fixAll": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Copyright (C) 2021 by NiceLabs <[email protected]> | ||
|
||
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# expandvars | ||
|
||
Inspired from Python's `os.expanduser` and `os.expandvars`. | ||
|
||
## LICENE | ||
|
||
[Zero Clause BSD](LICENSE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { assert } from 'chai'; | ||
import 'mocha'; | ||
import { expandUser, expandVars } from './index'; | ||
import { homedir } from 'os'; | ||
|
||
describe('expanduser', () => { | ||
const fakeHome = '/home/example'; | ||
|
||
it('unbound', () => { | ||
assert.equal(expandUser('~'), homedir()); | ||
}); | ||
|
||
it('$HOME', () => { | ||
const expand = expandUser.bind({ HOME: fakeHome }); | ||
assert.equal(expand('~'), fakeHome); | ||
}); | ||
|
||
it('$USERPROFILE', () => { | ||
const expand = expandUser.bind({ USERPROFILE: fakeHome }); | ||
assert.equal(expand('~'), fakeHome); | ||
}); | ||
}); | ||
|
||
describe('expandvars', () => { | ||
it('constants', () => { | ||
assert.equal(expandVars('FOO'), 'FOO'); | ||
assert.equal(expandVars('$'), '$'); | ||
assert.equal(expandVars('BAR$'), 'BAR$'); | ||
}); | ||
|
||
it('empty', () => { | ||
assert.equal(expandVars(''), ''); | ||
assert.equal(expandVars('$FOO'), '$FOO'); | ||
}); | ||
|
||
it('simple', () => { | ||
const expand = expandVars.bind({ FOO: 'bar' }); | ||
assert.equal(expand('$FOO'), 'bar'); | ||
assert.equal(expand('${FOO}'), 'bar'); | ||
}); | ||
|
||
it('combo', () => { | ||
const expand = expandVars.bind({ FOO: 'bar', BIZ: 'buz' }); | ||
assert.equal(expand('${FOO}:$BIZ'), 'bar:buz'); | ||
assert.equal(expand('$FOO$BIZ'), 'barbuz'); | ||
assert.equal(expand('${FOO}$BIZ'), 'barbuz'); | ||
assert.equal(expand('$FOO${BIZ}'), 'barbuz'); | ||
assert.equal(expand('$FOO-$BIZ'), 'bar-buz'); | ||
assert.equal(expand('boo$BIZ'), 'boobuz'); | ||
assert.equal(expand('boo${BIZ}'), 'boobuz'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
export type Environ = Record<string, string | undefined>; | ||
|
||
export function expandUser(input: string, environ: Environ = process.env) { | ||
if (typeof input !== 'string') { | ||
throw new Error('The input type not is string'); | ||
} | ||
const home = environ.HOME ?? environ.USERPROFILE; | ||
return home ? input.replace(/^~/g, home) : input; | ||
} | ||
|
||
expandUser.bind = (environ: Environ) => (input: string) => expandUser(input, environ); | ||
|
||
export function expandVars(input: string, environ: Environ = process.env) { | ||
if (typeof input !== 'string') { | ||
throw new Error('The input type not is string'); | ||
} | ||
if (input.indexOf('$') === -1) { | ||
return input; | ||
} | ||
return input.replace(/\$(\w+|\{[^}]*\})/g, (match, p1: string) => { | ||
if (p1[0] === '{' && p1[p1.length - 1] === '}') { | ||
p1 = p1.slice(1, -1); | ||
} | ||
return environ[p1] ?? match; | ||
}); | ||
} | ||
|
||
expandVars.bind = (environ: Environ) => (input: string) => expandVars(input, environ); |
Oops, something went wrong.