dt-toolbox 6.0.0 → 7.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Changelog.md +13 -0
- package/Migration.guide.md +51 -2
- package/README.md +60 -41
- package/README_v.6.x.x.md +1158 -0
- package/package.json +3 -3
- package/src/flatData/look.js +12 -5
- package/src/flatObject/index.js +9 -1
- package/src/flatObject/insert.js +16 -6
- package/src/mainLib.js +12 -3
package/Changelog.md
CHANGED
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
## Release History
|
|
2
2
|
|
|
3
|
+
### 7.0.0 (2023-07-15)
|
|
4
|
+
- [x] Method `look` has new arguments: functions `next` and `finish`;
|
|
5
|
+
- [x] To stop iteration on current dt-line `return next()`;
|
|
6
|
+
- [x] To stop iteration on all dt-lines `return finish()`;
|
|
7
|
+
- [x] Method `insert` was renamed to `insertSegment`;
|
|
8
|
+
- [x] Method `listSegments` was added to show list of all segments in dt-object;
|
|
9
|
+
- [x] Method `insertSegment` expect the incoming data as dt-object, but if is not - will assume it as a standard javascript object and will convert it to dt-object automatically;
|
|
10
|
+
- [] Method `insertSegment` can recognize a dt model object.
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
3
14
|
### 6.0.0 (2023-05-12)
|
|
4
15
|
- [x] Version 5 is full rethinking of the idea and rewrite from scratch;
|
|
5
16
|
- [x] Simplified API interface;
|
|
@@ -8,6 +19,8 @@
|
|
|
8
19
|
- [x] Predefined and custom filters for faster data scan;
|
|
9
20
|
- [x] Model and query functions to shape the results;
|
|
10
21
|
|
|
22
|
+
|
|
23
|
+
|
|
11
24
|
### 4.0.7 (2022-03-03)
|
|
12
25
|
- [x] Library '@peter.naydenov/walk' was included as a dependency;
|
|
13
26
|
- [x] Cleaning: Method 'help.generateObject' was removed. Not in use anymore;
|
package/Migration.guide.md
CHANGED
|
@@ -1,9 +1,58 @@
|
|
|
1
1
|
# Migration Guides
|
|
2
2
|
|
|
3
|
+
## From v.6.x.x - v.7.x.x
|
|
4
|
+
Look function can return `next` and `finish` functions. This functions are used to control the flow of the "**look**" function. If you `return next()`, the look function will continue to execute callback with next dt-line object. If you `return finish()`, the look function will end.
|
|
3
5
|
|
|
4
|
-
## From v.4.x.x - v.5.x.x
|
|
5
6
|
|
|
6
|
-
|
|
7
|
+
```js
|
|
8
|
+
// was:
|
|
9
|
+
dt.query ( (store) => {
|
|
10
|
+
store.look ( ({name, flatData, breadcrumbs}) => {
|
|
11
|
+
if ( flatData.includes('something') ) {
|
|
12
|
+
// do something
|
|
13
|
+
// return string 'next' to stop iteration on this dt-line
|
|
14
|
+
return 'next'
|
|
15
|
+
}
|
|
16
|
+
})
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
// become:
|
|
20
|
+
dt.query ( (store) => {
|
|
21
|
+
store.look ( ({name, flatData, breadcrumbs, next, finish}) => {
|
|
22
|
+
if ( flatData.includes('something') ) {
|
|
23
|
+
// do something
|
|
24
|
+
// return `next()` to stop iteration on current dt-line
|
|
25
|
+
return next ()
|
|
26
|
+
// return `finish()` to stop iteration on all dt-lines (it's a new option)
|
|
27
|
+
}
|
|
28
|
+
})
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Method `insert` was renamed to `insertSegment` to be clear that data is not mixed. Segments are separated peaces of data.
|
|
34
|
+
|
|
35
|
+
```js
|
|
36
|
+
// before
|
|
37
|
+
const dt = dtbox.init ( a );
|
|
38
|
+
dt.insert ( 'specificDataName' b ) // b is a dt-object
|
|
39
|
+
|
|
40
|
+
// after
|
|
41
|
+
const dt = dtbox.init ( a );
|
|
42
|
+
dt.insertSegment ( 'specificDataName' b ) // b is a dt-object
|
|
43
|
+
// it's possible to insert a standard js object but it's not recomended
|
|
44
|
+
// dt.insertSegment ( 'specialDataName', a ) // a is a standard js object
|
|
45
|
+
// also possible to insert a dt model object directly
|
|
46
|
+
// dt.insertSegment ( 'specialDataName', c ) // Where c = b.export(). A dt model object.
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Method `listSegments` was added to show list of all segments in dt-object.
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
## From v.4.x.x - v.6.x.x
|
|
54
|
+
|
|
55
|
+
The library "dt-toolbox" exist for a full 7 years and now version 6 is coming as full rewrite of the original idea. Difference are more then similarity and better aproach is to read a version 6 documentation first. Then you will find that:
|
|
7
56
|
|
|
8
57
|
- Data creation is very simular;
|
|
9
58
|
- Dt-Object becomes a storage and you can add more data to it;
|
package/README.md
CHANGED
|
@@ -1,20 +1,27 @@
|
|
|
1
|
-
# DT Toolbox v.
|
|
1
|
+
# DT Toolbox v.7.x.x
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+

