19
19
20
20
import * as util from './util'
21
21
22
- const BOOKMARK_KEY = 'bookmark'
23
22
const BOOKMARKS_KEY = 'bookmarks'
24
- const BOOKMARK_PREFIX = 'neo4j:bookmark:v1:tx'
25
-
26
- const UNKNOWN_BOOKMARK_VALUE = - 1
27
23
28
24
export default class Bookmark {
29
25
/**
@@ -32,7 +28,6 @@ export default class Bookmark {
32
28
*/
33
29
constructor ( values ) {
34
30
this . _values = asStringArray ( values )
35
- this . _maxValue = maxBookmark ( this . _values )
36
31
}
37
32
38
33
static empty ( ) {
@@ -44,15 +39,7 @@ export default class Bookmark {
44
39
* @return {boolean } returns `true` bookmark has a value, `false` otherwise.
45
40
*/
46
41
isEmpty ( ) {
47
- return this . _maxValue === null
48
- }
49
-
50
- /**
51
- * Get maximum value of this bookmark as string.
52
- * @return {string|null } the maximum value or `null` if it is not defined.
53
- */
54
- maxBookmarkAsString ( ) {
55
- return this . _maxValue
42
+ return this . _values . length === 0
56
43
}
57
44
58
45
/**
@@ -77,7 +64,6 @@ export default class Bookmark {
77
64
// bookmark that is why driver has to parse and compare given list of bookmarks. This functionality will
78
65
// eventually be removed.
79
66
return {
80
- [ BOOKMARK_KEY ] : this . _maxValue ,
81
67
[ BOOKMARKS_KEY ] : this . _values
82
68
}
83
69
}
@@ -87,7 +73,7 @@ const EMPTY_BOOKMARK = new Bookmark(null)
87
73
88
74
/**
89
75
* Converts given value to an array.
90
- * @param {string|string[] } [value=undefined] argument to convert.
76
+ * @param {string|string[]|Array } [value=undefined] argument to convert.
91
77
* @return {string[] } value converted to an array.
92
78
*/
93
79
function asStringArray ( value ) {
@@ -101,13 +87,14 @@ function asStringArray (value) {
101
87
102
88
if ( Array . isArray ( value ) ) {
103
89
const result = [ ]
104
- for ( let i = 0 ; i < value . length ; i ++ ) {
105
- const element = value [ i ]
90
+ const flattenedValue = flattenArray ( value )
91
+ for ( let i = 0 ; i < flattenedValue . length ; i ++ ) {
92
+ const element = flattenedValue [ i ]
106
93
// if it is undefined or null, ignore it
107
94
if ( element !== undefined && element !== null ) {
108
95
if ( ! util . isString ( element ) ) {
109
96
throw new TypeError (
110
- `Bookmark should be a string, given: '${ element } '`
97
+ `Bookmark value should be a string, given: '${ element } '`
111
98
)
112
99
}
113
100
result . push ( element )
@@ -122,40 +109,17 @@ function asStringArray (value) {
122
109
}
123
110
124
111
/**
125
- * Find latest bookmark in the given array of bookmarks.
126
- * @param {string[] } bookmarks array of bookmarks.
127
- * @return {string|null } latest bookmark value.
128
- */
129
- function maxBookmark ( bookmarks ) {
130
- if ( ! bookmarks || bookmarks . length === 0 ) {
131
- return null
132
- }
133
-
134
- let maxBookmark = bookmarks [ 0 ]
135
- let maxValue = bookmarkValue ( maxBookmark )
136
-
137
- for ( let i = 1 ; i < bookmarks . length ; i ++ ) {
138
- const bookmark = bookmarks [ i ]
139
- const value = bookmarkValue ( bookmark )
140
-
141
- if ( value > maxValue ) {
142
- maxBookmark = bookmark
143
- maxValue = value
144
- }
145
- }
146
-
147
- return maxBookmark
148
- }
149
-
150
- /**
151
- * Calculate numeric value for the given bookmark.
152
- * @param {string } bookmark argument to get numeric value for.
153
- * @return {number } value of the bookmark.
112
+ * Recursively flattens an array so that the result becomes a single array
113
+ * of values, which does not include any sub-arrays
114
+ *
115
+ * @param {Array } value
154
116
*/
155
- function bookmarkValue ( bookmark ) {
156
- if ( bookmark && bookmark . indexOf ( BOOKMARK_PREFIX ) === 0 ) {
157
- const result = parseInt ( bookmark . substring ( BOOKMARK_PREFIX . length ) )
158
- return result || UNKNOWN_BOOKMARK_VALUE
159
- }
160
- return UNKNOWN_BOOKMARK_VALUE
117
+ function flattenArray ( values ) {
118
+ return values . reduce (
119
+ ( dest , value ) =>
120
+ Array . isArray ( value )
121
+ ? dest . concat ( flattenArray ( value ) )
122
+ : dest . concat ( value ) ,
123
+ [ ]
124
+ )
161
125
}
0 commit comments