Skip to content

Commit

Permalink
* First commit
Browse files Browse the repository at this point in the history
* Added test cases
* Created getDirname and getFilename functions
* Created LICENSE
* Created Github Actions
* Created README
  • Loading branch information
sant123 committed Dec 23, 2020
0 parents commit 82f30fb
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 0 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/deno.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

# This workflow will install Deno and run tests across stable and nightly builds on Windows, Ubuntu and macOS.
# For more information see: https://github.com/denolib/setup-deno

name: Deno

on:
push:
branches: [master]
pull_request:
branches: [master]

jobs:
test:
runs-on: ${{ matrix.os }} # runs a test on Ubuntu, Windows and macOS

strategy:
matrix:
deno: ["v1.x", "nightly"]
os: [macOS-latest, windows-latest, ubuntu-latest]

steps:
- name: Setup repo
uses: actions/checkout@v2

- name: Setup Deno
uses: denolib/setup-deno@v2
with:
deno-version: ${{ matrix.deno }} # tests across multiple Deno versions

- name: Cache Dependencies
run: deno cache deps.ts

- name: Run Tests
run: deno test -A --unstable
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"deno.enable": true
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Santiago Aguilar Hernández

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 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.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# cjs ![Deno](https://github.com/sant123/cjs-vars/workflows/Deno/badge.svg)

Create variables like `__dirname` and `__filename` in Deno. Their behavior is the same as Node.js variables.

## Usage

```ts
// example.ts
import { getDirname, getFilename } from 'https://deno.land/x/cjs/mod.ts';

const __dirname = getDirname(import.meta.url);
const __filename = getFilename(import.meta.url);

// Your code here...
```

## API

### getDirname(importMetaUrl: string): string

The directory name of the current module.

### getFilename(importMetaUrl: string): string

The file name of the current module. This is the current module file's absolute path.

## Node.js support

This should work with the [stable](https://nodejs.org/dist/latest-v15.x/docs/api/esm.html#esm_modules_ecmascript_modules) Ecmascript modules implementation of Node.js. See this [link](https://nodejs.org/dist/latest-v15.x/docs/api/esm.html#esm_no_filename_or_dirname) for more information.

## Testing

This library is highly tested to provide the same variables behavior of Node.js. If you see something is missing or ¿did you find a bug? pull requests are really welcome.
2 changes: 2 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { getDirname } from './src/get_dirname.ts';
export { getFilename } from './src/get_filename.ts';
5 changes: 5 additions & 0 deletions src/get_dirname.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const removeProtocolAndFilenameRegex = /\w+:\/\/(.*)\/[^/]+$/;

export function getDirname(importMetaUrl: string): string {
return importMetaUrl.replace(removeProtocolAndFilenameRegex, '$1');
}
5 changes: 5 additions & 0 deletions src/get_filename.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const removeProtocolRegex = /\w+:\/\//;

export function getFilename(importMetaUrl: string): string {
return importMetaUrl.replace(removeProtocolRegex, '');
}
49 changes: 49 additions & 0 deletions tests/get_dirname.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { assertEquals } from 'https://deno.land/[email protected]/testing/asserts.ts';
import { getDirname } from '../mod.ts';

Deno.test('Get dirname from a simple directory', () => {
const __dirname = getDirname(
'file:///Users/santiago.aguilar/Desktop/index.ts',
);

assertEquals(__dirname, '/Users/santiago.aguilar/Desktop');
});

Deno.test('Get dirname with a directory with spaces', () => {
const __dirname = getDirname(
'file:///Users/santiago.aguilar/Desktop/dir%20with%20spaces/index.ts',
);

assertEquals(
__dirname,
'/Users/santiago.aguilar/Desktop/dir%20with%20spaces',
);
});

Deno.test('Get dirname with a directory with dots', () => {
const __dirname = getDirname(
'file:///Users/santiago.aguilar/Desktop/dir.with.dots/index.ts',
);

assertEquals(__dirname, '/Users/santiago.aguilar/Desktop/dir.with.dots');
});

Deno.test('Get dirname with a directory with dashes', () => {
const __dirname = getDirname(
'file:///Users/santiago.aguilar/Desktop/dir-with-dashes/index.ts',
);

assertEquals(__dirname, '/Users/santiago.aguilar/Desktop/dir-with-dashes');
});

Deno.test('Get dirname with a file without extension', () => {
const __dirname = getDirname('file:///Users/santiago.aguilar/Desktop/index');

assertEquals(__dirname, '/Users/santiago.aguilar/Desktop');
});

Deno.test('Get dirname with a hidden file', () => {
const __dirname = getDirname('file:///Users/santiago.aguilar/Desktop/.index');

assertEquals(__dirname, '/Users/santiago.aguilar/Desktop');
});
59 changes: 59 additions & 0 deletions tests/get_filename.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { assertEquals } from 'https://deno.land/[email protected]/testing/asserts.ts';
import { getFilename } from '../mod.ts';

Deno.test('Get filename from a simple directory', () => {
const __filename = getFilename(
'file:///Users/santiago.aguilar/Desktop/index.ts',
);

assertEquals(__filename, '/Users/santiago.aguilar/Desktop/index.ts');
});

Deno.test('Get filename with a directory with spaces', () => {
const __filename = getFilename(
'file:///Users/santiago.aguilar/Desktop/dir%20with%20spaces/index.ts',
);

assertEquals(
__filename,
'/Users/santiago.aguilar/Desktop/dir%20with%20spaces/index.ts',
);
});

Deno.test('Get filename with a directory with dots', () => {
const __filename = getFilename(
'file:///Users/santiago.aguilar/Desktop/dir.with.dots/index.ts',
);

assertEquals(
__filename,
'/Users/santiago.aguilar/Desktop/dir.with.dots/index.ts',
);
});

Deno.test('Get filename with a directory with dashes', () => {
const __filename = getFilename(
'file:///Users/santiago.aguilar/Desktop/dir-with-dashes/index.ts',
);

assertEquals(
__filename,
'/Users/santiago.aguilar/Desktop/dir-with-dashes/index.ts',
);
});

Deno.test('Get filename without extension', () => {
const __filename = getFilename(
'file:///Users/santiago.aguilar/Desktop/index',
);

assertEquals(__filename, '/Users/santiago.aguilar/Desktop/index');
});

Deno.test('Get filename in a hidden file', () => {
const __filename = getFilename(
'file:///Users/santiago.aguilar/Desktop/.index',
);

assertEquals(__filename, '/Users/santiago.aguilar/Desktop/.index');
});

0 comments on commit 82f30fb

Please sign in to comment.