Skip to content

Commit

Permalink
feat: added stripSlashes
Browse files Browse the repository at this point in the history
it strips out beginning and ending forward or backward slashes from the given path argument
  • Loading branch information
teclone committed Apr 20, 2019
1 parent 7b8247b commit 891176a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@ const alphabets = 'abcdefghijklmnopqrstuvwxyz';

const digits = '0123456789';

const units = {
'k': 1000,
'm': 1000000,
'g': 1000000000,
't': 1000000000000
}

export declare interface Callback {
(...args): any;
[propName: string]: any;
Expand Down Expand Up @@ -535,4 +528,12 @@ export const expandToNumeric = (size: number | string): number => {
else {
return 0;
}
};

/**
* strips out beginning and ending forward or backward slashes from the given path
* @param path the path to work on
*/
export const stripSlashes = (path: string): string => {
return path.replace(/^[\\/]+/, '').replace(/[\\/]+$/, '');
};
8 changes: 8 additions & 0 deletions tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -595,4 +595,12 @@ describe('Utils', function() {
expect(Utils.expandToNumeric('a00')).toEqual(0);
});
});

describe('.stripSlashes(path: string): string', function() {
it(`should strip beginning and ending backward or forward slashes from the given path`, function() {
expect(Utils.stripSlashes('/path/to/resource/')).toEqual('path/to/resource');
expect(Utils.stripSlashes('\\path/to/resource\\')).toEqual('path/to/resource');
expect(Utils.stripSlashes('\\/\\/path/to/resource\\/\\/')).toEqual('path/to/resource');
});
});
});

0 comments on commit 891176a

Please sign in to comment.