Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
septs committed Mar 30, 2021
0 parents commit 2780813
Show file tree
Hide file tree
Showing 15 changed files with 2,355 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/node_modules
/cjs
/esm
/umd
6 changes: 6 additions & 0 deletions .mocharc.json
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"]
}
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.*
/scripts
/cjs/*.spec.*
/esm/*.spec.*
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"quoteProps": "consistent",
"singleQuote": true,
"printWidth": 160
}
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll": true
}
}
5 changes: 5 additions & 0 deletions LICENSE
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.
7 changes: 7 additions & 0 deletions README.md
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)
52 changes: 52 additions & 0 deletions index.spec.ts
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');
});
});
28 changes: 28 additions & 0 deletions index.ts
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);
Loading

0 comments on commit 2780813

Please sign in to comment.