UNPKG

2.9 kBJavaScriptView Raw
1// Global object/function references
2var _Object = Object;
3var _Object_hasOwnProperty = _Object.hasOwnProperty;
4var _Array_isArray = Array.isArray;
5var _Error = Error;
6var _TypeError = TypeError;
7
8/** Reference to global Object.defineProperty */
9export var defineProperty = _Object.defineProperty;
10
11/**
12 * Checks if an object has a specified property as its own property (ignores prototype properties and `__proto__`)
13 * @param object Object to check
14 * @param key Property key
15 * @returns Does `object` have the property `key`?
16 */
17export function hasOwnProperty(object, key) {
18 return key !== "__proto__" && _Object_hasOwnProperty.call(object, key);
19}
20
21/**
22 * Creates an ECMAScript 6 Symbol, falling back to a simple string in environments that do not support Symbols
23 * @param description Symbol description
24 * @returns Symbol object or string
25 * @function
26 */
27/* c8 ignore start */
28/* istanbul ignore next */
29var createSymbol =
30 typeof Symbol === "function"
31 ? Symbol
32 : function (description) {
33 return "__" + description;
34 };
35/* c8 ignore stop */
36
37/** Hint property to indicate if an object has been observed */
38export var HINT_OBSERVE = createSymbol("observe");
39/** Hint property to indicate if a function has been disposed */
40export var HINT_DISPOSE = createSymbol("dispose");
41/** Hint property that contains a function's dependency disposal callbacks */
42export var HINT_DEPENDS = createSymbol("depends");
43
44/**
45 * Defines a hint property on an object
46 * @param object Object to define property on
47 * @param hint Property key
48 * @param {*} [value] Property value, property will be made non-configurable if this is unset (`undefined`)
49 */
50export function defineHint(object, hint, value) {
51 defineProperty(object, hint, {
52 value: value,
53 configurable: value !== void 0,
54 enumerable: false,
55 writable: false
56 });
57}
58
59/**
60 * Checks if a value is a normal object, ignores functions and arrays
61 * @param value Value to check
62 * @returns Is `value` a normal object?
63 */
64export function isObject(value) {
65 return value !== null && typeof value === "object" && !_Array_isArray(value);
66}
67
68/**
69 * Checks if a value is a function
70 * @param value Value to check
71 * @return Is `value` a function?
72 */
73export function isFunction(value) {
74 return typeof value === "function";
75}
76
77/** Error message printed when an argument is of an incorrect type (not a normal object) */
78export var MESSAGE_NOT_OBJECT = "Argument 'object' is not an object";
79/** Error message printed when an argument is of an incorrect type (not a function) */
80export var MESSAGE_NOT_FUNCTION = "Argument 'func' is not a function";
81
82/**
83 * Throws an error message
84 * @param message Message to construct the error with
85 * @param generic Should the more generic `Error` be thrown instead of `TypeError`?
86 */
87export function throwError(message, generic) {
88 throw new (generic ? _Error : _TypeError)(message);
89}