1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15const URI_SPLIT = '/';
16
17let dataUriUtils = {
18  getId: (uri) => {
19    console.debug('DataUriUtils getId called.');
20    if (typeof uri !== 'string') {
21      return -1;
22    }
23    let index = uri.lastIndexOf(URI_SPLIT);
24    if (index === -1) {
25      return -1;
26    }
27    let ret = uri.substring(index + 1);
28    if (ret === '' || isNaN(Number(ret))) {
29      return -1;
30    }
31    return Number(ret);
32  },
33  updateId: (uri, id) => {
34    console.debug('DataUriUtils updateId called.');
35    if (typeof uri !== 'string' || typeof id !== 'number') {
36      return uri;
37    }
38    let ret = dataUriUtils.deleteId(uri);
39    if (ret === uri) {
40      return uri;
41    }
42    return ret + URI_SPLIT + id;
43  },
44  deleteId: (uri) => {
45    console.debug('DataUriUtils deleteId called.');
46    if (typeof uri !== 'string') {
47      return uri;
48    }
49    let index = uri.lastIndexOf(URI_SPLIT);
50    if (index === -1) {
51      return uri;
52    }
53    let id = uri.substring(index + 1);
54    if (id === '' || isNaN(Number(id))) {
55      return uri;
56    }
57    return uri.substring(0, index);
58  },
59  attachId: (uri, id) => {
60    console.debug('DataUriUtils attachId called.');
61    if (typeof uri !== 'string' || typeof id !== 'number') {
62      return uri;
63    }
64    return uri + URI_SPLIT + id;
65  }
66};
67
68export default dataUriUtils;