-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorvObjLib.js
1550 lines (1137 loc) · 52.9 KB
/
orvObjLib.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
/*************************************************************************************
** LIBRARY FOR WORKING WITH JAVASCRIPT OBJECTS AND ARRAYS **
Library Info Section of Comments:
=================================
Project Location on Github: https://github.com/OrvilleChomer/orvObjLib
Project Info on my Blog: TBD
Shameless Self Promotion Section of Comments!:
==============================================
Library Author: Orville Chomer
Bio Page: http://chomer.com/bio/
Main Website: http://chomer.com/
YouTube Channel: https://www.youtube.com/c/OrvilleChomer
Twitter Handle: @orville
Linked In Profile Page: https://www.linkedin.com/in/OrvilleChomer/
Copy of Resume/CV (PDF): http://chomer.com/wp-content/2017/12/OrvilleChomerResume2017.pdf
Copy of Resume/CV (docx): http://chomer.com/wp-content/2017/12/OrvilleChomerResume2017.docx
-----------------------------------------------------------------------------
Change Log:
Change Date Version
============ =======
12/27/2017 Initial log entry. Filled in these comments better! .002
01/03/2018 Added support for "freeze", "seal", .003
"preventExtensions"
01/14/2018 Changed method name: "setCopyMethods()" to: .004
"setCopyMethodsFlagTo()"
to make its use more clear.
**************************************************************************
function below is a Constructor... use with "new" key word.
Example: var orvObjLib = new OrvObjLib();
Note that if you are doing any asynchronous operations with this library...
think Ajax calls or indexedDb calls.... you should probably declare your
object variable for this at the global level.
This library has a set of process Queues that it uses to keep track of pending
work for asynchronous operations.
---------------------------------------------
Version used for Production code should be minified (of course).
*************************************************************************************/
function OrvObjLib() {
"use strict"
// instance variables:
var nCurrentDebugLevel = 0; // 0 = no debug level
var alteredObjsByIndex = [];
var alteredObjsByKey = [];
var objKeyLookup = new WeakMap; // 12/31/2017
var nodesByKey = []; // used when deserializing object
var self = this;
var nNextKeyNum = 0;
var serializedObj;
var bCopyMethods = false;
var bProduceMinifiedResults = false;
var sPiDbName = "defaultDb";
var taskQueueByIndex = [];
var taskQueueById = [];
var completedTasksByIndex = [];
var completedTasksById = [];
/*
// ??? running total instance variables:
???
/* var nTotalStringProps = 0;
var nTotalNumProps = 0;
var nTotalDateProps = 0;
var nTotalBooleanProps = 0;
var nTotalObjProps = 0;
var nTotalArrProps = 0;
var nTotalFrozenObjs = 0;
var nTotalFrozenArrs = 0;
*/
/*******************************************************************************
Used to clone JavaScript objects or arrays.
First, it tries to use native functionality...
If there aren't any circular references, it will work, and it will work faster!
If it does Not work... it uses this library to do the work!
*******************************************************************************/
self.cloneObj = function(inputObj) {
var outputObj;
var serObj,deserObj;
try {
// try vanilla Js before using this library's serialize/deserialize code
outputObj = JSON.parse(JSON.stringify(inputObj));
} catch(err) {
serObj = self.serializeObj(inputObj);
deserObj = self.deserializeObj(serObj);
outputObj = deserObj.output;
} // end of try/catch block
return outputObj;
} // end of method cloneObj()
/********************************************************
Takes instructions in serialized object, and from them
builds a new copy of the original object
********************************************************/
self.deserializeObj = function(inputSerializedObj) {
var nMax,n;
var resultObj = baseDeserializedObj();
var commands,cmd;
var bRootObjSet = false;
var obj,arr;
resultObj.deserializationDate = new Date();
nodesByKey = []; // clear out anything there Might have been from before
commands = inputSerializedObj.creationInstructions;
// process through the commands to re-constitute original object/array:
nMax = commands.length;
for (n=0;n<nMax;n++) {
cmd = commands[n];
switch(cmd.cmd) {
case "create-obj":
obj = aa_deser_createObj(cmd, resultObj.procLog);
if (!bRootObjSet) {
resultObj.output = obj;
bRootObjSet = true;
} // end if
break;
case "create-array":
arr = aa_deser_createArray(cmd, resultObj.procLog);
if (!bRootObjSet) {
resultObj.output = arr;
bRootObjSet = true;
} // end if
break;
case "create-array-property":
aa_deser_createArrayProperty(cmd, resultObj.procLog);
break;
case "add-method":
aa_deser_addMethod(cmd, resultObj.procLog);
break;
case "add-object-to-array":
aa_deser_addObjectToArray(cmd, resultObj.procLog);
break;
case "add-array-to-array":
aa_deser_addArrayToArray(cmd, resultObj.procLog);
break;
case "add-basic-value-to-array":
aa_deser_addBasicValueToArray(cmd, resultObj.procLog);
break;
case "add-date-value-to-array":
aa_deser_addDateValueToArray(cmd, resultObj.procLog);
break;
case "create-object-property":
aa_deser_createObjProperty(cmd, resultObj.procLog);
break;
case "create-basic-property":
aa_deser_createBasicProperty(cmd, resultObj.procLog);
break;
case "create-date-property":
aa_deser_createDateProperty(cmd, resultObj.procLog);
break;
case "freeze-obj":
// used for both objects and arrays.
aa_deser_freezeObj(cmd, resultObj.procLog);
break;
case "prevent_ext":
// used for both objects and arrays.
aa_deser_preventExt(cmd, resultObj.procLog);
break;
case "seal-obj":
// used for both objects and arrays.
aa_deser_sealObj(cmd, resultObj.procLog);
break;
default:
break;
} // end of switch(cmd.cmd)
} // next n
nodesByKey = []; // since we are done, clear out to free up memory
resultObj.completionTimestamp = new Date();
// how long did it take?
resultObj.totalMs = getMsDif(resultObj.deserializationDate, resultObj.completionTimestamp);
return resultObj;
} // end of method deserializeObj()
/*******************************************************************************
Must be used on a JSON string that was created from a serialized
object.
*******************************************************************************/
self.deserializeJsonString = function(sJson) {
var objWork = JSON.parse(sJson);
return self.deserializeObj(objWork);
} // end of method deserializeJsonString()
/*******************************************************************************
This method is used to load an object out of a local indexedDb
database based on the database its in... and the object's primary key.
use: orvObjLib.loadObjFromDb(params).onsuccess = function(obj, [info]) {
}
*******************************************************************************/
self.loadObjFromDb = function(params) {
var obj = params.getObj;
var sKey = params.withKey;
} // end of method loadObjFromDb()
/*******************************************************************************
Merges data from one complex object into the data of another complex
object.
Primary Key Defs param is an Array.
Sample primary key defs:
{"objPropName": "customer", "pkPropName": "custId"}
{"objPropName": "dealSections", "pkPropName": "dealSectionId"}
*******************************************************************************/
self.merge = function(params) {
var dataFromObj = params.dataFromObj;
var objBeingModified = params.intoThisObj;
var primaryKeyDefs = params.usingPrimaryKeyDefs;
} // end of method merge()
/*******************************************************************************
Takes JSON created from object that was serialized,
and returns a copy of the original object (or array)
*******************************************************************************/
self.parse = function(sJson) {
var objWork = self.deserializeJsonString(sJson);
return objWork.output;
} // end of method loadObjFromDb()
/*******************************************************************************
Saves Js object to indexed Db database with what is basically a
Primary Key!
*******************************************************************************/
self.saveObjToDb = function(params) {
var inputObj = params.inputObj;
var sKey = params.key;
var sDbName = params.dbName; // sPiDbName - fix code later
var objToSave = self.deserializeObj(inputObj);
} // end of method saveObjToDb()
/*******************************************************************************
Takes a JavaScript object or array and creates a "serialized object."
This object will contain a list of commands which can be used by
the deserializeObj() method to rebuild a copy of the original object or
array.
*******************************************************************************/
self.serializeObj = function(inputObj) {
var sRootKey;
var sDataType = getPropValueType(inputObj);
var bRootNodeAdded = false;
var nDepth = -1;
var sContainer = "~root";
var sContainerDataType = "n/a";
// at least for now: -- cannot serialize a DOM object
if (sDataType === "domItem") {
initSerializedObj();
addError("Cannot serialize a DOM object", "usage");
serializedObj.status = "aborted";
return serializedObj;
} // end if
cleanupWork(inputObj);
initSerializedObj();
sRootKey = setObjKey(inputObj, sDataType); // set object/array key on top-level object/array
// add command to create root node:
switch(sDataType) {
case "object":
makeCreateObjectCommand(sRootKey);
bRootNodeAdded = true;
break;
case "array":
makeCreateArrayCommand(sRootKey);
bRootNodeAdded = true;
break;
default:
break;
} // end of switch()
// top level was not an object or array so we are aborting this operation
if (!bRootNodeAdded) {
addError("Input parameter is neither a JS Object or a Js Array", "usage");
serializedObj.status = "aborted";
return serializedObj;
} // end if
if (sDataType === "object") {
processInputObj(inputObj, nDepth, sContainer, sContainerDataType); // recursively enumerate through object generating commands
} else {
processArray(inputObj, nDepth, sContainer, sContainerDataType);
} // end if/else
serializedObj.completionTimestamp = new Date();
clearObjMarkers(inputObj);
cleanupWork(inputObj);
// how long did it take?
serializedObj.totalMs = getMsDif(serializedObj.serializationDate, serializedObj.completionTimestamp);
return serializedObj;
} // end of method serializeObj()
/*******************************************************************************
Use this if you do not want to specify the db name every time you call
saveObjToDb() method
or you do not want to use the default db name
*******************************************************************************/
self.setDbName = function(siDbName) {
sPiDbName = siDbName;
} // end of method setDbName()
/*******************************************************************************
Set boolean flag to either true or false for copying (serializing)
any methods that may be part of the input object.
... the default value is (false).
*******************************************************************************/
self.setCopyMethodsFlagTo = function(bSetting) {
bCopyMethods = bSetting;
} // end of method self.setCopyMethods()
/*******************************************************************************
By default the sorting is case insensitive!
*******************************************************************************/
self.sortArrayOfObjects = function(params) {
var arr = self.cloneObj(params.thisArray);
var sortFields1 = params.sortFields;
var nMax = arr.length;
var nMax2 = sortFields1.length,nMax3;
var n,n2,obj,fld,sPropName,sOrder,nPos,sWork;
var sortFields2 = [];
var wrk,sDefault,prop,elementSortInfo,elementFieldInfo;
var bCaseInsensitive = true;
var aNumberFieldLst = [],nDecPlaces;
// build our little sort order objects...
for (n=0;n<nMax2;n++) {
sWork = sortFields1[n];
nPos = sWork.indexOf(" ");
sOrder="asc";
sDefault = "";
if (nPos>0 && nPos < sWork.length-1) {
wrk = sWork.split(" ");
sPropName = wrk[0];
sOrder = wrk[1];
} else {
sPropName = sWork;
} // end if
fld = {};
fld.propName = sPropName;
fld.sortOrder = sOrder;
fld.maxFieldLength = 0;
fld.maxNumericValue = 0; // only used if data format becomes numeric!
fld.maxDecPos = 0; // only used if data format becomes numeric!
fld.dataFormat = "string";
fld.defaultValue = sDefault; // value if property is missing
sortFields2[sortFields2.length] = fld;
} // next n
nMax2= sortFields2.length;
// okay, loop through our array & collect our property sort data
for (n=0;n<nMax;n++) {
obj = arr[n];
elementSortInfo = {};
elementSortInfo.arrayIndex = n;
elementSortInfo.fieldInfoByIndex = [];
obj["__orvSortInfo__"] = elementSortInfo;
// loop through list of fields to sort:
for (n2=0;n2<nMax2;n2++) {
fld = sortFields2[n2];
elementFieldInfo = {};
elementFieldInfo.dataFormat = fld.dataFormat;
nMax3 = elementSortInfo.fieldInfoByIndex.length;
elementSortInfo.fieldInfoByIndex[nMax3] = elementFieldInfo;
prop = obj[fld.propName];
if (typeof prop !== "undefined") {
sWork = prop; // prop contains a value so... get it
} else {
sWork = fld.defaultValue;
} // end if/else
if (typeof prop === "number" && fld.dataFormat === "string") {
fld.dataFormat = "number";
aNumberFieldLst[aNumberFieldLst.length] = fld;
} // end if
if (bCaseInsensitive) {
elementFieldInfo.value = sWork.toLowercase();
} else {
elementFieldInfo.value = sWork;
} // end if/else
if (sWork.length > fld.maxFieldLength) {
fld.maxFieldLength = sWork.length;
} // end if
} // next n2
} // next n
// next phase do some post processing on number fields:
// ----------------------------------------------------
// first, figure out largest decimal position
nMax2 = aNumberFieldLst.length;
// re-loop through main array:
for (n=0;n<nMax;n++) {
obj = arr[n];
// only process sort fields for numbers
// find largest decimal place
for (n2=0;n2<nMax2;n2++) {
fld = aNumberFieldLst[n];
prop = obj[fld.propName]-0;
nDecPlaces = decimalPlacesOfNumber(prop);
if (nDecPlaces > fld.maxDecPos) {
fld.maxDecPos = nDecPlaces;
} // end if
} // next n2
} // next n
return arr;
} // end of method self.sortArrayOfObjects()
/*******************************************************************************
Serializes and stringifies your object or array all in one shot!
*******************************************************************************/
self.stringify = function(inputObj) {
return JSON.stringify(self.serializeObj(inputObj));
} // end of method stringifyObj()
// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
// PRIVATE FUNCTIONS - PRIVATE FUNCTIONS - PRIVATE FUNCTIONS
// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
/*******************************************************************************
*******************************************************************************/
function aa_deser_addArrayToArray(cmd, procLog) {
var arr = nodesByKey[cmd.ownerKey]; // key to parent array
var arr2 = nodesByKey[cmd.arrayKey];
arr[cmd.arrayIndex] = arr2; // index# to place in parent array
aa_deser_log(cmd, procLog);
} // end of function aa_deser_addArrayToArray()
/*******************************************************************************
*******************************************************************************/
function aa_deser_addBasicValueToArray(cmd, procLog) {
var arr = nodesByKey[cmd.ownerKey];
arr[cmd.arrayIndex] = cmd.value;
aa_deser_log(cmd, procLog);
} // end of function aa_deser_addBasicValueToArray()
/*******************************************************************************
*******************************************************************************/
function aa_deser_addDateValueToArray(cmd, procLog) {
var arr = nodesByKey[cmd.ownerKey];
arr[cmd.arrayIndex] = new Date(cmd.value);
aa_deser_log(cmd, procLog);
} // end of function aa_deser_addDateValueToArray()
/*******************************************************************************
*******************************************************************************/
function aa_deser_addMethod(cmd, procLog) {
var obj = nodesByKey[cmd.ownerKey]; // object to add method to
var sMethodName = cmd.methodName;
var sArgs = cmd.args;
var sCode = cmd.jsCode;
var fn;
if (sArgs.length === 0) {
fn = new Function(sCode);
} else {
fn = new Function(sArgs, sCode);
} // end if/else
obj[sMethodName] = fn;
aa_deser_log(cmd, procLog);
} // end of function aa_deser_addMethod()
/*******************************************************************************
*******************************************************************************/
function aa_deser_addObjectToArray(cmd, procLog) {
var arr = nodesByKey[cmd.ownerKey];
var obj = nodesByKey[cmd.objectKey];
arr[cmd.arrayIndex] = obj;
aa_deser_log(cmd, procLog);
} // end of function aa_deser_addObjectToArray()
/*******************************************************************************
*******************************************************************************/
function aa_deser_createArray(cmd, procLog) {
var arr = [];
nodesByKey[cmd.key] = arr; // add to lookup
aa_deser_log(cmd, procLog);
return arr;
} // end of function aa_deser_createArray()
/*******************************************************************************
*******************************************************************************/
function aa_deser_createArrayProperty(cmd, procLog) {
var obj = nodesByKey[cmd.ownerKey];
var arr = nodesByKey[cmd.arrayKey];
obj[cmd.name] = arr;
aa_deser_log(cmd, procLog);
} // end of function aa_deser_createArrayProperty()
/*******************************************************************************
*******************************************************************************/
function aa_deser_createBasicProperty(cmd, procLog) {
var obj = nodesByKey[cmd.ownerKey];
obj[cmd.name] = cmd.value;
aa_deser_log(cmd, procLog);
} // end of function aa_deser_createBasicProperty()
/*******************************************************************************
*******************************************************************************/
function aa_deser_createDateProperty(cmd, procLog) {
var obj = nodesByKey[cmd.ownerKey];
obj[cmd.name] = new Date(cmd.value);
aa_deser_log(cmd, procLog);
} // end of function aa_deser_createDateProperty()
/*******************************************************************************
*******************************************************************************/
function aa_deser_createObj(cmd, procLog) {
var obj = {};
nodesByKey[cmd.key] = obj; // add to lookup
aa_deser_log(cmd, procLog);
return obj;
} // end of function aa_deser_createObj()
/*******************************************************************************
*******************************************************************************/
function aa_deser_createObjProperty(cmd, procLog) {
var obj = nodesByKey[cmd.ownerKey];
var childObj = nodesByKey[cmd.objectKey];
obj[cmd.name] = childObj;
aa_deser_log(cmd, procLog);
} // end of function aa_deser_createObjProperty()
/*******************************************************************************
Method added: Jan 1, 2018
Freezes either a JS Object or JS Array.
Info about freeze() method:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze
*******************************************************************************/
function aa_deser_freezeObj(cmd, procLog) {
var objOrArr = nodesByKey[cmd.key];
Object.freeze(objOrArr);
aa_deser_log(cmd, procLog);
} // end of function aa_deser_freezeObj()
/*******************************************************************************
add entry to deserialization process log
*******************************************************************************/
function aa_deser_log(cmd, procLog) {
var logEntry = {};
logEntry.cmd = cmd;
logEntry.opTimestamp = new Date();
procLog[procLog.length] = logEntry;
} // end of function aa_deser_log()
/*******************************************************************************
Method added: Jan 1, 2018
Prevents Extensions to either a JS Object or JS Array.
Info about preventExtensions() method:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/preventExtensions
*******************************************************************************/
function aa_deser_preventExt(cmd, procLog) {
var objOrArr = nodesByKey[cmd.key];
Object.preventExtensions(objOrArr);
aa_deser_log(cmd, procLog);
} // end of function aa_deser_preventExt()
/*******************************************************************************
Method added: Jan 1, 2018
Seals either a JS Object or JS Array.
Info about seal() method:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/seal
*******************************************************************************/
function aa_deser_sealObj(cmd, procLog) {
var objOrArr = nodesByKey[cmd.key];
Object.seal(objOrArr);
aa_deser_log(cmd, procLog);
} // end of function aa_deser_sealObj()
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
/*******************************************************************************
*******************************************************************************/
function addCreationInstruction(cmd) {
var nMax = serializedObj.creationInstructions.length;
cmd.cmdIndexNum = nMax;
cmd.commandCreatedAt = new Date();
serializedObj.creationInstructions[nMax] = cmd;
} // end of function addCreationInstruction()
/*******************************************************************************
*******************************************************************************/
function addError(sMsg, sClassification) {
var errEntry = {};
var nMax = serializedObj.errorList.length;
errEntry.msg = sMsg;
errEntry.errTimestamp = new Date();
errEntry.errIndexNum = nMax;
serializedObj.errorList[nMax] = errEntry;
} // end of function addError()
/*******************************************************************************
Used to add a special temporary "marker" object to JavaScript objects/
arrays that are being deserialized.
Has unique key we make up as well as any other status we need to keep
track of regarding this object or array.
This function is called from the: setObjKey() function.
*******************************************************************************/
function addObjMarker(obj, sDataType, nArrayIndexNum) {
var objMarker = {};
objMarker.key = getNextKey();
objMarker.processed = false;
objMarker.justCreated = true;
objMarker.objDataType = sDataType;
objMarker.originalObj = obj;
// if object or array is inside another indexed array element
// this keeps track of the original array index that it was in!
if (typeof nArrayIndexNum === "number") {
objMarker.arrayIndexNum = nArrayIndexNum;
} // end if
// obj["__orvSerialRef___"] = objMarker;
objKeyLookup.set(obj, objMarker); // add to weak map
// still need?
alteredObjsByIndex[alteredObjsByIndex.length] = obj;
alteredObjsByKey[objMarker.key] = obj;
return objMarker;
} // end of function addObjMarker()
/*******************************************************************************
Makes our boilerplate deserialized object (which we will add other
stuff to later).
*******************************************************************************/
function baseDeserializedObj() {
var obj = {};
initMetaData(obj, "deserializedObj", "Deserialized Object");
obj.deserializedStartTime = new Date();
obj.procLog = [];
return obj;
} // end of function baseDeserializedObj()
/*******************************************************************************
Called after processing a JS Object or Array during the serializing process,
to see if that entity has any restrictions on it...
- is it Frozen?
- is it Sealed?
- has it been set up to prevent Extensions?
It checks, and if any of the above is the case, it generates commands
that it adds to the serialized object so that the deserializing process
can make the new copy have the same restricted state(s).
*******************************************************************************/
function checkObjRestrictions(obj, sObjKey, sDataType) {
var bFrozen = false;
var bSealed = false;
if (Object.isFrozen(obj) === true) {
makeFreezeObjCommand(sObjKey);
bFrozen = true;
} // end if
if (Object.isSealed(obj) === true && !bFrozen) {
makeSealObjCommand(sObjKey);
bSealed = true;
} // end if
// the odd-ball opposite check!!! (a Not operation)!
if (!Object.isExtensible(obj) && !bFrozen === true && !bSealed === true) {
makePreventExtCommand(sObjKey);
} // end if
} // end of function checkObjRestrictions()
/*******************************************************************************
Used for indexedDb operations and Ajax operations
... or any other asynchronous operations that I might add to
this library!!
current task types:
"createGroup" - make a group to put other tasks in
groups can contain groups and so on.
"openDb" - open indexedDb
"createTrans" - create indexedDb transaction object
"getObjStore" - get indexDb object store
"ajaxCall" - do an Ajax call
current task classifications:
"indexedDb"
"ajax"
"group"
supported callbacks:
.onbeforestart - runs Just Before task is started
.onsuccess - runs if task was a success - passes in a result object
.onerror - run if task did not succeed for some reason - passes an object in about the error
all callbacks also pass in the task object that they belong to!
*******************************************************************************/
function createTask(params) {
var task = {};
var sTaskType = params.taskType;
var sComments = params.comments;
var sTaskClassification = params.classification;
task.createdAt = new Date();
task.startedAt = "n/a";
task.completedAt = "n/a";
task.msWaitTime = "n/a"; // will hold how much time in milliseconds the task sat on the queue
task.msCompleteTime = "n/a"; // will hold how much time in milliseconds it took to complete
task.started = false;
task.completed = false;
task.classification = sTaskClassification;
task.taskType = sTaskType;
task.comments = sComments;
} // end of function createTask()
/**************************************************************
The function removes all markers added to input object(s).
**************************************************************/
function clearObjMarkers(inputObj) {
var nMax = alteredObjsByIndex.length;
var n,alteredObj;
for (n=0;n<nMax;n++) {
alteredObj = alteredObjsByIndex[n];
if (typeof alteredObj["__orvSerialRef___"] !== "undefined") {
delete alteredObj["__orvSerialRef___"];
} // end if
} // next n
} // end of function clearObjMarkers()
/**************************************************************
The function cleans up top level values.
This is done BEFORE and AFTER work is done.
clearObjMarkers() is only done AFTER work is done!
**************************************************************/
function cleanupWork(inputObj) {
alteredObjsByIndex = [];
alteredObjsByKey = [];
nNextKeyNum = 0;
} // end of function cleanupWork()
/*******************************************************************************
function's code inspired from:
https://stackoverflow.com/questions/27082377/get-number-of-decimal-places-with-javascript
*******************************************************************************/
function decimalPlacesOfNumber(inputValue) {
var nValue = inputValue - 0; // casts to number if not already
var nDecPlaces = 0;
if (Math.floor(nValue) !== nValue) {
nDecPlaces = value.toString().split(".")[1].length || 0;
} // end if
return nDecPlaces;
} // end of function decimalPlacesOfNumber()
/*******************************************************************************
returns a data type for the value passed in in a format that I like.
I mean... why not? <g>
*******************************************************************************/
function getPropValueType(propValue) {
var bTypeFound = false;
var sType = "null";
var sWork = Object.prototype.toString.call(propValue);
if (sWork === "[object Array]") {
sType = "array";
bTypeFound = true;
} // end if
if (sWork === "[object Date]" && !bTypeFound) {
sType = "date";
bTypeFound = true;
} // end if
if (sWork === "[object Object]" && !bTypeFound) {
sType = "object";
bTypeFound = true;
} // end if
if (sWork === "[object Function]" && !bTypeFound) {
sType = "method";
bTypeFound = true;
} // end if
if (sWork.length>11 && !bTypeFound) {
if (sWork.substr(0,12) === "[object HTML") {
sType = "domItem";
bTypeFound = true;
} // end if
} // end if
if (!bTypeFound) {
sType = typeof propValue; // would return "string" for a string, "number" for a number or "boolean" for a boolean!
} // end if
return sType;
} // end of function getPropValueType()
/*******************************************************************************
Returns unique key to be used for this instance of this serialization
operation.
Multiple keys can be generated per serialization operation.
This is used to:
- map object/array relationships
- and which operations have been done regarding those relationships
- when deserializing, these keys are used to create the same object
relationships in the new reconstituted object.
*******************************************************************************/
function getNextKey() {
nNextKeyNum = nNextKeyNum + 1;
return "k"+nNextKeyNum;
} // end of function getNextKey()
/*******************************************************************************
Returns how many milliseconds occurred between start date/time
and end date/time.
*******************************************************************************/
function getMsDif(dtStart, dtEnd) {
var nDif = dtEnd.getMilliseconds() - dtStart.getMilliseconds();