UNPKG

3.21 kBJavaScriptView Raw
1// Copyright 2013 Lovell Fuller and others.
2// SPDX-License-Identifier: Apache-2.0
3
4'use strict';
5
6/**
7 * Is this value defined and not null?
8 * @private
9 */
10const defined = function (val) {
11 return typeof val !== 'undefined' && val !== null;
12};
13
14/**
15 * Is this value an object?
16 * @private
17 */
18const object = function (val) {
19 return typeof val === 'object';
20};
21
22/**
23 * Is this value a plain object?
24 * @private
25 */
26const plainObject = function (val) {
27 return Object.prototype.toString.call(val) === '[object Object]';
28};
29
30/**
31 * Is this value a function?
32 * @private
33 */
34const fn = function (val) {
35 return typeof val === 'function';
36};
37
38/**
39 * Is this value a boolean?
40 * @private
41 */
42const bool = function (val) {
43 return typeof val === 'boolean';
44};
45
46/**
47 * Is this value a Buffer object?
48 * @private
49 */
50const buffer = function (val) {
51 return val instanceof Buffer;
52};
53
54/**
55 * Is this value a typed array object?. E.g. Uint8Array or Uint8ClampedArray?
56 * @private
57 */
58const typedArray = function (val) {
59 if (defined(val)) {
60 switch (val.constructor) {
61 case Uint8Array:
62 case Uint8ClampedArray:
63 case Int8Array:
64 case Uint16Array:
65 case Int16Array:
66 case Uint32Array:
67 case Int32Array:
68 case Float32Array:
69 case Float64Array:
70 return true;
71 }
72 }
73
74 return false;
75};
76
77/**
78 * Is this value an ArrayBuffer object?
79 * @private
80 */
81const arrayBuffer = function (val) {
82 return val instanceof ArrayBuffer;
83};
84
85/**
86 * Is this value a non-empty string?
87 * @private
88 */
89const string = function (val) {
90 return typeof val === 'string' && val.length > 0;
91};
92
93/**
94 * Is this value a real number?
95 * @private
96 */
97const number = function (val) {
98 return typeof val === 'number' && !Number.isNaN(val);
99};
100
101/**
102 * Is this value an integer?
103 * @private
104 */
105const integer = function (val) {
106 return Number.isInteger(val);
107};
108
109/**
110 * Is this value within an inclusive given range?
111 * @private
112 */
113const inRange = function (val, min, max) {
114 return val >= min && val <= max;
115};
116
117/**
118 * Is this value within the elements of an array?
119 * @private
120 */
121const inArray = function (val, list) {
122 return list.includes(val);
123};
124
125/**
126 * Create an Error with a message relating to an invalid parameter.
127 *
128 * @param {string} name - parameter name.
129 * @param {string} expected - description of the type/value/range expected.
130 * @param {*} actual - the value received.
131 * @returns {Error} Containing the formatted message.
132 * @private
133 */
134const invalidParameterError = function (name, expected, actual) {
135 return new Error(
136 `Expected ${expected} for ${name} but received ${actual} of type ${typeof actual}`
137 );
138};
139
140/**
141 * Ensures an Error from C++ contains a JS stack.
142 *
143 * @param {Error} native - Error with message from C++.
144 * @param {Error} context - Error with stack from JS.
145 * @returns {Error} Error with message and stack.
146 * @private
147 */
148const nativeError = function (native, context) {
149 context.message = native.message;
150 return context;
151};
152
153module.exports = {
154 defined,
155 object,
156 plainObject,
157 fn,
158 bool,
159 buffer,
160 typedArray,
161 arrayBuffer,
162 string,
163 number,
164 integer,
165 inRange,
166 inArray,
167 invalidParameterError,
168 nativeError
169};