Merge pull request #23462 from petejohanson/urijs-uritemplate-type

Add explicit URITemplate type to urijs types.
This commit is contained in:
Ron Buckton
2018-02-07 16:02:39 -08:00
committed by GitHub
2 changed files with 53 additions and 2 deletions
+15 -2
View File
@@ -1,6 +1,6 @@
// Type definitions for URI.js 1.15.1
// Project: https://github.com/medialize/URI.js
// Definitions by: RodneyJT <https://github.com/RodneyJT>, Brian Surowiec <https://github.com/xt0rted>
// Definitions by: RodneyJT <https://github.com/RodneyJT>, Brian Surowiec <https://github.com/xt0rted>, Pete Johanson <https://github.com/petejohanson>
// 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<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;
}
+38
View File
@@ -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