1 | 'use strict';
|
2 |
|
3 | const _ = require('lodash');
|
4 | const DB = require('./clients').getClient;
|
5 |
|
6 | const isString = function(s) {
|
7 | return _.isString(s);
|
8 | };
|
9 |
|
10 | const isNumber = function(n) {
|
11 | return _.isNumber(n) && _.isFinite(n) && !isString(n);
|
12 | };
|
13 |
|
14 | const isBoolean = function(b) {
|
15 | return _.isBoolean(b);
|
16 | };
|
17 |
|
18 | const isDate = function(d) {
|
19 | return isNumber(d) || _.isDate(d) || isNumber(Date.parse(d));
|
20 | };
|
21 |
|
22 | const isBuffer = function(b) {
|
23 | return typeof b === 'object' || b instanceof Buffer;
|
24 | };
|
25 |
|
26 | const isObject = function(o) {
|
27 | return _.isObject(o);
|
28 | };
|
29 |
|
30 | const isArray = function(a) {
|
31 | return _.isArray(a);
|
32 | };
|
33 |
|
34 | const isDocument = function(m) {
|
35 | return m && m.documentClass && m.documentClass() === 'document';
|
36 | };
|
37 |
|
38 | const isEmbeddedDocument = function(e) {
|
39 | return e && e.documentClass && e.documentClass() === 'embedded';
|
40 | };
|
41 |
|
42 | const isReferenceable = function(r) {
|
43 | return isDocument(r) || isNativeId(r);
|
44 | };
|
45 |
|
46 | const isNativeId = function(n) {
|
47 | return DB().isNativeId(n);
|
48 | };
|
49 |
|
50 | const isSupportedType = function(t) {
|
51 | return (t === String || t === Number || t === Boolean ||
|
52 | t === Buffer || t === Date || t === Array ||
|
53 | isArray(t) || t === Object || t instanceof Object ||
|
54 | typeof(t.documentClass) === 'function');
|
55 | };
|
56 |
|
57 | const isType = function(value, type) {
|
58 | if (type === String) {
|
59 | return isString(value);
|
60 | } else if (type === Number) {
|
61 | return isNumber(value);
|
62 | } else if (type === Boolean) {
|
63 | return isBoolean(value);
|
64 | } else if (type === Buffer) {
|
65 | return isBuffer(value);
|
66 | } else if (type === Date) {
|
67 | return isDate(value);
|
68 | } else if (type === Array || isArray(type)) {
|
69 | return isArray(value);
|
70 | } else if (type === Object) {
|
71 | return isObject(value);
|
72 | } else if (type.documentClass && type.documentClass() === 'document') {
|
73 | return isDocument(value) || DB().isNativeId(value);
|
74 | } else if (type.documentClass && type.documentClass() === 'embedded') {
|
75 | return isEmbeddedDocument(value);
|
76 | } else if (type === DB().nativeIdType()) {
|
77 | return isNativeId(value);
|
78 | } else {
|
79 | throw new Error('Unsupported type: ' + type.name);
|
80 | }
|
81 | };
|
82 |
|
83 | const isValidType = function(value, type) {
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | if (value === null) return true;
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | if (value === undefined) return true;
|
96 |
|
97 |
|
98 | if (type === Array || isArray(type)) {
|
99 |
|
100 | if (isArray(type) && type.length > 1) {
|
101 | throw new Error('Unsupported type. Only one type can be specified in arrays, but multiple found:', + type);
|
102 | }
|
103 |
|
104 | if (isArray(type) && type.length === 1 && isArray(value)) {
|
105 | let arrayType = type[0];
|
106 | for (let i = 0; i < value.length; i++) {
|
107 | let v = value[i];
|
108 | if (!isType(v, arrayType)) {
|
109 | return false;
|
110 | }
|
111 | }
|
112 | } else if (isArray(type) && type.length === 0 && !isArray(value)) {
|
113 | return false;
|
114 | } else if (type === Array && !isArray(value)) {
|
115 | return false;
|
116 | }
|
117 |
|
118 | return true;
|
119 | }
|
120 |
|
121 | return isType(value, type);
|
122 | };
|
123 |
|
124 | const isInChoices = function(choices, choice) {
|
125 | if (!choices) {
|
126 | return true;
|
127 | }
|
128 | return choices.indexOf(choice) > -1;
|
129 | };
|
130 |
|
131 | const isEmptyValue = function(value) {
|
132 | return typeof value === 'undefined' || (!(typeof value === 'number' || value instanceof Date || typeof value === 'boolean')
|
133 | && (0 === Object.keys(value).length));
|
134 | };
|
135 |
|
136 | exports.isString = isString;
|
137 | exports.isNumber = isNumber;
|
138 | exports.isBoolean = isBoolean;
|
139 | exports.isDate = isDate;
|
140 | exports.isBuffer = isBuffer;
|
141 | exports.isObject = isObject;
|
142 | exports.isArray = isArray;
|
143 | exports.isDocument = isDocument;
|
144 | exports.isEmbeddedDocument = isEmbeddedDocument;
|
145 | exports.isReferenceable = isReferenceable;
|
146 | exports.isNativeId = isNativeId;
|
147 | exports.isSupportedType = isSupportedType;
|
148 | exports.isType = isType;
|
149 | exports.isValidType = isValidType;
|
150 | exports.isInChoices = isInChoices;
|
151 | exports.isEmptyValue = isEmptyValue; |
\ | No newline at end of file |