1 |
|
2 |
|
3 |
|
4 | 'use strict';
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | const defined = function (val) {
|
11 | return typeof val !== 'undefined' && val !== null;
|
12 | };
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | const object = function (val) {
|
19 | return typeof val === 'object';
|
20 | };
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | const plainObject = function (val) {
|
27 | return Object.prototype.toString.call(val) === '[object Object]';
|
28 | };
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | const fn = function (val) {
|
35 | return typeof val === 'function';
|
36 | };
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | const bool = function (val) {
|
43 | return typeof val === 'boolean';
|
44 | };
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | const buffer = function (val) {
|
51 | return val instanceof Buffer;
|
52 | };
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | const 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 |
|
79 |
|
80 |
|
81 | const arrayBuffer = function (val) {
|
82 | return val instanceof ArrayBuffer;
|
83 | };
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | const string = function (val) {
|
90 | return typeof val === 'string' && val.length > 0;
|
91 | };
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | const number = function (val) {
|
98 | return typeof val === 'number' && !Number.isNaN(val);
|
99 | };
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | const integer = function (val) {
|
106 | return Number.isInteger(val);
|
107 | };
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 | const inRange = function (val, min, max) {
|
114 | return val >= min && val <= max;
|
115 | };
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | const inArray = function (val, list) {
|
122 | return list.includes(val);
|
123 | };
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 | const 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 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | const nativeError = function (native, context) {
|
149 | context.message = native.message;
|
150 | return context;
|
151 | };
|
152 |
|
153 | module.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 | };
|