• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022-2023 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
16class DeepTypeUtils {
17    static getType_(value) {
18       return Object.prototype.toString.apply(value);
19   }
20    static isA_(typeName, value) {
21        return this.getType_(value) === '[object ' + typeName + ']';
22    }
23    static isAsymmetricEqualityTester_(obj) {
24        return obj ? this.isA_('Function', obj.asymmetricMatch) : false;
25    }
26
27    /**
28     * 是否是function
29     * @param value
30     */
31    static isFunction_(value) {
32        return this.isA_('Function', value);
33    }
34
35    /**
36     * 是否是undefined
37     * @param obj
38     */
39    static isUndefined(obj) {
40        return obj === void 0;
41    }
42
43    /**
44     * 是否是Node
45     * @param obj
46     */
47    static isDomNode(obj) {
48        return obj !== null &&
49              typeof obj === 'object' &&
50              typeof obj.nodeType === 'number' &&
51              typeof obj.nodeName === 'string';
52    }
53
54    /**
55     * 是否是promise对象
56     * @param obj
57     */
58    static isPromise (obj) {
59        return !!obj && obj.constructor === Promise;
60  };
61    /**
62     *  是否是map对象
63     * @param obj
64     */
65    static isMap(obj) {
66        return (
67            obj !== null &&
68            typeof obj !== 'undefined' &&
69            obj.constructor === Map
70        );
71    }
72
73    /**
74     * 是否是set对象
75     * @param obj 对象
76     */
77    static isSet(obj) {
78        return (
79            obj !== null &&
80            typeof obj !== 'undefined' &&
81            obj.constructor === Set
82        );
83    }
84
85    /**
86     * 对象是否有key属性
87     * @param obj 对象
88     * @param key 对象属性名称
89     */
90    static has(obj, key) {
91        return Object.prototype.hasOwnProperty.call(obj, key);
92    }
93
94    /**
95     * 获取对象的自有属性
96     * @param obj 对象
97     * @param isArray 是否是数组,[object Array]
98     */
99    static keys(obj, isArray) {
100     const extraKeys = [];
101        // 获取对象所有属性
102     const  allKeys = this.getAllKeys(obj);
103        if (!isArray) {
104            return allKeys;
105        }
106        if (allKeys.length === 0) {
107            return allKeys;
108        }
109        for (const k of allKeys) {
110            if (typeof k === 'symbol' || !/^[0-9]+$/.test(k)) {
111                extraKeys.push(k);
112            }
113        }
114        return extraKeys;
115    }
116
117    /**
118     * 获取obj对象的所有属性
119     * @param obj obj对象
120     */
121    static getAllKeys(obj) {
122        const keys = [];
123        for (let key in obj) {
124            if(this.has(obj, key)) {
125                keys.push(key);
126            }
127        }
128        const symbols = Object.getOwnPropertySymbols(obj);
129        for (const sym of symbols) {
130            if (obj.propertyIsEnumerable(sym)) {
131                keys.push(sym);
132            }
133        }
134        return keys;
135    }
136
137}
138export default DeepTypeUtils;