本文整理汇总了TypeScript中url.URLSearchParams.get方法的典型用法代码示例。如果您正苦于以下问题:TypeScript URLSearchParams.get方法的具体用法?TypeScript URLSearchParams.get怎么用?TypeScript URLSearchParams.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类url.URLSearchParams
的用法示例。
在下文中一共展示了URLSearchParams.get方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: test
test("decode a cookie", () => {
const { data, signature } = parseCookie(sampleCookie);
expect(signature.length).toBe(684);
const params = new URLSearchParams(data);
expect(params.get("firstName")).toBe("Test");
expect(params.get("lastName")).toBe("User");
});
示例2: parseUser
export function parseUser(data: string): User {
const params = new URLSearchParams(data);
function stringField(name: string): string {
const value = params.get(name);
if(!value ) { throw new Error(`Missing ${name}`) }
return value;
}
function numberField(name: string): number {
const value = params.get(name);
if(!value) { throw new Error(`Missing ${name}`) }
return parseInt(value);
}
function booleanField(name: string): boolean {
return params.get(name) === 'true';
}
function stringListField(name: string): string[] {
const value = params.get(name);
if(!value) { throw new Error(`Missing ${name}`) }
return value.split(",");
}
const avatarUrl = params.get("avatarUrl");
return {
firstName: stringField("firstName"),
lastName: stringField("lastName"),
email: stringField("email"),
avatarUrl: avatarUrl ? avatarUrl : undefined,
authenticatingSystem: stringField("system"),
authenticatedIn: stringListField("authedIn"),
expires: numberField("expires"),
multifactor: booleanField("multifactor")
};
}
示例3:
myURL = new url.URL('/foo', 'https://example.org/');
assert.equal(myURL.href, 'https://example.org/foo');
assert.equal(myURL.toJSON(), myURL.href);
}
{
const searchParams = new url.URLSearchParams('abc=123');
assert.equal(searchParams.toString(), 'abc=123');
searchParams.forEach((value: string, name: string, me: url.URLSearchParams): void => {
assert.equal(name, 'abc');
assert.equal(value, '123');
assert.equal(me, searchParams);
});
assert.equal(searchParams.get('abc'), '123');
searchParams.append('abc', 'xyz');
assert.deepEqual(searchParams.getAll('abc'), ['123', 'xyz']);
const entries = searchParams.entries();
assert.deepEqual(entries.next(), { value: ["abc", "123"], done: false });
assert.deepEqual(entries.next(), { value: ["abc", "xyz"], done: false });
assert.deepEqual(entries.next(), { value: undefined, done: true });
const keys = searchParams.keys();
assert.deepEqual(keys.next(), { value: "abc", done: false });
assert.deepEqual(keys.next(), { value: "abc", done: false });
assert.deepEqual(keys.next(), { value: undefined, done: true });