Skip to content

Commit ee31166

Browse files
committed
Move util.js to core module
1 parent 066863d commit ee31166

File tree

6 files changed

+292
-145
lines changed

6 files changed

+292
-145
lines changed

core/src/index.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
} from './error'
2727

2828
import Integer, { int, isInt, inSafeRange, toNumber, toString } from './integer'
29+
import * as internal from './internal' // todo: removed afterwards
2930

3031
/**
3132
* Object containing string constants representing predefined {@link Neo4jError} codes.
@@ -48,7 +49,8 @@ const forExport = {
4849
isInt,
4950
inSafeRange,
5051
toNumber,
51-
toString
52+
toString,
53+
internal
5254
}
5355

5456
export {
@@ -60,7 +62,8 @@ export {
6062
isInt,
6163
inSafeRange,
6264
toNumber,
63-
toString
65+
toString,
66+
internal
6467
}
6568

6669
export default forExport

core/src/internal/index.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
import * as util from './util'
21+
22+
export { util }

core/src/internal/util.ts

+227
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
/**
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
import Integer, { isInt } from '../integer'
21+
22+
const ENCRYPTION_ON: string = 'ENCRYPTION_ON'
23+
const ENCRYPTION_OFF: string = 'ENCRYPTION_OFF'
24+
25+
/**
26+
* Verifies if the object is null or empty
27+
* @param obj The subject object
28+
* @returns {boolean} True if it's empty object or null
29+
*/
30+
function isEmptyObjectOrNull(obj?: any): boolean {
31+
if (obj === null) {
32+
return true
33+
}
34+
35+
if (!isObject(obj)) {
36+
return false
37+
}
38+
39+
for (const prop in obj) {
40+
if (Object.prototype.hasOwnProperty.bind(obj, prop)) {
41+
return false
42+
}
43+
}
44+
45+
return true
46+
}
47+
48+
/**
49+
* Verify if it's an object
50+
* @param obj The subject
51+
* @returns {boolean} True if it's an object
52+
*/
53+
function isObject(obj: any): boolean {
54+
return typeof obj === 'object' && !Array.isArray(obj) && obj !== null
55+
}
56+
57+
/**
58+
* Check and normalize given query and parameters.
59+
* @param {string|{text: string, parameters: Object}} query the query to check.
60+
* @param {Object} parameters
61+
* @return {{validatedQuery: string|{text: string, parameters: Object}, params: Object}} the normalized query with parameters.
62+
* @throws TypeError when either given query or parameters are invalid.
63+
*/
64+
function validateQueryAndParameters(
65+
query: string | { text: string; parameters: Object },
66+
parameters: Object
67+
): {
68+
validatedQuery: string | { text: string; parameters: Object }
69+
params: Object
70+
} {
71+
let validatedQuery = query
72+
let params = parameters || {}
73+
74+
if (typeof query === 'object' && query.text) {
75+
validatedQuery = query.text
76+
params = query.parameters || {}
77+
}
78+
79+
assertCypherQuery(validatedQuery)
80+
assertQueryParameters(params)
81+
82+
return { validatedQuery, params }
83+
}
84+
85+
/**
86+
* Assert it's a object
87+
* @param {any} obj The subject
88+
* @param {string} objName The object name
89+
* @returns {object} The subject object
90+
* @throws {TypeError} when the supplied param is not an object
91+
*/
92+
function assertObject(obj: any, objName: string): Object {
93+
if (!isObject(obj)) {
94+
throw new TypeError(
95+
objName + ' expected to be an object but was: ' + JSON.stringify(obj)
96+
)
97+
}
98+
return obj
99+
}
100+
101+
/**
102+
* Assert it's a string
103+
* @param {any} obj The subject
104+
* @param {string} objName The object name
105+
* @returns {string} The subject string
106+
* @throws {TypeError} when the supplied param is not a string
107+
*/
108+
function assertString(obj: any, objName: Object): string {
109+
if (!isString(obj)) {
110+
throw new TypeError(
111+
objName + ' expected to be string but was: ' + JSON.stringify(obj)
112+
)
113+
}
114+
return obj
115+
}
116+
117+
/**
118+
* Assert it's a number
119+
* @param {any} obj The subject
120+
* @param {string} objName The object name
121+
* @returns {number} The number
122+
* @throws {TypeError} when the supplied param is not a number
123+
*/
124+
function assertNumber(obj: any, objName: string): Number {
125+
if (typeof obj !== 'number') {
126+
throw new TypeError(
127+
objName + ' expected to be a number but was: ' + JSON.stringify(obj)
128+
)
129+
}
130+
return obj
131+
}
132+
133+
/**
134+
* Assert it's a number or integer
135+
* @param {any} obj The subject
136+
* @param {string} objName The object name
137+
* @returns {number|Integer} The subject object
138+
* @throws {TypeError} when the supplied param is not a number or integer
139+
*/
140+
function assertNumberOrInteger(obj: any, objName: string): number | Integer {
141+
if (typeof obj !== 'number' && !isInt(obj)) {
142+
throw new TypeError(
143+
objName +
144+
' expected to be either a number or an Integer object but was: ' +
145+
JSON.stringify(obj)
146+
)
147+
}
148+
return obj
149+
}
150+
151+
/**
152+
* Assert it's a valid datae
153+
* @param {any} obj The subject
154+
* @param {string} objName The object name
155+
* @returns {Date} The valida date
156+
* @throws {TypeError} when the supplied param is not a valid date
157+
*/
158+
function assertValidDate(obj: any, objName: string): Date {
159+
if (Object.prototype.toString.call(obj) !== '[object Date]') {
160+
throw new TypeError(
161+
objName +
162+
' expected to be a standard JavaScript Date but was: ' +
163+
JSON.stringify(obj)
164+
)
165+
}
166+
if (Number.isNaN(obj.getTime())) {
167+
throw new TypeError(
168+
objName +
169+
' expected to be valid JavaScript Date but its time was NaN: ' +
170+
JSON.stringify(obj)
171+
)
172+
}
173+
return obj
174+
}
175+
176+
/**
177+
* Validates a cypher query string
178+
* @param {any} obj The query
179+
* @returns {void}
180+
* @throws {TypeError} if the query is not valid
181+
*/
182+
function assertCypherQuery(obj: any): void {
183+
assertString(obj, 'Cypher query')
184+
if (obj.trim().length === 0) {
185+
throw new TypeError('Cypher query is expected to be a non-empty string.')
186+
}
187+
}
188+
189+
/**
190+
* Validates if the query parameters is an object
191+
* @param {any} obj The parameters
192+
* @returns {void}
193+
* @throws {TypeError} if the parameters is not valid
194+
*/
195+
function assertQueryParameters(obj: any): void {
196+
if (!isObject(obj)) {
197+
// objects created with `Object.create(null)` do not have a constructor property
198+
const constructor = obj.constructor ? ' ' + obj.constructor.name : ''
199+
throw new TypeError(
200+
`Query parameters are expected to either be undefined/null or an object, given:${constructor} ${obj}`
201+
)
202+
}
203+
}
204+
205+
/**
206+
* Verify if the supplied object is a string
207+
*
208+
* @param str The string
209+
* @returns {boolean} True if the supplied object is an string
210+
*/
211+
function isString(str: any): boolean {
212+
return Object.prototype.toString.call(str) === '[object String]'
213+
}
214+
215+
export {
216+
isEmptyObjectOrNull,
217+
isObject,
218+
isString,
219+
assertObject,
220+
assertString,
221+
assertNumber,
222+
assertNumberOrInteger,
223+
assertValidDate,
224+
validateQueryAndParameters,
225+
ENCRYPTION_ON,
226+
ENCRYPTION_OFF
227+
}

