Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export class Path {
- constructor(private readonly sys: Sys) {}
- private intoStack(pathLike: string): string[] {
- return pathLike.split('/').filter(n => n.length !== 0)
- }
- /** returns true if the path is absolute. */
- public isAbsolute(pathLike: string): boolean {
- return (pathLike.indexOf('/') === 0)
- }
- /** returns the `dirname` path for this path. */
- public dirname(pathLike: string): string {
- const stack = this.intoStack(pathLike)
- stack.pop()
- return stack.length > 0 ? stack.join('/') : '/'
- }
- /** Returns the `basename` for the given path. */
- public basename(pathLike: string): string {
- return this.intoStack(pathLike).pop() || ''
- }
- /** Returns the `extname` for the given path. */
- public extname(pathLike: string): string {
- const stack = this.intoStack(pathLike)
- const last = stack[stack.length - 1]
- const parts = last.split('.')
- return (!(parts.length === 1 || parts[0].length === 0))
- ? '.' + parts[parts.length - 1]
- : ''
- }
- public join(...pathLikes: string[]): string {
- return ''
- }
- public resolve(pathLike: string): string {
- return ''
- }
- public relative(from: string, to: string): string {
- return ''
- }
- public normalize(pathLike: string): string {
- const absolute = this.isAbsolute(pathLike)
- const normal = this.intoStack(pathLike).reduce((acc, current) => {
- if(current === '..') {
- if(absolute) {
- if(acc.length === 0) {
- return ['']
- } else if (acc[acc.length - 1] !== '..') {
- acc.pop()
- return acc
- } else {
- return [...acc, current]
- }
- } else {
- if(acc.length === 0) {
- return ['..']
- } else if (acc[acc.length - 1] !== '..') {
- acc.pop()
- return acc
- } else {
- return [...acc, current]
- }
- }
- }
- return (current !== '.')
- ? [...acc, current]
- : [...acc]
- }, [] as string[]).join('/')
- return (absolute && normal.indexOf('/') !== 0)
- ? ('/' + normal)
- : normal
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement