Skip to content

Commit

Permalink
feat: Updated Github Actions
Browse files Browse the repository at this point in the history
* Added `deno test --doc`
* Added `deno fmt --check`
* Using double quotes instead of single quotes
* Added deno settings to .vscode
* Updated function comments
  • Loading branch information
sant123 committed Sep 15, 2021
1 parent 7023df8 commit a9015a3
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 74 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/deno.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@ jobs:
with:
deno-version: v1.x

# Uncomment this step to verify the use of 'deno fmt' on each commit.
# - name: Verify formatting
# run: deno fmt --check
- name: Verify formatting
run: deno fmt --check

- name: Run linter
run: deno lint

- name: Run tests
run: deno test -A --unstable
run: deno test --doc
7 changes: 5 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"deno.enable": true
}
"deno.enable": true,
"deno.lint": true,
"deno.unstable": false,
"editor.defaultFormatter": "denoland.vscode-deno"
}
23 changes: 15 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# 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.
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';
import { getDirname, getFilename } from "https://deno.land/x/cjs/mod.ts";

const __dirname = getDirname(import.meta.url);
const __filename = getFilename(import.meta.url);
Expand All @@ -18,25 +19,31 @@ const __filename = getFilename(import.meta.url);

### getDirname(importMetaUrl: string): string

The directory name of the current module.
Returns the directory of the current module.

### getFilename(importMetaUrl: string): string

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

## 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.
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.
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.

## Benchmark

Tested with 1,000 records using Deno v1.14:

``` txt
```txt
/home/sant821/Desktop/projects/cjs/benchmark/
URL: 5ms
Expand All @@ -45,4 +52,4 @@ STD: 7ms
/home/sant821/Desktop/projects/cjs/benchmark
CJS: 1ms
```
```
30 changes: 15 additions & 15 deletions benchmark/get_dirname.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import {
fromFileUrl,
dirname,
} from 'https://deno.land/std@0.107.0/path/mod.ts';
fromFileUrl,
} from "https://deno.land/std@0.107.0/path/mod.ts";

import { getDirname } from '../mod.ts';
import { getDirname } from "../mod.ts";

const loops = 1000;

export const testURL = () => {
console.time('URL');
export function testURL(): void {
console.time("URL");

for (let i = 0; i < loops; i++) {
new URL('.', import.meta.url);
new URL(".", import.meta.url);
}

const __url = new URL('.', import.meta.url);
const __url = new URL(".", import.meta.url);
console.log(__url.pathname);

console.timeEnd('URL');
console.timeEnd("URL");
console.log();
};
}

export const testPathSTD = () => {
console.time('STD');
export function testPathSTD(): void {
console.time("STD");

for (let i = 0; i < loops; i++) {
dirname(fromFileUrl(import.meta.url));
Expand All @@ -31,12 +31,12 @@ export const testPathSTD = () => {
const __dirname = dirname(fromFileUrl(import.meta.url));
console.log(__dirname);

console.timeEnd('STD');
console.timeEnd("STD");
console.log();
};
}

export const testCJS = () => {
console.time('CJS');
console.time("CJS");

for (let i = 0; i < loops; i++) {
getDirname(import.meta.url);
Expand All @@ -45,6 +45,6 @@ export const testCJS = () => {
const __dirname = getDirname(import.meta.url);
console.log(__dirname);

console.timeEnd('CJS');
console.timeEnd("CJS");
console.log();
};
2 changes: 1 addition & 1 deletion benchmark/mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { testCJS, testPathSTD, testURL } from './get_dirname.ts';
import { testCJS, testPathSTD, testURL } from "./get_dirname.ts";

// new URL('.', import.meta.url);
testURL();
Expand Down
4 changes: 2 additions & 2 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { getDirname } from './src/get_dirname.ts';
export { getFilename } from './src/get_filename.ts';
export { getDirname } from "./src/get_dirname.ts";
export { getFilename } from "./src/get_filename.ts";
11 changes: 10 additions & 1 deletion src/get_dirname.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
const removeProtocolAndFilenameRegex = /\w+:\/\/(.*)\/[^/]+$/;

/**
* Returns the directory of the current module.
*
* ``` ts
* import { getDirname } from './get_dirname.ts';
* const __dirname = getDirname(import.meta.url);
*
* ```
*/
export function getDirname(importMetaUrl: string): string {
return importMetaUrl.replace(removeProtocolAndFilenameRegex, '$1');
return importMetaUrl.replace(removeProtocolAndFilenameRegex, "$1");
}
11 changes: 10 additions & 1 deletion src/get_filename.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
const removeProtocolRegex = /\w+:\/\//;

/**
* Returns the filename of the current module.
*
* ``` ts
* import { getFilename } from './get_filename.ts';
* const __filename = getFilename(import.meta.url);
*
* ```
*/
export function getFilename(importMetaUrl: string): string {
return importMetaUrl.replace(removeProtocolRegex, '');
return importMetaUrl.replace(removeProtocolRegex, "");
}
40 changes: 20 additions & 20 deletions tests/get_dirname.test.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
import { assertEquals } from 'https://deno.land/std@0.82.0/testing/asserts.ts';
import { getDirname } from '../mod.ts';
import { assertEquals } from "https://deno.land/std@0.82.0/testing/asserts.ts";
import { getDirname } from "../mod.ts";

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

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

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

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

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

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

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

assertEquals(__dirname, '/Users/santiago.aguilar/Desktop/dir-with-dashes');
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');
Deno.test("Get dirname with a file without extension", () => {
const __dirname = getDirname("file:///Users/santiago.aguilar/Desktop/index");

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

0 comments on commit a9015a3

Please sign in to comment.