src/index.js

+16-16
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,12 @@ import {
2424
Relationship,
2525
UnboundRelationship
2626
} from './graph-types'
27-
import {
28-
Neo4jError,
29-
error,
30-
Integer,
31-
inSafeRange,
32-
int,
33-
isInt,
34-
toNumber,
35-
toString
36-
} from 'neo4j-driver-core'
3727
import Result from './result'
3828
import ResultSummary from './result-summary'
3929
import Record from './record'
4030
import { Driver, READ, WRITE } from './driver'
4131
import RoutingDriver from './routing-driver'
4232
import VERSION from './version'
43-
import {
44-
ENCRYPTION_ON,
45-
ENCRYPTION_OFF,
46-
assertString,
47-
isEmptyObjectOrNull
48-
} from './internal/util'
4933
import urlUtil from './internal/url-util'
5034
import { isPoint, Point } from './spatial-types'
5135
import {
@@ -64,6 +48,22 @@ import {
6448
} from './temporal-types'
6549
import ServerAddress from './internal/server-address'
6650

51+
import {
52+
Neo4jError,
53+
error,
54+
Integer,
55+
inSafeRange,
56+
int,
57+
isInt,
58+
toNumber,
59+
toString,
60+
internal
61+
} from 'neo4j-driver-core'
62+
63+
const {
64+
util: { ENCRYPTION_ON, ENCRYPTION_OFF, assertString, isEmptyObjectOrNull }
65+
} = internal
66+
6767
/**
6868
* Construct a new Neo4j Driver. This is your main entry point for this
6969
* library.

0 commit comments

Comments
 (0)