• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * @file Describe the file
3 * Copyright (c) 2023 Huawei Device Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import { isEmptyStr } from './TextUtils'
18
19export const QUERY_START = '?';
20
21export const QUERY_SPLIT = '&';
22
23export const QUERY_CONNECTOR = '=';
24
25export const BUNDLE_NAME = 'bundleName';
26
27export const TOKEN_ID = 'tokenId';
28
29/**
30 * parse query params from Uri, like:
31 * xxx?params1=value1&params2=value2&params3=value3
32 *
33 * @param url the url to be parsed.
34 * @return map Describes the key-value pair of the query parameter.
35 */
36export function getQueries(url: string): Map<string, string> | undefined {
37  if (url === null || url === undefined) {
38    return undefined;
39  }
40  let startIndex = url.indexOf(QUERY_START) + 1;
41  const totalLength = url.length;
42  if (startIndex < 1 || startIndex >= totalLength) {
43    return undefined;
44  }
45  const query = url.substring(startIndex);
46  if (query === null || query === undefined || query.length < 1) {
47    return undefined;
48  }
49  const array = query.split(QUERY_SPLIT);
50  if (array === null || array === undefined || array.length < 1) {
51    return undefined;
52  }
53  const map = new Map<string, string>();
54  for (let i = 0; i < array.length; i++) {
55    const item = array[i];
56    if (isEmptyStr(item)) {
57      continue;
58    }
59
60    const connectorIndex = item.indexOf(QUERY_CONNECTOR);
61    if (connectorIndex <= 0 || connectorIndex + 1 >= item.length) {
62      // QUERY_CONNECTOR at the first or last index is invalid
63      continue;
64    }
65    const key = item.substring(0, connectorIndex);
66    const value = item.substring(connectorIndex + 1);
67    map.set(key, value);
68  }
69  return map;
70}
71
72/**
73 * transfer the user input uri to TokenID
74 *
75 * @param uri the user input uri
76 * @return the tokenID coming from user's input.if TokenID is undefined, this function will return ""
77 */
78export function getTokenIDByUri(uri: string): string {
79  let queries = getQueries(uri);
80  if (queries && queries.has(TOKEN_ID)) {
81    return queries.get(TOKEN_ID) as string;
82  }
83  return "";
84}
85
86/**
87 * transfer the user input uri to TokenID
88 *
89 * @param uri the user input uri
90 * @return the bundleName coming from user's input.if TokenID is undefined, this function will return ""
91 */
92export function getBundleNameByUri(uri: string): string {
93  let queries = getQueries(uri);
94  if (queries && queries.has(BUNDLE_NAME)) {
95    return queries.get(BUNDLE_NAME) as string;
96  }
97  return "";
98}
99
100/**
101 * transfer the user input uri to BundleNameAndTokenIdFromUri
102 *
103 * @param uri the user input uri
104 * @return the object containing bundleName and tokenID come from user's input.
105 * if bundleName or TokenID is undefined, this function will return null object.
106 */
107export function getBundleNameAndTokenIDByUri(uri: string): BundleNameAndTokenIdFromUri {
108  let queries = getQueries(uri);
109  let bundleNameAndTokenIdFromUri: BundleNameAndTokenIdFromUri = new BundleNameAndTokenIdFromUri();
110  if (queries && queries.has(TOKEN_ID) && queries.has(BUNDLE_NAME)) {
111    bundleNameAndTokenIdFromUri = {
112      bundleName: queries.get(BUNDLE_NAME) as string,
113      tokenId: queries.get(TOKEN_ID) as string
114    };
115    return bundleNameAndTokenIdFromUri;
116  }
117  return bundleNameAndTokenIdFromUri;
118}
119
120/**
121 * the structure of BundleName and tokenId which are got from user's uri.
122 *
123 * @since 2022-10-29
124 */
125export class BundleNameAndTokenIdFromUri {
126  bundleName: string = '';
127  tokenId: string = '';
128}
129