From c466d3dc68c8f9050d3ae69dcedd708e3509ae17 Mon Sep 17 00:00:00 2001 From: Alexander Fenster Date: Tue, 24 Mar 2020 17:20:59 -0700 Subject: [PATCH] fix: allow passing numbers as path template parameters (#756) --- src/pathTemplate.ts | 4 ++-- test/unit/{path_template.ts => pathTemplate.ts} | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) rename test/unit/{path_template.ts => pathTemplate.ts} (93%) diff --git a/src/pathTemplate.ts b/src/pathTemplate.ts index 2f96a38d0..1d16e289e 100644 --- a/src/pathTemplate.ts +++ b/src/pathTemplate.ts @@ -34,7 +34,7 @@ export interface Segment { } export interface Bindings { - [index: string]: string; + [index: string]: string | number; } export class PathTemplate { @@ -129,7 +129,7 @@ export class PathTemplate { ); throw new TypeError(msg); } - const tmp = new PathTemplate(bindings[segment.literal]); + const tmp = new PathTemplate(bindings[segment.literal].toString()); Array.prototype.push.apply(out, tmp.segments); inABinding = true; } else if (segment.kind === extras.END_BINDING) { diff --git a/test/unit/path_template.ts b/test/unit/pathTemplate.ts similarity index 93% rename from test/unit/path_template.ts rename to test/unit/pathTemplate.ts index 9cdf13975..0579ef67c 100644 --- a/test/unit/path_template.ts +++ b/test/unit/pathTemplate.ts @@ -139,6 +139,18 @@ describe('PathTemplate', () => { const want = 'bar/1/2/foo/3'; expect(template.render(params)).to.eql(want); }); + + it('should accept both strings and numbers as values', () => { + const template = new PathTemplate( + 'projects/{project}/sessions/{session}' + ); + const params = { + project: 'testProject', + session: 123, + }; + const want = 'projects/testProject/sessions/123'; + expect(template.render(params)).to.eql(want); + }); }); describe('method `inspect`', () => {