Skip to content

Commit

Permalink
feat!: improved implementation with tests (#3)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Roe <daniel@roe.dev>
Co-authored-by: Pooya Parsa <pyapar@gmail.com>
  • Loading branch information
danielroe and pi0 authored Sep 27, 2021
1 parent b90fdaf commit d8d0cb5
Show file tree
Hide file tree
Showing 12 changed files with 1,211 additions and 161 deletions.
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"extends": [
"@nuxtjs/eslint-config-typescript"
]
],
"rules": {
"no-unused-expressions": "off"
}
}
7 changes: 6 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ on:

jobs:
ci:
runs-on: ubuntu-latest
strategy:
matrix:
os: [ubuntu-latest, windows-latest]

runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v2
Expand All @@ -21,3 +25,4 @@ jobs:
cache: yarn
- run: yarn install
- run: yarn lint
- run: yarn test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ node_modules
coverage
dist
types
.nyc_output
23 changes: 23 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,26 @@ 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.

--------------------------------------------------------------------------------

Copyright Joyent, Inc. and other Node contributors.

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.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ Read more about path utils from [Node.js documentation](https://nodejs.org/api/p

MIT. Made with 💖

Some code used form Node.js project. See [LICENSE](./LICENSE).

<!-- Refs -->
[npm-v-src]: https://img.shields.io/npm/v/pathe?style=flat-square
[npm-v-href]: https://npmjs.com/package/pathe
Expand Down
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,20 @@
"lint": "eslint --ext .ts .",
"prepublishOnly": "yarn build",
"release": "yarn test && standard-version && git push --follow-tags && npm publish",
"test": "yarn lint"
"test": "nyc mocha -r jiti/register test/*"
},
"devDependencies": {
"@nuxtjs/eslint-config-typescript": "latest",
"@types/chai": "^4.2.22",
"@types/mocha": "^9.0.0",
"@types/node": "^16.9.6",
"chai": "^4.3.4",
"eslint": "latest",
"jiti": "^1.12.3",
"mocha": "^9.1.1",
"nyc": "^15.1.0",
"siroc": "latest",
"standard-version": "latest",
"typescript": "latest"
"typescript": "^4.4.3"
}
}
92 changes: 4 additions & 88 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,94 +1,10 @@
// Reference https://nodejs.org/api/path.html
import path from 'path'

// Force posix contants
export const sep = '/'
export const delimiter = ':'

// Export platforms as is
export const win32 = path.win32
export const posix = path.posix

// extname
export const extname: typeof path.extname = function (p) {
return path.posix.extname(normalizeWindowsPath(p))
}

// normalize
export const normalize: typeof path.normalize = function (p) {
return path.posix.normalize(normalizeWindowsPath(p))
}

// join
export const join: typeof path.join = function (...args) {
return path.posix.join.apply(path.posix.join, args.map(arg => normalizeWindowsPath(arg)))
}

// relative
export const relative: typeof path.relative = function (from, to) {
return path.posix.relative(normalizeWindowsPath(from), normalizeWindowsPath(to))
}

// dirname
export const dirname: typeof path.dirname = function (p) {
return path.posix.dirname(normalizeWindowsPath(p))
}

// resolve
// TODO: Find a way to use path.posix for consistant behavior
export const resolve: typeof path.resolve = function (...args) {
return path.resolve.apply(path.resolve, args.map(arg => normalizeWindowsPath(arg)))
}

// format
export const format: typeof path.format = function (p) {
return normalizeWindowsPath(path.posix.format(p))
}

// basename
export const basename: typeof path.basename = function (p, ext) {
return path.posix.basename(normalizeWindowsPath(p), ext)
}

// parse
export const parse: typeof path.parse = function (p) {
return path.posix.parse(normalizeWindowsPath(p))
}

// toNamespacedPath
export const toNamespacedPath: typeof path.toNamespacedPath = function (p) {
return p
}

// isAbsolute
const _IS_ABSOLUTE_RE = /^\/|^\\|^[a-zA-Z]:[/\\]/
export const isAbsolute: typeof path.isAbsolute = function (p) {
return _IS_ABSOLUTE_RE.test(p)
}

// Util to normalize windows paths to posix
export function normalizeWindowsPath (input: string = '') {
if (!input.includes('\\')) {
return input
}
return input.replace(/\\/g, '/')
}
import * as _path from './path'
export * from './path'

// Default export
export default {
win32,
posix,
sep,
delimiter,
resolve,
normalize,
isAbsolute,
join,
relative,
toNamespacedPath,
dirname,
basename,
extname,
format,
parse
} as typeof path
..._path
} as Omit<typeof path, 'win32' | 'posix'>
Loading

0 comments on commit d8d0cb5

Please sign in to comment.