-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathspace.js
executable file
·1983 lines (1655 loc) · 48.9 KB
/
space.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"use strict";
function Space(content) {
this._load(content)
}
Space._setTokens = (lineChar, assignmentChar) => {
Space._lineChar = lineChar || "\n"
Space._lineRegex = new RegExp(Space._lineChar, "g")
Space._assignmentChar = assignmentChar || " "
Space._assignmentRegEx = new RegExp(Space._assignmentChar, "g")
}
Space._setTokens()
Space.version = "0.22.2"
Space._isSpacePath = property => property.indexOf(Space._assignmentChar) > -1
Space.fromHeredoc = (content, start, end) => {
// Remove Windows newlines
content = content.replace(/\r/g, "")
const lines = content.split(Space._lineChar)
const linesToDelete = []
const startRegex = new RegExp("\^" + start + `(?:${Space._assignmentChar}|$)`)
const linesLength = lines.length
const startLength = start.length
const endRegex = new RegExp("\^" + end)
let startIndex = null
for (let i = 0; i < linesLength; i++) {
if (startIndex === null) {
if (lines[i].match(startRegex)) {
startIndex = i
// Make sure the key starts with a " " so its value is treated as a multiline
// string.
if (lines[i].length === startLength)
lines[i] = lines[i] + Space._assignmentChar
} else
continue
} else if (lines[i].match(endRegex)) {
startIndex = null
linesToDelete.push(i)
} else
lines[i] = Space._assignmentChar + lines[i]
}
Space._removeItems(lines, linesToDelete)
return new Space(lines.join(Space._lineChar))
}
Space.fromCsv = (str, hasHeaders) => {
return Space.fromDelimiter(str, ",", hasHeaders)
}
Space.fromDelimiter = (str, delimiter, hasHeaders, sanitizeString, quoteChar) => {
return Space._fromDelimiter(str, delimiter, hasHeaders, sanitizeString, quoteChar || `"`)
}
Space._fromDelimiter = (str, delimiter, hasHeaders, sanitizeString, quote) => {
if (sanitizeString !== false && str.indexOf("\r") > -1)
str = str.replace(/\r/g, "")
const rows = [[]]
const strHasQuotes = str.indexOf(quote) > -1
const newLine = "\n"
if (strHasQuotes) {
const length = str.length
let currentItem = ""
let inQuote = str.substr(0, 1) === quote
let currentPosition = inQuote ? 1 : 0
let nextChar
let isLastChar
let currentRow = 0
let c
let isNextCharAQuote
while (currentPosition < length) {
c = str[currentPosition]
isLastChar = currentPosition + 1 === length
nextChar = str[currentPosition + 1]
isNextCharAQuote = nextChar === quote
if (inQuote) {
if (c !== quote)
currentItem += c
// Both the current and next char are ", so the " is escaped
else if (isNextCharAQuote) {
currentItem += nextChar
currentPosition++ // Jump 2
}
// If the current char is a " and the next char is not, it's the end of the quotes
else {
inQuote = false
if (isLastChar)
rows[currentRow].push(currentItem)
}
} else {
if (c === delimiter) {
rows[currentRow].push(currentItem)
currentItem = ""
if (isNextCharAQuote) {
inQuote = true
currentPosition++ // Jump 2
}
}
else if (c === newLine) {
rows[currentRow].push(currentItem)
currentItem = ""
currentRow++
if (nextChar)
rows[currentRow] = []
if (isNextCharAQuote) {
inQuote = true
currentPosition++ // Jump 2
}
}
else if (isLastChar)
rows[currentRow].push(currentItem + c)
else
currentItem += c
}
currentPosition++
}
} else {
const lines = str.split(newLine)
const lineCount = lines.length
for (let i = 0; i < lineCount; i++) {
if (lines[i])
rows[i] = lines[i].split(delimiter)
}
}
const numberOfColumns = rows[0].length
hasHeaders = hasHeaders === false ? false : true
const headerRow = hasHeaders ? rows[0] : []
if (hasHeaders) {
// Strip any spaces from column names in the header row.
// This makes the mapping not quite 1 to 1 if there are any spaces in prop names.
for (let i = 0; i < numberOfColumns; i++) {
headerRow[i] = headerRow[i].replace(Space._assignmentRegEx, "")
}
} else {
// If str has no headers, create them as 0,1,2,3
for (let i = 0; i < numberOfColumns; i++) {
headerRow.push(i)
}
}
// Immediately ditch ref to str for GC
str = null
const result = new Space()
const resultProps = []
const resultValues = []
const headerIndex = {}
const type = {
properties: headerRow
}
const rowCount = rows.length
let rowIndex = 0
for (let i = (hasHeaders ? 1 : 0); i < rowCount; i++) {
const obj = new Space()
let row = rows[i]
// If the row contains too many columns, shift the extra columns onto the last one.
// This allows you to not have to escape delimiter characters in the final column.
if (row.length > numberOfColumns) {
row[numberOfColumns - 1] = row.slice(numberOfColumns - 1).join(delimiter)
row = row.slice(0, numberOfColumns)
} else if (row.length < numberOfColumns) {
// If the row is missing columns add empty columns until it is full.
// This allows you to make including delimiters for empty ending columns in each row optional.
while (row.length < numberOfColumns) {
row.push("")
}
}
obj.setWithType(type, row)
resultProps.push(rowIndex)
resultValues.push(obj)
rowIndex++
}
const collectionType = {
properties: resultProps,
index: resultProps // In this case index is identical to array
}
result.setWithType(collectionType, resultValues)
return result
}
Space.fromArrayWithHeader = rows => {
if (!rows.length)
return new Space()
const length = rows.length
const indexes = []
const childrenArray = []
const rowType = {
properties: rows[0]
}
for (let i = 1; i < length; i++) {
const obj = new Space()
obj.setWithType(rowType, rows[i])
childrenArray.push(obj)
indexes.push(i - 1)
}
const collectionType = {
properties: indexes,
index: indexes
}
return new Space().setWithType(collectionType, childrenArray)
}
Space.fromSsv = (str, hasHeaders) => {
return Space.fromDelimiter(str, " ", hasHeaders)
}
Space.fromTsv = (str, hasHeaders) => {
return Space.fromDelimiter(str, "\t", hasHeaders)
}
Space.makeIndex = (properties, index, startAt) => {
const length = properties.length
index = index || {}
startAt = startAt || 0
for (let i = startAt || 0; i < length; i++) {
index[properties[i]] = i
}
return index
}
Space._parseXml2 = str => {
const el = document.createElement("div")
el.innerHTML = str
return el
}
Space._initializeXmlParser = () => {
if (Space._parseXml)
return
const windowObj = window
if (typeof windowObj.DOMParser !== "undefined")
Space._parseXml = xmlStr => (new windowObj.DOMParser()).parseFromString(xmlStr, "text/xml")
else if (typeof windowObj.ActiveXObject !== "undefined" && new windowObj.ActiveXObject("Microsoft.XMLDOM")) {
Space._parseXml = xmlStr => {
const xmlDoc = new windowObj.ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async = "false"
xmlDoc.loadXML(xmlStr)
return xmlDoc
}
}
else
throw new Error("No XML parser found")
}
Space.fromXml = str => {
Space._initializeXmlParser()
const xml = Space._parseXml(str)
try {
return Space._fromXml(xml).get("children")
}
catch (e) {
return Space._fromXml(Space._parseXml2(str)).get("children")
}
}
Space._fromXml = xml => {
const result = new Space()
const children = new Space()
// Set attributes
if (xml.attributes) {
for (let a = 0; a < xml.attributes.length; a++) {
result.set(xml.attributes[a].name, xml.attributes[a].value)
}
}
if (xml.data)
children.push(xml.data)
// Set content
if (xml.childNodes && xml.childNodes.length > 0) {
for (let i = 0; i < xml.childNodes.length; i++) {
const child = xml.childNodes[i]
if (child.tagName && child.tagName.match(/parsererror/i))
throw new Error("Parse Error")
if (child.childNodes.length > 0 && child.tagName)
children.append(child.tagName, Space._fromXml(child))
else if (child.tagName)
children.append(child.tagName, new Space())
else if (child.data) {
const data = child.data.trim()
if (data)
children.push(data)
}
}
}
if (children.length > 0)
result.set("children", children)
return result
}
Space._pairToString = (property, value, spaces) => {
const ac = Space._assignmentChar
const lc = Space._lineChar
// Set up the property part of the property/value pair
const string = Space._strRepeat(ac, spaces) + property
// If the value is a space, concatenate it
if (value instanceof Space)
return string + lc + value._toString(spaces + 1)
value = value.toString()
// multiline string
if (value.indexOf(lc) > -1)
return string + ac + value.replace(Space._lineRegex, lc + Space._strRepeat(ac, spaces + 1)) + lc
// Plain string
return string + ac + value + lc
}
Space._removeItems = (array, indexes) => {
const removedValues = []
if (typeof indexes === "number")
indexes = [indexes]
for (let i = indexes.length - 1; i >= 0 ; i--)
removedValues.push(array.splice(indexes[i], 1))
return removedValues
}
Space._strRepeat = (string, count) => {
let str = ""
for (let i = 0; i < count; i++) {
str += string
}
return str
}
Space.union = function () {
const argumentsLength = arguments.length
let union = Space._unionSingle(arguments[0], arguments[1])
for (let i = 0; i < argumentsLength; i++) {
if (i === 1) continue // skip the first one
union = Space._unionSingle(union, arguments[i])
if (!union.length)
break
}
return union
}
Space._unionSingle = (spaceA, spaceB) => {
const union = new Space()
if (!(spaceB instanceof Space))
return union
spaceA.each((property, value) => {
const spaceBValue = spaceB._getValueByProperty(property)
if (value instanceof Space && spaceBValue && spaceBValue instanceof Space)
union._setPair(property, Space._unionSingle(value, spaceB._getValueByProperty(property)))
if (value === spaceBValue)
union._setPair(property, value)
})
return union
}
Space.prototype.append = function(property, value) {
this._setPair(property, value)
return this
}
Space.prototype.at = function(index) {
return this._getValueAt(index)
}
Space.prototype._clear = function() {
delete this._properties
delete this._values
delete this._index
delete this._type
return this
}
Space.prototype.clear = function(space) {
if (this.isEmpty())
return this
this._clear()
if (space)
this._load(space)
return this
}
Space.prototype.clone = function() {
return new Space(this.toString())
}
Space.prototype.concat = function(b) {
if (typeof b === "string")
b = new Space(b)
const a = this
b.each((property, value) => {
a.append(property, value)
})
return this
}
Space.prototype.deleteDuplicates = function(recursive) {
const matches = {} // StringMap<int>
const me = this
this.each((property, value, index) => {
const isDupe = matches[property] !== undefined
if (isDupe) {
me._deleteByIndex(index)
return true
}
matches[property] = true
if (recursive && value instanceof Space)
value.deleteDuplicates(recursive)
}, null, true)
return this
}
Space.prototype._delete = function(property) {
if (Space._isSpacePath(property))
return this._deleteBySpacePath(property)
else
return this._deleteByProperty(property)
}
Space.prototype._deleteByIndex = function(index) {
const values = this._getValues()
if (values[index] === undefined)
return 0
this._deleteProperty(index)
values.splice(index, 1)
delete this._index
return 1
}
Space.prototype._deleteByIndexes = function (indexesToDelete) {
const length = indexesToDelete.length
const values = this._getValues()
for (let i = length - 1; i >= 0 ; i--) {
this._deleteProperty(indexesToDelete[i])
values.splice(indexesToDelete[i], 1)
}
delete this._index
return this
}
Space.prototype._deleteByProperty = function(property) {
const index = this.indexOf(property)
return index === -1 ? 0 : this._deleteByIndex(index)
}
Space.prototype._deleteBySpacePath = function(spacePath) {
// Get parent
const parts = spacePath.split(Space._assignmentChar)
const child = parts.pop()
const parent = this.get(parts.join(Space._assignmentChar))
return parent instanceof Space ? parent._delete(child) : 0
}
Space.prototype._deleteProperty = function(index) {
this._dropType()
return this._getProperties().splice(index, 1)
}
Space.prototype._dropType = function() {
if (!this._type)
return;
this._properties = this._type.properties.slice()
delete this._type
}
Space.prototype._getProperties = function() {
if (this._type)
return this._type.properties
if (!this._properties)
this._properties = []
return this._properties
}
Space.prototype._insertProperty = function (index, property) {
this._dropType()
this._getProperties().splice(index, 0, property)
}
Space.prototype._setProperty = function(index, property) {
this._dropType()
this._getProperties()[index] = property
return this._getProperties()
}
Space.prototype._reverseProperties = function() {
this._dropType()
this._getProperties().reverse()
}
Space.prototype.decrement = function(path, amount) {
return this.increment(path, amount || -1)
}
Space.prototype.deepLength = function() {
let length = 0
this.every(() => {
length++
})
return length
}
Space.prototype["delete"] = function(property) {
while (this._delete(property)) {
}
return this
}
Space.prototype.deleteAt = function(index) {
let somethingChanged = false
if (typeof index === "number")
somethingChanged = this._deleteByIndex(index)
else if (index && index.length)
somethingChanged = this._deleteByIndexes(index)
return this
}
Space.prototype.diff = function(space) {
const diff = new Space()
if (!(space instanceof Space))
space = new Space(space)
const me = this
this.each((property, value) => {
const spaceValue = space._getValueByProperty(property)
// Case: Deleted
if (spaceValue === undefined) {
diff._setPair(property, "")
return true
}
const thisValue = me._getValueByProperty(property)
const typeofSpaceValue = typeof(spaceValue)
const typeofThisValue = typeof(thisValue)
// Different Properties
if (typeofThisValue !== typeofSpaceValue) {
if (typeofSpaceValue === "object")
diff._setPair(property, new Space(spaceValue))
// We treat a spaceValue of 1 equal to "1"
else if (thisValue == spaceValue)
return true
else
diff._setPair(property, spaceValue)
return true
}
// Strings, floats, etc
if (typeofThisValue !== "object") {
if (thisValue !== spaceValue)
diff._setPair(property, spaceValue)
return true
}
// Both are Objects
const sub_diff = thisValue.diff(spaceValue)
if (sub_diff.length)
diff._setPair(property, sub_diff)
})
// Leftovers are Additions
space.each((property, value) => {
if (me.has(property))
return true
if (typeof value !== "object") {
diff._setPair(property, value)
return true
} else if (value instanceof Space)
diff._setPair(property, new Space(value))
else
diff._setPair(property, new Space(space))
})
return diff
}
Space.prototype.each = function(fn, deep, reverse) {
const length = this.length
const properties = this._getProperties()
const values = this._getValues()
if (!reverse) {
for (let i = 0; i < length; i++) {
if (deep && values[i] instanceof Space)
values[i].each(fn, deep)
if (fn.call(this, properties[i], values[i], i) === false)
return this
}
} else {
for (let i = length - 1; i >= 0; i--) {
if (deep && values[i] instanceof Space)
values[i].each(fn, deep)
if (fn.call(this, properties[i], values[i], i) === false)
return this
}
}
return this
}
Space.prototype.every = function(fn) {
this._every(fn)
return this
}
Space.prototype._every = function(fn, leafsOnly) {
let result = true
this.each((property, value, index) => {
const isSpace = value instanceof Space
if (!isSpace || !leafsOnly)
result = fn.call(this, property, value, index)
if (result === false)
return false
if (isSpace)
result = value._every(fn, leafsOnly)
return result
})
return result
}
Space.prototype.extract = function (properties) {
const props = properties.split(Space._assignmentChar)
const propKey = {}
const matches = new Space()
props.forEach(val => propKey[val] = true)
this._extract(propKey, matches)
return matches
}
Space.prototype._extract = function (propKey, matches) {
this.each((property, value) => {
if (propKey[property])
matches.append(property, value)
else if (value instanceof Space)
value._extract(propKey, matches)
})
}
Space.prototype.everyLeaf = function(fn) {
this._every(fn, true)
return this
}
Space.prototype.filter = function(fn, inPlace) {
if (inPlace)
return this._filterInPlace(fn)
const result = new Space()
const length = this.length
const properties = this._getProperties()
const values = this._getValues()
for (let i = 0; i < length; i++) {
if (fn.call(this, properties[i], values[i], i) === true)
result.append(properties[i], values[i])
}
return result
}
Space.prototype._filterInPlace = function(fn) {
this._dropType()
const properties = this._getProperties()
const values = this._getValues()
for (let i = this.length - 1; i >= 0 ; i--) {
if (fn.call(this, properties[i], values[i], i) !== true) {
properties.splice(i, 1)
values.splice(i, 1)
}
}
delete this._index
return this
}
Space.prototype.find = function(property, value) {
// for now assume string test
// search this one
const matches = new Space()
if (this.get(property) === value)
matches.push(this)
this.each((prop, val) => {
if (!(val instanceof Space))
return true
val
.find(property, value)
.each(function(k, v) {
matches.push(v)
})
})
return matches
}
Space.prototype.flattenTypes = function () {
const unionType = this.getUnionType()
this.each((k, v) => {
if (v instanceof Space)
v.setType(unionType)
})
return this
}
Space.prototype.format = function(str) {
const that = this
return str.replace(/{([^\}]+)}/g, (match, path) => {
const value = that.get(path)
return value !== undefined ? value : ""
})
}
Space.prototype.get = function(spacePath) {
if (spacePath === undefined || spacePath === null)
return undefined
return this._getValueByString(spacePath.toString())
}
Space.prototype.getAll = function(query) {
const matches = new Space()
this.each((property, value) => {
if (property !== query)
return true
matches.append(property, value)
})
return matches
}
Space.prototype.getArray = function(query, recursive) {
const matches = []
this._getArray(query, recursive === undefined ? true : recursive, matches)
return matches
}
Space.prototype._getArray = function(query, recursive, matches) {
this.each((property, value) => {
if (property === query)
matches.push(value)
if (recursive && value instanceof Space)
value._getArray(query, true, matches)
})
}
Space.prototype.getColumn = function(path) {
const arr = []
this.each((k, v) => {
if (v instanceof Space)
arr.push(v.get(path))
})
return arr
}
Space.prototype.getIndex = function() {
const parent = this.getParent()
const that = this
let index
if (!parent)
return -1
parent.each((k, v, i) => {
if (v === that) {
index = i
return false
}
})
return index
}
Space.prototype.getParent = function() {
return this._parent || null
}
Space.prototype.getPath = function() {
let parent = this._parent
let child = this
let path = ""
let first = ""
while (parent) {
parent.each((k, v) => {
if (v === child) {
path = k + first + path
first = Space._assignmentChar
return false
}
})
child = parent
parent = parent._parent
}
return path
}
Space.prototype.getRoot = function() {
let parent = this._parent
if (!parent)
return this
while (parent._parent) {
parent = parent._parent
}
return parent || this
}
Space.prototype.getBySpace = function(query) {
return this._getValueBySpace(query)
}
Space.prototype._getCachedValue = function(property) {
return this._getValues()[this._getIndex()[property]]
}
Space.prototype._getValueAt = function(index) {
// Passing -1 gets the last item, et cetera
if (index < 0)
index = this.length + index
return this._getValues()[index]
}
Space.prototype._getValueByProperty = function(property) {
return this._getCachedValue(property)
}
Space.prototype._getPropertyByIndex = function(index) {
// Passing -1 gets the last item, et cetera
const length = this.length
if (index < 0)
index = length + index
if (index >= length)
return undefined
return this._getProperties()[index]
}
Space.prototype.getProperties = function() {
return this._getProperties().slice(0)
}
Space.prototype.getType = function () {
if (this._type)
return this._type
this._type = {properties: this._properties || []}
this._type.index = Space.makeIndex(this._properties)
delete this._properties
delete this._index
return this._type
}
Space.prototype.getTypeIndex = function() {
const index = {}
this.each((k, v, i) => {
if (!(v instanceof Space))
return true
const type = v.getType()
if (type.key === undefined)
type.key = type.properties.join(Space._assignmentChar)
const typeInIndex = index[type.key]
// If it's not in the index yet add it
if (!typeInIndex)
index[type.key] = type
// If it's a dupe remove the reference to the second occurrence
else if (typeInIndex && (typeInIndex !== type))
v._type = typeInIndex
})
return index
}
Space.prototype.getUnionType = function () {
if (!this.length)
return {properties: []}
const typeIndex = this.getTypeIndex()
const keys = Object.keys(typeIndex)
// If there's only one type return that
if (keys.length === 1)
return typeIndex[keys[0]]
const index = {}
const props = []
const type = {properties: props, index: index}
// Remove dupes
const properties = keys.join(Space._assignmentChar).split(Space._assignmentChar)
const length = properties.length
for (let i = 0; i < length; i++) {
if (index[properties[i]] === undefined) {
props.push(properties[i])
index[properties[i]] = props.length
}
}
return type
}
Space.prototype._getValueByString = function(spacePath) {
const value = this._getValueByProperty(spacePath)
if (value)
return value
if (value === "" || value === 0 || value === false)
return value
if (!Space._isSpacePath(spacePath))
return undefined
const parts = spacePath.split(Space._assignmentChar)
const current = parts.shift()
// Not set
if (!this.has(current))
return undefined
const currentValue = this._getValueByProperty(current)
if (currentValue instanceof Space)
return this._getValueByProperty(current).get(parts.join(Space._assignmentChar))
else
return undefined
}
Space.prototype._getValueBySpace = function(space) {
const result = new Space()
const me = this
space.each((property, v) => {
const value = me._getValueByProperty(property)
// If this doesnt have that property, continue
if (typeof value === "undefined")
return true
// If the request is a leaf or empty space, set
if (!(space._getValueByProperty(property) instanceof Space) || !space._getValueByProperty(property).length) {
result._setPair(property, value)
return true
}
// Else the request is a space with types, make sure the subject is a space
if (!(value instanceof Space))
return true
// Now time to recurse
result._setPair(property, value._getValueBySpace(space._getValueByProperty(property)))
})
return result
}
Space.prototype._getIndex = function() {
// StringMap<int> {property: index}
// When there are multiple values with the same property, _index stores the last value.
return (this._type && this._type.index) || this._index || this._makeIndex()
}
Space.prototype._getValues = function() {