Skip to content

Commit a6ef584

Browse files
merlinnotthechenky
authored andcommitted
Extract and document path manipulation utilities (#514)
1 parent 5363a48 commit a6ef584

File tree

6 files changed

+63
-40
lines changed

6 files changed

+63
-40
lines changed

spec/index.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,5 @@ import './providers/pubsub.spec';
4343
import './providers/remoteConfig.spec';
4444
import './providers/storage.spec';
4545
import './setup.spec';
46+
import './utilities/path.spec';
4647
import './utils.spec';

spec/utilities/path.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { expect } from 'chai';
2+
import { normalizePath, pathParts } from '../../src/utilities/path';
3+
4+
describe('utilities', () => {
5+
describe('path', () => {
6+
describe('#normalizePath', () => {
7+
it('should strip leading and trailing slash', () => {
8+
expect(normalizePath('/my/path/is/{rad}/')).to.eq('my/path/is/{rad}');
9+
});
10+
});
11+
12+
describe('#pathParts', () => {
13+
it('should turn a path into an array of strings', () => {
14+
expect(pathParts('/foo/bar/baz')).to.deep.equal(['foo', 'bar', 'baz']);
15+
});
16+
17+
it('should turn a root path, empty string, or null path into an empty array', () => {
18+
expect(pathParts('')).to.deep.equal([]);
19+
expect(pathParts(null)).to.deep.equal([]);
20+
expect(pathParts('/')).to.deep.equal([]);
21+
});
22+
});
23+
});
24+
});

spec/utils.spec.ts

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,9 @@
2121
// SOFTWARE.
2222

2323
import { expect } from 'chai';
24-
import { applyChange, normalizePath, pathParts } from '../src/utils';
24+
import { applyChange } from '../src/utils';
2525

2626
describe('utils', () => {
27-
describe('.normalizePath(path: string)', () => {
28-
it('should strip leading and trailing slash', () => {
29-
expect(normalizePath('/my/path/is/{rad}/')).to.eq('my/path/is/{rad}');
30-
});
31-
});
32-
33-
describe('.pathParts(path: string): string[]', () => {
34-
it('should turn a path into an array of strings', () => {
35-
expect(pathParts('/foo/bar/baz')).to.deep.equal(['foo', 'bar', 'baz']);
36-
});
37-
38-
it('should turn a root path, empty string, or null path into an empty array', () => {
39-
expect(pathParts('')).to.deep.equal([]);
40-
expect(pathParts(null)).to.deep.equal([]);
41-
expect(pathParts('/')).to.deep.equal([]);
42-
});
43-
});
44-
4527
describe('.applyChange(from: any, to: any): any', () => {
4628
it('should return the to value for non-object values of from and to', () => {
4729
expect(applyChange({ a: 'b' }, null)).to.eq(null);

src/providers/database.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ import {
3232
} from '../cloud-functions';
3333
import { firebaseConfig } from '../config';
3434
import { DeploymentOptions } from '../function-configuration';
35-
import { applyChange, joinPath, normalizePath, pathParts } from '../utils';
35+
import { joinPath, normalizePath, pathParts } from '../utilities/path';
36+
import { applyChange } from '../utils';
3637

3738
/** @hidden */
3839
export const provider = 'google.firebase.database';

src/utilities/path.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Removes leading and trailing slashes from a path.
3+
*
4+
* @param path A path to normalize, in POSIX format.
5+
*/
6+
export function normalizePath(path: string): string {
7+
if (!path) {
8+
return '';
9+
}
10+
return path.replace(/^\//, '').replace(/\/$/, '');
11+
}
12+
13+
/**
14+
* Normalizes a given path and splits it into an array of segments.
15+
*
16+
* @param path A path to split, in POSIX format.
17+
*/
18+
export function pathParts(path: string): string[] {
19+
if (!path || path === '' || path === '/') {
20+
return [];
21+
}
22+
return normalizePath(path).split('/');
23+
}
24+
25+
/**
26+
* Normalizes given paths and joins these together using a POSIX separator.
27+
*
28+
* @param base A first path segment, in POSIX format.
29+
* @param child A second path segment, in POSIX format.
30+
*/
31+
export function joinPath(base: string, child: string) {
32+
return pathParts(base)
33+
.concat(pathParts(child))
34+
.join('/');
35+
}

src/utils.ts

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,6 @@
2222

2323
import * as _ from 'lodash';
2424

25-
export function normalizePath(path: string): string {
26-
if (!path) {
27-
return '';
28-
}
29-
return path.replace(/^\//, '').replace(/\/$/, '');
30-
}
31-
32-
export function pathParts(path: string): string[] {
33-
if (!path || path === '' || path === '/') {
34-
return [];
35-
}
36-
return normalizePath(path).split('/');
37-
}
38-
39-
export function joinPath(base: string, child: string) {
40-
return pathParts(base)
41-
.concat(pathParts(child))
42-
.join('/');
43-
}
44-
4525
export function applyChange(src: any, dest: any) {
4626
// if not mergeable, don't merge
4727
if (!_.isPlainObject(dest) || !_.isPlainObject(src)) {

0 commit comments

Comments
 (0)