Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions spec/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ import './providers/pubsub.spec';
import './providers/remoteConfig.spec';
import './providers/storage.spec';
import './setup.spec';
import './utilities/path.spec';
import './utils.spec';
24 changes: 24 additions & 0 deletions spec/utilities/path.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { expect } from 'chai';
import { normalizePath, pathParts } from '../../src/utilities/path';

describe('utilities', () => {
describe('path', () => {
describe('#normalizePath', () => {
it('should strip leading and trailing slash', () => {
expect(normalizePath('/my/path/is/{rad}/')).to.eq('my/path/is/{rad}');
});
});

describe('#pathParts', () => {
it('should turn a path into an array of strings', () => {
expect(pathParts('/foo/bar/baz')).to.deep.equal(['foo', 'bar', 'baz']);
});

it('should turn a root path, empty string, or null path into an empty array', () => {
expect(pathParts('')).to.deep.equal([]);
expect(pathParts(null)).to.deep.equal([]);
expect(pathParts('/')).to.deep.equal([]);
});
});
});
});
20 changes: 1 addition & 19 deletions spec/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,9 @@
// SOFTWARE.

import { expect } from 'chai';
import { applyChange, normalizePath, pathParts } from '../src/utils';
import { applyChange } from '../src/utils';

describe('utils', () => {
describe('.normalizePath(path: string)', () => {
it('should strip leading and trailing slash', () => {
expect(normalizePath('/my/path/is/{rad}/')).to.eq('my/path/is/{rad}');
});
});

describe('.pathParts(path: string): string[]', () => {
it('should turn a path into an array of strings', () => {
expect(pathParts('/foo/bar/baz')).to.deep.equal(['foo', 'bar', 'baz']);
});

it('should turn a root path, empty string, or null path into an empty array', () => {
expect(pathParts('')).to.deep.equal([]);
expect(pathParts(null)).to.deep.equal([]);
expect(pathParts('/')).to.deep.equal([]);
});
});

describe('.applyChange(from: any, to: any): any', () => {
it('should return the to value for non-object values of from and to', () => {
expect(applyChange({ a: 'b' }, null)).to.eq(null);
Expand Down
3 changes: 2 additions & 1 deletion src/providers/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ import {
} from '../cloud-functions';
import { firebaseConfig } from '../config';
import { DeploymentOptions } from '../function-configuration';
import { applyChange, joinPath, normalizePath, pathParts } from '../utils';
import { joinPath, normalizePath, pathParts } from '../utilities/path';
import { applyChange } from '../utils';

/** @hidden */
export const provider = 'google.firebase.database';
Expand Down
35 changes: 35 additions & 0 deletions src/utilities/path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Removes leading and trailing slashes from a path.
*
* @param path A path to normalize, in POSIX format.
*/
export function normalizePath(path: string): string {
if (!path) {
return '';
}
return path.replace(/^\//, '').replace(/\/$/, '');
}

/**
* Normalizes a given path and splits it into an array of segments.
*
* @param path A path to split, in POSIX format.
*/
export function pathParts(path: string): string[] {
if (!path || path === '' || path === '/') {
return [];
}
return normalizePath(path).split('/');
}

/**
* Normalizes given paths and joins these together using a POSIX separator.
*
* @param base A first path segment, in POSIX format.
* @param child A second path segment, in POSIX format.
*/
export function joinPath(base: string, child: string) {
return pathParts(base)
.concat(pathParts(child))
.join('/');
}
20 changes: 0 additions & 20 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,6 @@

import * as _ from 'lodash';

export function normalizePath(path: string): string {
if (!path) {
return '';
}
return path.replace(/^\//, '').replace(/\/$/, '');
}

export function pathParts(path: string): string[] {
if (!path || path === '' || path === '/') {
return [];
}
return normalizePath(path).split('/');
}

export function joinPath(base: string, child: string) {
return pathParts(base)
.concat(pathParts(child))
.join('/');
}

export function applyChange(src: any, dest: any) {
// if not mergeable, don't merge
if (!_.isPlainObject(dest) || !_.isPlainObject(src)) {
Expand Down