From a9015a3e6616dc98c50b7efaad314e0720376ab7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Santiago=20Aguilar=20Hern=C3=A1ndez?= Date: Wed, 15 Sep 2021 14:25:35 -0500 Subject: [PATCH] feat: Updated Github Actions * Added `deno test --doc` * Added `deno fmt --check` * Using double quotes instead of single quotes * Added deno settings to .vscode * Updated function comments --- .github/workflows/deno.yml | 7 +++---- .vscode/settings.json | 7 +++++-- README.md | 23 ++++++++++++++-------- benchmark/get_dirname.ts | 30 ++++++++++++++-------------- benchmark/mod.ts | 2 +- mod.ts | 4 ++-- src/get_dirname.ts | 11 ++++++++++- src/get_filename.ts | 11 ++++++++++- tests/get_dirname.test.ts | 40 +++++++++++++++++++------------------- tests/get_filename.test.ts | 40 +++++++++++++++++++------------------- 10 files changed, 101 insertions(+), 74 deletions(-) diff --git a/.github/workflows/deno.yml b/.github/workflows/deno.yml index 92a59b2..7196b4b 100644 --- a/.github/workflows/deno.yml +++ b/.github/workflows/deno.yml @@ -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 diff --git a/.vscode/settings.json b/.vscode/settings.json index b943dbc..91edd55 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,6 @@ { - "deno.enable": true -} \ No newline at end of file + "deno.enable": true, + "deno.lint": true, + "deno.unstable": false, + "editor.defaultFormatter": "denoland.vscode-deno" +} diff --git a/README.md b/README.md index 91d4d1b..16ae0ed 100644 --- a/README.md +++ b/README.md @@ -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); @@ -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 @@ -45,4 +52,4 @@ STD: 7ms /home/sant821/Desktop/projects/cjs/benchmark CJS: 1ms -``` \ No newline at end of file +``` diff --git a/benchmark/get_dirname.ts b/benchmark/get_dirname.ts index 5258d7d..227c521 100644 --- a/benchmark/get_dirname.ts +++ b/benchmark/get_dirname.ts @@ -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)); @@ -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); @@ -45,6 +45,6 @@ export const testCJS = () => { const __dirname = getDirname(import.meta.url); console.log(__dirname); - console.timeEnd('CJS'); + console.timeEnd("CJS"); console.log(); }; diff --git a/benchmark/mod.ts b/benchmark/mod.ts index 849adf3..61507d8 100644 --- a/benchmark/mod.ts +++ b/benchmark/mod.ts @@ -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(); diff --git a/mod.ts b/mod.ts index e99f501..e3f5cb9 100644 --- a/mod.ts +++ b/mod.ts @@ -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"; diff --git a/src/get_dirname.ts b/src/get_dirname.ts index 22ad054..17a7943 100644 --- a/src/get_dirname.ts +++ b/src/get_dirname.ts @@ -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"); } diff --git a/src/get_filename.ts b/src/get_filename.ts index dc4ef60..ddfba5d 100644 --- a/src/get_filename.ts +++ b/src/get_filename.ts @@ -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, ""); } diff --git a/tests/get_dirname.test.ts b/tests/get_dirname.test.ts index ab631c9..a697e9f 100644 --- a/tests/get_dirname.test.ts +++ b/tests/get_dirname.test.ts @@ -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"); }); diff --git a/tests/get_filename.test.ts b/tests/get_filename.test.ts index 60f0092..58fd87d 100644 --- a/tests/get_filename.test.ts +++ b/tests/get_filename.test.ts @@ -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"); });