From 29a079a66b8d96315cb19451c99ab17fde06f5ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Natan=20S=C4=85gol?= Date: Wed, 10 Jul 2019 22:39:42 +0200 Subject: [PATCH] Extract and document path manipulation utilities --- spec/index.spec.ts | 1 + spec/utilities/path.spec.ts | 24 ++++++++++++++++++++++++ spec/utils.spec.ts | 20 +------------------- src/providers/database.ts | 3 ++- src/utilities/path.ts | 35 +++++++++++++++++++++++++++++++++++ src/utils.ts | 21 +-------------------- 6 files changed, 64 insertions(+), 40 deletions(-) create mode 100644 spec/utilities/path.spec.ts create mode 100644 src/utilities/path.ts diff --git a/spec/index.spec.ts b/spec/index.spec.ts index 64ffdf62e..0eb95045d 100644 --- a/spec/index.spec.ts +++ b/spec/index.spec.ts @@ -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'; diff --git a/spec/utilities/path.spec.ts b/spec/utilities/path.spec.ts new file mode 100644 index 000000000..c0a5dc318 --- /dev/null +++ b/spec/utilities/path.spec.ts @@ -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([]); + }); + }); + }); +}); diff --git a/spec/utils.spec.ts b/spec/utils.spec.ts index 57634dd72..96a66b834 100644 --- a/spec/utils.spec.ts +++ b/spec/utils.spec.ts @@ -21,27 +21,9 @@ // SOFTWARE. import { expect } from 'chai'; -import { applyChange, normalizePath, pathParts, valAt } from '../src/utils'; +import { applyChange, valAt } 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('.valAt(source: any, path?: string): any', () => { it('should be null if null along any point in the path', () => { expect(valAt(null)).to.be.null; diff --git a/src/providers/database.ts b/src/providers/database.ts index 7740a4373..ec6969909 100644 --- a/src/providers/database.ts +++ b/src/providers/database.ts @@ -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'; diff --git a/src/utilities/path.ts b/src/utilities/path.ts new file mode 100644 index 000000000..40ab8f047 --- /dev/null +++ b/src/utilities/path.ts @@ -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('/'); +} diff --git a/src/utils.ts b/src/utils.ts index 0ebbb38c4..e1effa09f 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -21,26 +21,7 @@ // SOFTWARE. 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('/'); -} +import { pathParts } from './utilities/path'; export function applyChange(src: any, dest: any) { // if not mergeable, don't merge