From 1758d5cc16104a202468564b37a788b153606346 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Tue, 6 Feb 2018 11:46:52 -0500 Subject: [PATCH 1/3] Add explicit URITemplate type to urijs types. * Strongly typed `expand` function on URITemplate that outlines possible values. --- types/urijs/index.d.ts | 15 ++++++++++++++- types/urijs/urijs-tests.ts | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/types/urijs/index.d.ts b/types/urijs/index.d.ts index 64d048a6a7..be4ee4cc00 100644 --- a/types/urijs/index.d.ts +++ b/types/urijs/index.d.ts @@ -234,6 +234,18 @@ declare namespace uri { withinString(source: string, func: (url: string) => string): string; } + type URITemplateValue = string | string[] | { [key: string] : string } | undefined | null; + type URITemplateCallback = (keyName: string) => URITemplateValue; + + interface URITemplate { + expand(data: { [key: string]: URITemplateValue | URITemplateCallback } | URITemplateCallback, opts?: Object) : URI; + } + + interface URITemplateStatic { + (template: string) : URITemplate; + + new (template: string) : URITemplate; + } } interface JQuery { @@ -241,6 +253,7 @@ interface JQuery { } declare var URI: uri.URIStatic; +declare var URITemplate : uri.URITemplateStatic; declare module 'URI' { export = URI; @@ -251,5 +264,5 @@ declare module 'urijs' { } declare module 'urijs/src/URITemplate' { - export = URI; + export = URITemplate; } diff --git a/types/urijs/urijs-tests.ts b/types/urijs/urijs-tests.ts index f520f0eaca..231879b94d 100644 --- a/types/urijs/urijs-tests.ts +++ b/types/urijs/urijs-tests.ts @@ -65,6 +65,44 @@ URI('http://user:pass@example.org:80/foo/bar.html?foo=bar&bar=baz#frag').equals( }) ); +// Basic URITemplate type usage +URI('http://user:pass@example.org:80/foo/bar.html?foo=bar&bar=baz#frag').equals( + URITemplate('http://user:pass@example.org:80{/p*}{?q*}{#h}').expand({ + p: ["foo", "bar.html"], + q: {foo: "bar", bar: "baz"}, + h: "frag" + }) +); + +// Using a callback for a specific key value. +URI('http://user:pass@example.org:80/foo/bar.html?foo=bar&bar=baz#frag').equals( + URITemplate('http://user:pass@example.org:80{/p*}{?q*}{#h}').expand({ + p: (key) => ["foo", "bar.html"], + q: {foo: "bar", bar: "baz"}, + h: "frag" + }) +); + +// Using a callback for entire data parameter. +URI('http://user:pass@example.org:80/foo/bar.html?foo=bar&bar=baz#frag').equals( + URITemplate('http://user:pass@example.org:80{/p*}{?q*}{#h}').expand((key) => { + switch(key) { + case 'p': return ["foo", "bar.html"]; + case '1': return {foo: "bar", bar: "baz"}; + case 'h': return "frag"; + } + }) +); + +// Supports null/undefined values for certain keys +URI('http://user:pass@example.org:80/foo/bar.html').equals( + URITemplate('http://user:pass@example.org:80{/p*}{?q*}{#h}').expand({ + p: ["foo", "bar.html"], + q: null, + h: undefined + }) +); + /* Tests for hasSearch(), hasQuery() From: http://medialize.github.io/URI.js/docs.html#search-has From 8e3b4d3f3f708a5494be5faf2b2200872f700699 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Tue, 6 Feb 2018 11:51:12 -0500 Subject: [PATCH 2/3] Add myself to `Definitions` header of urijs. --- types/urijs/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/urijs/index.d.ts b/types/urijs/index.d.ts index be4ee4cc00..6c1d04a0aa 100644 --- a/types/urijs/index.d.ts +++ b/types/urijs/index.d.ts @@ -1,6 +1,6 @@ // Type definitions for URI.js 1.15.1 // Project: https://github.com/medialize/URI.js -// Definitions by: RodneyJT , Brian Surowiec +// Definitions by: RodneyJT , Brian Surowiec , Pete Johanson // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 From 4ac19f3b7b38dcc2f554bf5f053365fe66f9d0c1 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Tue, 6 Feb 2018 11:56:47 -0500 Subject: [PATCH 3/3] Switch to using ReadonlyArray for URITemplateValue. --- types/urijs/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/urijs/index.d.ts b/types/urijs/index.d.ts index 6c1d04a0aa..4b5e9706ff 100644 --- a/types/urijs/index.d.ts +++ b/types/urijs/index.d.ts @@ -234,7 +234,7 @@ declare namespace uri { withinString(source: string, func: (url: string) => string): string; } - type URITemplateValue = string | string[] | { [key: string] : string } | undefined | null; + type URITemplateValue = string | ReadonlyArray | { [key: string] : string } | undefined | null; type URITemplateCallback = (keyName: string) => URITemplateValue; interface URITemplate {