• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2025 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
16import { AssertResult } from '../modal/assertModel';
17import { AnyType } from '../types/common';
18
19export function assertInstanceOf(actualValue: AnyType, expected: string[]): AssertResult {
20  const type = typeof actualValue;
21  let pass = false;
22  let message = '';
23  const expectType = expected[0];
24  if (type !== 'object' && type === expectType.toLowerCase()) {
25    pass = true;
26  } else if (actualValue === null && expectType === 'Null') {
27    pass = true;
28  } else if (Array.isArray(actualValue) && expectType === 'Array') {
29    pass = true;
30  } else if (actualValue instanceof Date && expectType === 'Date') {
31    pass = true;
32  } else if (actualValue instanceof RegExp && expectType === 'RegExp') {
33    pass = true;
34  } else if (actualValue instanceof Promise && expectType === 'Promise') {
35    pass = true;
36  } else if (actualValue instanceof Map && expectType === 'Map') {
37    pass = true;
38  } else if (actualValue instanceof Set && expectType === 'Set') {
39    pass = true;
40  } else if (actualValue instanceof WeakMap && expectType === 'WeakMap') {
41    pass = true;
42  } else if (actualValue instanceof WeakSet && expectType === 'WeakSet') {
43    pass = true;
44  } else if (actualValue instanceof Object && expectType === 'Object') {
45    pass = true;
46  } else {
47    return {
48      pass: false,
49      message: actualValue + ' is not  ' + expectType,
50    };
51  }
52  return {
53    pass,
54    message,
55  };
56}
57