• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 */
15
16let hilog = requireNapi('hilog');
17
18let domainID = 0xD001320;
19let TAG = 'JSENV';
20
21const URI_SPLIT = '/';
22
23let dataUriUtils = {
24  getId: (uri) => {
25    hilog.sLogD(domainID, TAG, 'DataUriUtils getId called.');
26    if (typeof uri !== 'string') {
27      return -1;
28    }
29    let index = uri.lastIndexOf(URI_SPLIT);
30    if (index === -1) {
31      return -1;
32    }
33    let ret = uri.substring(index + 1);
34    if (ret === '' || isNaN(Number(ret))) {
35      return -1;
36    }
37    return Number(ret);
38  },
39  updateId: (uri, id) => {
40    hilog.sLogD(domainID, TAG, 'DataUriUtils updateId called.');
41    if (typeof uri !== 'string' || typeof id !== 'number') {
42      return uri;
43    }
44    let ret = dataUriUtils.deleteId(uri);
45    if (ret === uri) {
46      return uri;
47    }
48    return ret + URI_SPLIT + id;
49  },
50  deleteId: (uri) => {
51    hilog.sLogD(domainID, TAG, 'DataUriUtils deleteId called.');
52    if (typeof uri !== 'string') {
53      return uri;
54    }
55    let index = uri.lastIndexOf(URI_SPLIT);
56    if (index === -1) {
57      return uri;
58    }
59    let id = uri.substring(index + 1);
60    if (id === '' || isNaN(Number(id))) {
61      return uri;
62    }
63    return uri.substring(0, index);
64  },
65  attachId: (uri, id) => {
66    hilog.sLogD(domainID, TAG, 'DataUriUtils attachId called.');
67    if (typeof uri !== 'string' || typeof id !== 'number') {
68      return uri;
69    }
70    return uri + URI_SPLIT + id;
71  }
72};
73
74export default dataUriUtils;