diff --git a/types/urijs/index.d.ts b/types/urijs/index.d.ts index 64d048a6a7..4b5e9706ff 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 @@ -234,6 +234,18 @@ declare namespace uri { withinString(source: string, func: (url: string) => string): string; } + type URITemplateValue = string | ReadonlyArray | { [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