當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript URLSearchParams.get方法代碼示例

本文整理匯總了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");
});
開發者ID:guardian,項目名稱:pan-domain-authentication,代碼行數:9,代碼來源:utils.test.ts

示例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")
    };
}
開發者ID:guardian,項目名稱:pan-domain-authentication,代碼行數:41,代碼來源:utils.ts

示例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 });
開發者ID:Lavoaster,項目名稱:DefinitelyTyped,代碼行數:30,代碼來源:node-tests.ts


注:本文中的url.URLSearchParams.get方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。