|
|
4
|
+

|
|
4
5
|
|
|
6
|
+
- [Documentatation for old v.6.x.x is here](htts://github.com/PeterNaydenov/dt-toolbox/blob/master/README_v.6.x.x.md)
|
|
5
7
|
|
|
6
8
|
|
|
7
|
-
## About version 6.x.x
|
|
8
|
-
* Version 5 is full rethinking of the idea and rewrite from scratch;
|
|
9
|
-
* Simplified API interface;
|
|
10
|
-
* New internal data-model;
|
|
11
|
-
* Multiple data inserts;
|
|
12
|
-
* Predefined and custom filters for faster data scan;
|
|
13
|
-
* Model and query functions to shape results;
|
|
14
9
|
|
|
10
|
+
## Breaking changes
|
|
11
|
+
* In version 7 look functions have 2 new arguments: Function `finish` and `next`;
|
|
12
|
+
* Instead of returning a string 'next'(in version 6) to stop iteration on current dt-line, now you have to `return next()`;
|
|
13
|
+
* Function `finish` is a new option. Call `return finish()` to stop iteration on all dt-lines;
|
|
14
|
+
* Method `insert` was renamed to `insertSegment` to be clear that data is not mixed. Segments are separated peaces of data;
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
## Other changes
|
|
19
|
+
* Method `listSegments` was added to show list of all segments in dt-object;
|
|
20
|
+
* Method `insertSegment` expect the incoming data as dt-object, but if is not - will assume it as a standard javascript object and will convert it to dt-object automatically;
|
|
15
21
|
|
|
16
22
|
|
|
17
23
|
## Description
|
|
24
|
+
|
|
18
25
|
DT-Toolbox is created to simplify the work with deep nested javascript objects. The library was created as an immutable data-storage(dt-object), internally based on data-model called `DT-model`.
|
|
19
26
|
|
|
20
27
|
## What is DT-model?
|
|
@@ -37,6 +44,7 @@ This data-description is easy to read, saved, or transfered.
|
|
|
37
44
|
|
|
38
45
|
## Installation
|
|
39
46
|
Install for node.js projects by writing in your terminal:
|
|
47
|
+
|
|
40
48
|
```
|
|
41
49
|
npm install dt-toolbox
|
|
42
50
|
```
|
|
@@ -53,12 +61,12 @@ import dtbox from 'dt-toolbox'
|
|
|
53
61
|
Use Dt-toolbox methods(init and load) to create a `dt-object`.
|
|
54
62
|
|
|
55
63
|
DT-object:
|
|
56
|
-
- Provides multiple insertion of data
|
|
64
|
+
- Provides multiple insertion of data segments. Data from each insertion stays differentiated;
|
|
57
65
|
- Has prebuilded filters for fast search of data;
|
|
58
66
|
- Can create and register a customized filters for fast search of data;
|
|
59
67
|
- Can apply `query functions` to find, extract and reshape the data;
|
|
60
68
|
- Can apply `model function` to reshape the final result;
|
|
61
|
-
- Can executes 'query' and 'model' function over all available data in the storage (All
|
|
69
|
+
- Can executes 'query' and 'model' function over all available data in the storage (All data segments);
|
|
62
70
|
- Execution of query/model functions will not change anything inside the host dt-object;
|
|
63
71
|
|
|
64
72
|
The **dt-object** contains internally a `dt-storage` object, that is available during call of the 'query' or 'model' functions. Dt-storage have couple of methods for searching data as well can create a new **DT-model** structures and fill them with data.
|
|
@@ -70,7 +78,7 @@ Query functions are returning a **new instance of dt-object** with the result, c
|
|
|
70
78
|
'list' : 'Scan only dt-lines where flatData is an array'
|
|
71
79
|
, 'listObject' : 'Scan objects that are members of array'
|
|
72
80
|
, 'object' : 'Scan just dt-lines where flatData is an object'
|
|
73
|
-
, 'root' : 'Scan only root dt-lines of each data
|
|
81
|
+
, 'root' : 'Scan only root dt-lines of each data segment'
|
|
74
82
|
```
|
|
75
83
|
Filters are very simple functions to build. Here is one example of how we can create filter for finding object with specific key and value in it.
|
|
76
84
|
```js
|
|
@@ -116,13 +124,14 @@ Take a look on the library APIs and see the '**Examples**' section bellow.
|
|
|
116
124
|
### dt-object API Fast Reference
|
|
117
125
|
|
|
118
126
|
```js
|
|
119
|
-
|
|
127
|
+
insertSegment : 'Inserts a new data-segment in the dt-object. Insertion should be provided as a dt-object.'
|
|
120
128
|
, 'export' : 'Returns the DT-model from dt-object - part or full'
|
|
121
129
|
, copy : 'Creates a copy of original provided data'
|
|
122
130
|
, query : 'Executes a "query" function on the dt-object. Returns a new dt-object with the result'
|
|
123
131
|
, model : 'Executes a "model" function on the dt-object. Returns a data model'
|
|
124
132
|
, setupFilter : 'Evaluate data according "filter" function and create a shorter scan list that can be used by "dt-storage" during execution of query and model functions'
|
|
125
133
|
, index : 'Provides a copy of specified dt-line by breadcrumbs'
|
|
134
|
+
, listSegments : 'Returns a list of all segments in dt-object'
|
|
126
135
|
```
|
|
127
136
|
|
|
128
137
|
### dt-storage API Fast Reference
|
|
@@ -312,8 +321,8 @@ const walk = dtbox.getWalk ();
|
|
|
312
321
|
## DT-object API
|
|
313
322
|
|
|
314
323
|
|
|
315
|
-
### dt.
|
|
316
|
-
Extend the dt-object with a new data. Insertion should be provided as dt-object.
|
|
324
|
+
### dt.insertSegment ()
|
|
325
|
+
Extend the dt-object with a new data segment. Insertion should be provided as dt-object.
|
|
317
326
|
|
|
318
327
|
```js
|
|
319
328
|
const a = [ // it's a 'file' data-model
|
|
@@ -331,10 +340,10 @@ const a = [ // it's a 'file' data-model
|
|
|
331
340
|
];
|
|
332
341
|
const b = { shoes: [ 'Puma', 'UA' ]}
|
|
333
342
|
const dt = dtbox.init ( a, { model : 'file' }) // create a dt-object
|
|
334
|
-
dt.
|
|
335
|
-
'extra'
|
|
343
|
+
dt.insertSegment (
|
|
344
|
+
'extra' // object name
|
|
336
345
|
, dtbox.init(b) // the extra object
|
|
337
|
-
) // insert to 'dt' storage extra
|
|
346
|
+
) // insert to 'dt' storage data segment named 'extra' with data from 'b' object
|
|
338
347
|
|
|
339
348
|
/**
|
|
340
349
|
dt internal interpretation:
|
|
@@ -369,7 +378,7 @@ dt.insert (
|
|
|
369
378
|
, 'root/shoes/summer' // -> location
|
|
370
379
|
, [] // -> edges
|
|
371
380
|
]
|
|
372
|
-
// ---> Here are the elements that are coming from '
|
|
381
|
+
// ---> Here are the elements that are coming from 'insertSegment'
|
|
373
382
|
, [
|
|
374
383
|
'extra' // -> dt-line name. Object name of insert will become root element for data segment.
|
|
375
384
|
, {} // -> flatData
|
|
@@ -425,7 +434,7 @@ const
|
|
|
425
434
|
, c = { vitamins : [ 'a', 'b', 'c' ]}
|
|
426
435
|
, dt = dtbox.init ( b )
|
|
427
436
|
;
|
|
428
|
-
dt.
|
|
437
|
+
dt.insertSegment ( 'extra', c )
|
|
429
438
|
const deepCopy = dt.copy ()
|
|
430
439
|
// it equal to: const deepCopy = dt.copy ('root')
|
|
431
440
|
const onlyExtra = dt.copy ( 'extra' )
|
|
@@ -505,7 +514,14 @@ const [ name, flatData, breadcrumbs, edges ] = dt.index ( br )
|
|
|
505
514
|
// edges === []
|
|
506
515
|
```
|
|
507
516
|
|
|
517
|
+
### dt.listSegments ()
|
|
518
|
+
|
|
519
|
+
Returns a list of all data segment names. Result is always an array with at least one element - 'root'.
|
|
508
520
|
|
|
521
|
+
```js
|
|
522
|
+
dt.listSegments ()
|
|
523
|
+
// [ 'root', 'extra' ]
|
|
524
|
+
```
|
|
509
525
|
|
|
510
526
|
|
|
511
527
|
|
|
@@ -547,11 +563,13 @@ const result = dt.query ( store => {
|
|
|
547
563
|
, breadcrumbs // breadcrumbs for dt-line;
|
|
548
564
|
, links // List of tuples [[parent, child],...]. Parent and child are the dt-line names;
|
|
549
565
|
, empty // Will present only if object has no properties. Empty flatData for dt-line.
|
|
566
|
+
, next // Function that can be returned to move to next dt-line
|
|
567
|
+
, finish // Function that can be returned to stop execution of look function
|
|
550
568
|
}) => {
|
|
551
569
|
//... body of look function
|
|
552
570
|
// Move fast to next dt-line by returning a string 'next'
|
|
553
|
-
return
|
|
554
|
-
// unconditional return
|
|
571
|
+
return next ()
|
|
572
|
+
// unconditional `return next()` will executes the 'look' function once per dt-line
|
|
555
573
|
})
|
|
556
574
|
})
|
|
557
575
|
```
|
|
@@ -578,10 +596,10 @@ const
|
|
|
578
596
|
, res = dt.query ( store => {
|
|
579
597
|
store
|
|
580
598
|
.from ( 'root/personal/hobbies' )
|
|
581
|
-
.look ( ({name}) => {
|
|
599
|
+
.look ( ({name, next }) => {
|
|
582
600
|
console.log ( name )
|
|
583
601
|
// -> hobbies, music, sport
|
|
584
|
-
return
|
|
602
|
+
return next () // because we want to iterate once on each dt-line
|
|
585
603
|
})
|
|
586
604
|
});
|
|
587
605
|
```
|
|
@@ -593,7 +611,7 @@ List of predefined filters:
|
|
|
593
611
|
- 'list' : Scan only dt-lines where flatData is an array;
|
|
594
612
|
- 'listObject' : Scan objects that are members of array;
|
|
595
613
|
- 'object' : Scan just dt-lines where flatData is an object;
|
|
596
|
-
- 'root' : Scan only root dt-lines of each data
|
|
614
|
+
- 'root' : Scan only root dt-lines of each data segment;
|
|
597
615
|
|
|
598
616
|
If filter do not exist, look function will be executed on each dt-line.
|
|
599
617
|
|
|
@@ -616,10 +634,10 @@ const
|
|
|
616
634
|
, res = dt.query ( store => {
|
|
617
635
|
store
|
|
618
636
|
.use ( 'object' ) // predefined filter 'object'
|
|
619
|
-
.look ( ({name}) => {
|
|
637
|
+
.look ( ({name, next }) => {
|
|
620
638
|
console.log ( name )
|
|
621
639
|
// -> root, personal, hobbies
|
|
622
|
-
return
|
|
640
|
+
return next () // because we want to iterate once on each dt-line
|
|
623
641
|
})
|
|
624
642
|
});
|
|
625
643
|
```
|
|
@@ -650,10 +668,10 @@ const
|
|
|
650
668
|
, res = dt.query ( store => {
|
|
651
669
|
store
|
|
652
670
|
.get ( 'root/friends' )
|
|
653
|
-
.look ( ({flatData}) => {
|
|
671
|
+
.look ( ({ flatData, next }) => {
|
|
654
672
|
console.log ( flatData )
|
|
655
673
|
// -> [ 'Ivan', 'Dobroslav', 'Stefan' ]
|
|
656
|
-
return
|
|
674
|
+
return next () // because we want to iterate once on each dt-line
|
|
657
675
|
})
|
|
658
676
|
});
|
|
659
677
|
```
|
|
@@ -682,10 +700,10 @@ const
|
|
|
682
700
|
, res = dt.query ( store => {
|
|
683
701
|
store
|
|
684
702
|
.find ( 'music' )
|
|
685
|
-
.look ( ({flatData}) => {
|
|
703
|
+
.look ( ({flatData, next }) => {
|
|
686
704
|
console.log ( flatData )
|
|
687
705
|
// -> [ 'punk', 'ska', 'metal', 'guitar' ]
|
|
688
|
-
return
|
|
706
|
+
return next () // because we want to iterate once on each dt-line
|
|
689
707
|
})
|
|
690
708
|
});
|
|
691
709
|
```
|
|
@@ -714,12 +732,12 @@ const
|
|
|
714
732
|
, res = dt.query ( store => {
|
|
715
733
|
store
|
|
716
734
|
.like ( 'per' )
|
|
717
|
-
.look ( ({flatData, breadcrumbs}) => {
|
|
735
|
+
.look ( ({flatData, breadcrumbs, next }) => {
|
|
718
736
|
console.log ( flatData )
|
|
719
737
|
// -> { age: 49, eyes: 'blue' }
|
|
720
738
|
console.log ( breadcrumbs )
|
|
721
739
|
// -> 'root/personal'
|
|
722
|
-
return
|
|
740
|
+
return next () // because we want to iterate once on each dt-line
|
|
723
741
|
})
|
|
724
742
|
});
|
|
725
743
|
```
|
|
@@ -1009,13 +1027,13 @@ const result = dt.query ( store => {
|
|
|
1009
1027
|
store.set ( 'root', []) // setup a root array element
|
|
1010
1028
|
store
|
|
1011
1029
|
.use ( 'listObject' ) // use only objects that are members of array
|
|
1012
|
-
.look ( ({ name, flatData }) => {
|
|
1030
|
+
.look ( ({ name, flatData, next }) => {
|
|
1013
1031
|
if ( flatData.age < 40 ) {
|
|
1014
1032
|
store.set ( i, flatData )
|
|
1015
1033
|
connectBuffer.push ( `root/${i}` )
|
|
1016
1034
|
i++
|
|
1017
1035
|
}
|
|
1018
|
-
return
|
|
1036
|
+
return next ()
|
|
1019
1037
|
})
|
|
1020
1038
|
store.connect ( connectBuffer )
|
|
1021
1039
|
})
|
|
@@ -1044,9 +1062,9 @@ const result = dt.query ( store => {
|
|
|
1044
1062
|
store.set ( 'root', [])
|
|
1045
1063
|
store
|
|
1046
1064
|
.use ( 'listObject' ) // use only objects that are members of array
|
|
1047
|
-
.look ( ({ flatData }) => {
|
|
1065
|
+
.look ( ({ flatData, next }) => {
|
|
1048
1066
|
if ( flatData.age < 40 ) store.push ( 'root', flatData.name )
|
|
1049
|
-
return
|
|
1067
|
+
return next ()
|
|
1050
1068
|
})
|
|
1051
1069
|
})
|
|
1052
1070
|
.model ( () => ({as:'std'}) )
|
|
@@ -1075,10 +1093,10 @@ Return an object with two groups
|
|
|
1075
1093
|
store.connect ([ 'root/under40', 'root/over40' ])
|
|
1076
1094
|
store
|
|
1077
1095
|
.use ( 'listObject' ) // use only objects that are members of array
|
|
1078
|
-
.look ( ({ flatData }) => {
|
|
1096
|
+
.look ( ({ flatData, next }) => {
|
|
1079
1097
|
let location = ( flatData.age > 40 ) ? 'over40' : 'under40';
|
|
1080
1098
|
store.push ( location, flatData.name )
|
|
1081
|
-
return
|
|
1099
|
+
return next ()
|
|
1082
1100
|
})
|
|
1083
1101
|
})
|
|
1084
1102
|
.model ( () => ({ as : 'std'}))
|
|
@@ -1102,10 +1120,10 @@ console.log ( result )
|
|
|
1102
1120
|
|
|
1103
1121
|
```js
|
|
1104
1122
|
const result = dt.query ( store => {
|
|
1105
|
-
store.look ( ({ name, flatData, breadcrumbs }) => {
|
|
1123
|
+
store.look ( ({ name, flatData, breadcrumbs, next }) => {
|
|
1106
1124
|
store.set ( name, flatData )
|
|
1107
1125
|
if ( breadcrumbs.includes('/') ) store.connect ([breadcrumbs])
|
|
1108
|
-
return
|
|
1126
|
+
return next ()
|
|
1109
1127
|
})
|
|
1110
1128
|
})
|
|
1111
1129
|
.model ( () => ({as:'std'}))
|
|
@@ -1141,6 +1159,7 @@ console.log ( result )
|
|
|
1141
1159
|
## External Links
|
|
1142
1160
|
- [Migration guide](https://github.com/PeterNaydenov/dt-toolbox/blob/master/Migration.guide.md)
|
|
1143
1161
|
- [History of changes](https://github.com/PeterNaydenov/dt-toolbox/blob/master/Changelog.md)
|
|
1162
|
+
- [DT-Queries. Some useful query functions for DT-Toolbox](https://github.com/PeterNaydenov/dt-queries)
|
|
1144
1163
|
- [Documentation v.4.x.x](https://github.com/PeterNaydenov/dt-toolbox/blob/master/README_v.4.x.x.md)
|
|
1145
1164
|
- [Documentation v.2.x.x](https://github.com/PeterNaydenov/dt-toolbox/blob/master/README_v.2.x.x.md)
|
|
1146
1165
|
|