-
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.
* Added test cases * Created getDirname and getFilename functions * Created LICENSE * Created Github Actions * Created README
- Loading branch information
0 parents
commit 82f30fb
Showing
9 changed files
with
216 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,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 |
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,3 @@ | ||
{ | ||
"deno.enable": 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,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. |
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,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. |
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,2 @@ | ||
export { getDirname } from './src/get_dirname.ts'; | ||
export { getFilename } from './src/get_filename.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,5 @@ | ||
const removeProtocolAndFilenameRegex = /\w+:\/\/(.*)\/[^/]+$/; | ||
|
||
export function getDirname(importMetaUrl: string): string { | ||
return importMetaUrl.replace(removeProtocolAndFilenameRegex, '$1'); | ||
} |
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 @@ | ||
const removeProtocolRegex = /\w+:\/\//; | ||
|
||
export function getFilename(importMetaUrl: string): string { | ||
return importMetaUrl.replace(removeProtocolRegex, ''); | ||
} |
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,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'); | ||
}); |
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,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'); | ||
}); |