dt-toolbox 7.0.0 → 7.1.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 +14 -0
- package/Migration.guide.md +17 -0
- package/README.md +32 -13
- package/package.json +2 -2
- package/src/convertors/index.js +2 -2
- package/src/convertors/tuples.js +3 -3
- package/src/flatObject/extractList.js +47 -0
- package/src/flatObject/index.js +19 -12
- package/src/flatObject/model.js +1 -1
- package/src/main.js +5 -1
- package/src/mainLib.js +2 -2
package/Changelog.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
## Release History
|
|
2
2
|
|
|
3
|
+
|
|
4
|
+
### 7.1.0 (2023-08-28)
|
|
5
|
+
- [x] Method `extractList` was added;
|
|
6
|
+
- [x] Fix: Method `model` recognizes '**files**' as a model;
|
|
7
|
+
- [x] Fix: Tuple model if data is just an array is wrong;
|
|
8
|
+
- [x] Fix: Convertor to files shows 'root' in the beginning of the path;
|
|
9
|
+
- [x] Fix: Convertor to breadcrumbs shows 'root' in the beginning of the path;
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
3
13
|
### 7.0.0 (2023-07-15)
|
|
4
14
|
- [x] Method `look` has new arguments: functions `next` and `finish`;
|
|
5
15
|
- [x] To stop iteration on current dt-line `return next()`;
|
|
@@ -8,6 +18,10 @@
|
|
|
8
18
|
- [x] Method `listSegments` was added to show list of all segments in dt-object;
|
|
9
19
|
- [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
20
|
- [] Method `insertSegment` can recognize a dt model object.
|
|
21
|
+
- [] Bug: Method `model` doesn't recognize '**files**' as a model;
|
|
22
|
+
- [] Bug: Tuple model if data is just an array is wrong;
|
|
23
|
+
- [] Bug: Convertor to files shows 'root' in the beginning of the path;
|
|
24
|
+
- [] Bug: Convertor to breadcrumbs shows 'root' in the beginning of the path;
|
|
11
25
|
|
|
12
26
|
|
|
13
27
|
|
package/Migration.guide.md
CHANGED
|
@@ -48,7 +48,24 @@ Method `insert` was renamed to `insertSegment` to be clear that data is not mixe
|
|
|
48
48
|
|
|
49
49
|
Method `listSegments` was added to show list of all segments in dt-object.
|
|
50
50
|
|
|
51
|
+
After version 7.1.x method `extractList` was added. Method helps to extract from dt-object multiple segments and properties, defined in a list. Options are coming as a second argument. Use them to define a model of extracted data.
|
|
51
52
|
|
|
53
|
+
```js
|
|
54
|
+
const
|
|
55
|
+
first = { name: 'first', data: 'first data' }
|
|
56
|
+
, second = { name: 'second', data: 'second data' }
|
|
57
|
+
, third = { name: 'third', data: 'third data' }
|
|
58
|
+
;
|
|
59
|
+
const storage = dtbox.init ( first ); // first will become a root segment
|
|
60
|
+
storage.insertSegment ( 'second', second );
|
|
61
|
+
storage.insertSegment ( 'third', third );
|
|
62
|
+
|
|
63
|
+
const [ a, b c, firstData ] = storage.extractList ( ['first', 'second', 'third', 'data' ], { type: 'std' } ));
|
|
64
|
+
// a -> { name: 'first', data: 'first data' }
|
|
65
|
+
// b -> { name: 'second', data: 'second data' }
|
|
66
|
+
// c -> { name: 'third', data: 'third data' }
|
|
67
|
+
// firstData -> 'first data' // Field 'data' from main dt-line of 'root' segment.
|
|
68
|
+
```
|
|
52
69
|
|
|
53
70
|
## From v.4.x.x - v.6.x.x
|
|
54
71
|
|
package/README.md
CHANGED
|
@@ -3,23 +3,16 @@
|
|
|
3
3
|

|
|
4
4
|

|
|
5
5
|
|
|
6
|
-
- [Documentatation for
|
|
6
|
+
- [Documentatation for v.6.x.x is here](htts://github.com/PeterNaydenov/dt-toolbox/blob/master/README_v.6.x.x.md)
|
|
7
|
+
- [Migration guide from v.6.x.x to v.7.x.x](htts://github.com/PeterNaydenov/dt-toolbox/blob/master/MIGRATION.md)
|
|
7
8
|
|
|
8
9
|
|
|
10
|
+
## Last Updates
|
|
9
11
|
|
|
10
|
-
|
|
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;
|
|
12
|
+
After version 7.1.x `dt-object` api has a new method `extractList` that helps to extract a multiple segments or properties, defined as a list. Use '**options**'(the second argument) to define a model of extracted data if needed. Reed about `extractList` bellow.
|
|
15
13
|
|
|
16
14
|
|
|
17
15
|
|
|
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;
|
|
21
|
-
|
|
22
|
-
|
|
23
16
|
## Description
|
|
24
17
|
|
|
25
18
|
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`.
|
|
@@ -132,6 +125,7 @@ Take a look on the library APIs and see the '**Examples**' section bellow.
|
|
|
132
125
|
, 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'
|
|
133
126
|
, index : 'Provides a copy of specified dt-line by breadcrumbs'
|
|
134
127
|
, listSegments : 'Returns a list of all segments in dt-object'
|
|
128
|
+
, extractList : 'Extracts a list of segments and/or properties from dt-object'
|
|
135
129
|
```
|
|
136
130
|
|
|
137
131
|
### dt-storage API Fast Reference
|
|
@@ -463,9 +457,10 @@ function modelFn ( store, a,v,g ) { // optional arguments will come directly aft
|
|
|
463
457
|
as : 'std' // property 'as' will execute final conversion. Model name should be from supported list of the library.
|
|
464
458
|
}
|
|
465
459
|
}
|
|
460
|
+
// Supported model-names are:
|
|
461
|
+
// 'standard', 'std', 'midFlat', 'tuple', 'tuples', 'breadcrumb', 'breadcrumbs'
|
|
466
462
|
|
|
467
463
|
const result = dt.model ( modelFn, a,v,g ) // modelFn is the only required argument. All other arguments are optional.
|
|
468
|
-
|
|
469
464
|
```
|
|
470
465
|
|
|
471
466
|
### dt.setupFilter ()
|
|
@@ -523,6 +518,30 @@ dt.listSegments ()
|
|
|
523
518
|
// [ 'root', 'extra' ]
|
|
524
519
|
```
|
|
525
520
|
|
|
521
|
+
### dt.extractList ()
|
|
522
|
+
|
|
523
|
+
After version 7.1.x method `extractList` was added. Method can extract a list of segments and properties as a single instruction. Options are coming as a second argument. Use optioins(the second argument) to define a model of extracted data if needed. Modeling is applied only on objects.
|
|
524
|
+
|
|
525
|
+
If requested segment or property is not available, response will be '**null**'.
|
|
526
|
+
Segments have priority over properties. If there is a segment with the same name as a property, segment will be extracted.
|
|
527
|
+
|
|
528
|
+
```js
|
|
529
|
+
const
|
|
530
|
+
first = { name: 'first', data: 'first data' }
|
|
531
|
+
, second = { name: 'second', data: 'second data' }
|
|
532
|
+
, third = { name: 'third', data: 'third data' }
|
|
533
|
+
;
|
|
534
|
+
const storage = dtbox.init ( first ); // first will become a root segment
|
|
535
|
+
storage.insertSegment ( 'second', second );
|
|
536
|
+
storage.insertSegment ( 'third', third );
|
|
537
|
+
|
|
538
|
+
const [ a, b c, firstData, otherData ] = storage.extractList ( ['first', 'second', 'third', 'data', 'secondData' ], { type: 'std' } ));
|
|
539
|
+
// a -> { name: 'first', data: 'first data' }
|
|
540
|
+
// b -> { name: 'second', data: 'second data' }
|
|
541
|
+
// c -> { name: 'third', data: 'third data' }
|
|
542
|
+
// firstData -> 'first data' // Field 'data' from main dt-line of 'root' segment.
|
|
543
|
+
// otherData -> null // There is no 'secondData' in 'root' segment. No segment 'secondData' as well.
|
|
544
|
+
```
|
|
526
545
|
|
|
527
546
|
|
|
528
547
|
|
|
@@ -1157,8 +1176,8 @@ console.log ( result )
|
|
|
1157
1176
|
|
|
1158
1177
|
|
|
1159
1178
|
## External Links
|
|
1160
|
-
- [Migration guide](https://github.com/PeterNaydenov/dt-toolbox/blob/master/Migration.guide.md)
|
|
1161
1179
|
- [History of changes](https://github.com/PeterNaydenov/dt-toolbox/blob/master/Changelog.md)
|
|
1180
|
+
- [Migration guide](https://github.com/PeterNaydenov/dt-toolbox/blob/master/Migration.guide.md)
|
|
1162
1181
|
- [DT-Queries. Some useful query functions for DT-Toolbox](https://github.com/PeterNaydenov/dt-queries)
|
|
1163
1182
|
- [Documentation v.4.x.x](https://github.com/PeterNaydenov/dt-toolbox/blob/master/README_v.4.x.x.md)
|
|
1164
1183
|
- [Documentation v.2.x.x](https://github.com/PeterNaydenov/dt-toolbox/blob/master/README_v.2.x.x.md)
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dt-toolbox",
|
|
3
3
|
"description": "Data manipulation tool",
|
|
4
|
-
"version": "7.
|
|
4
|
+
"version": "7.1.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Peter Naydenov",
|
|
7
7
|
"keywords": [
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"c8": "^8.0.1",
|
|
35
|
-
"chai": "^4.3.
|
|
35
|
+
"chai": "^4.3.8",
|
|
36
36
|
"mocha": "^10.2.0"
|
|
37
37
|
},
|
|
38
38
|
"c8": {
|
package/src/convertors/index.js
CHANGED
|
@@ -65,7 +65,7 @@ function to ( type, dependencies, inData ) { // convert from flat to 'data-typ
|
|
|
65
65
|
return tuples.toType ( inData )
|
|
66
66
|
case 'files':
|
|
67
67
|
tups = tuples.toType ( inData );
|
|
68
|
-
return tups.map ( ([k,v]) => `${k}/${v}`)
|
|
68
|
+
return tups.map ( ([k,v]) => (( k === 'root' ) ? v : `${k}/${v}`) )
|
|
69
69
|
case 'breadcrumb' :
|
|
70
70
|
case 'breadcrumbs':
|
|
71
71
|
let current;
|
|
@@ -77,7 +77,7 @@ function to ( type, dependencies, inData ) { // convert from flat to 'data-typ
|
|
|
77
77
|
current = k
|
|
78
78
|
count = 0
|
|
79
79
|
}
|
|
80
|
-
let key = `${k}/${count}
|
|
80
|
+
let key = ( k === 'root' ) ? count : `${k}/${count}`
|
|
81
81
|
breadcrumbs[key] = v
|
|
82
82
|
count++
|
|
83
83
|
}
|
package/src/convertors/tuples.js
CHANGED
|
@@ -140,18 +140,18 @@ function toFlat ( dependencies, d ) { // Converts data to 'dt' model
|
|
|
140
140
|
|
|
141
141
|
function toType ( dt ) { // Converts data to tuples
|
|
142
142
|
let result = [];
|
|
143
|
+
|
|
143
144
|
dt.forEach ( line => {
|
|
144
145
|
const
|
|
145
146
|
[ name, flatData, breadcrumbs ] = line
|
|
146
147
|
, isArray = flatData instanceof Array ? true : false
|
|
147
|
-
// , result = []
|
|
148
148
|
;
|
|
149
149
|
let nakedBread = '';
|
|
150
150
|
if ( name !== 'root' ) nakedBread = breadcrumbs.replace ( `root/`, '' )
|
|
151
151
|
|
|
152
152
|
if ( isArray ) {
|
|
153
|
-
if ( nakedBread.length == 0 ) result.push ([
|
|
154
|
-
else
|
|
153
|
+
if ( nakedBread.length == 0 ) flatData.forEach ( (el,i) => result.push ([`root`, el]))
|
|
154
|
+
else flatData.forEach ( el => result.push ([nakedBread, el]))
|
|
155
155
|
return
|
|
156
156
|
}
|
|
157
157
|
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
function extractList ( dependencies, flatIO, indexFn ) {
|
|
6
|
+
return function exportList ( list, options={} ) {
|
|
7
|
+
const
|
|
8
|
+
root = indexFn ( 'root' )
|
|
9
|
+
, { main } = dependencies ()
|
|
10
|
+
, { load } = main ()
|
|
11
|
+
, possibleAs = ['standard', 'std', 'midFlat', 'tuple', 'tuples', 'breadcrumb', 'breadcrumbs', 'files', 'file' ]
|
|
12
|
+
;
|
|
13
|
+
let
|
|
14
|
+
error = false
|
|
15
|
+
, errorMsg = ''
|
|
16
|
+
;
|
|
17
|
+
|
|
18
|
+
if ( options ) {
|
|
19
|
+
if ( !options.as ) {
|
|
20
|
+
error = true
|
|
21
|
+
errorMsg = `Options should be an object and property "as" is required`
|
|
22
|
+
}
|
|
23
|
+
if ( !error && !possibleAs.includes(options.as) ) {
|
|
24
|
+
error = true
|
|
25
|
+
errorMsg = `Invalid option "as" value: ${options.as}. Choose one of: std, standard, ''`
|
|
26
|
+
}
|
|
27
|
+
if ( error ) throw new Error ( `Invalid option "as" value: ${options.as}` )
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return list
|
|
31
|
+
.map ( name => {
|
|
32
|
+
let lines = flatIO.export ( name );
|
|
33
|
+
if ( lines.length === 0 ) return root[1][name] ? root[1][name] : null
|
|
34
|
+
else return lines
|
|
35
|
+
})
|
|
36
|
+
.map ( item => {
|
|
37
|
+
if ( item == null ) return null
|
|
38
|
+
if ( !(item instanceof Array)) return item
|
|
39
|
+
return options ? load(item).model(() => options ) : load(item)
|
|
40
|
+
})
|
|
41
|
+
}} // extractList func.
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
export default extractList
|
|
46
|
+
|
|
47
|
+
|
package/src/flatObject/index.js
CHANGED
|
@@ -8,6 +8,9 @@ import copy from './copy.js'
|
|
|
8
8
|
import model from './model.js'
|
|
9
9
|
import query from './query.js'
|
|
10
10
|
import setupFilter from './setupFilter.js'
|
|
11
|
+
import extractList from './extractList.js'
|
|
12
|
+
|
|
13
|
+
|
|
11
14
|
|
|
12
15
|
|
|
13
16
|
|
|
@@ -31,20 +34,24 @@ function flatObject ( dependencies, d ) {
|
|
|
31
34
|
return res
|
|
32
35
|
}, [] )
|
|
33
36
|
}
|
|
34
|
-
, index
|
|
35
|
-
if ( n == null ) return null
|
|
36
|
-
let r = flatIO.getLine(n)
|
|
37
|
-
if ( r ) {
|
|
38
|
-
let [ name, d, breadcrumbs, links ] = r;
|
|
39
|
-
let copy;
|
|
40
|
-
if ( d instanceof Array ) copy = [...d ]
|
|
41
|
-
else copy = {...d }
|
|
42
|
-
return [ name, copy, breadcrumbs, [...links] ]
|
|
43
|
-
}
|
|
44
|
-
else return null
|
|
45
|
-
} // index
|
|
37
|
+
, index
|
|
46
38
|
}
|
|
47
39
|
;
|
|
40
|
+
|
|
41
|
+
function index (n) {
|
|
42
|
+
if ( n == null ) return null
|
|
43
|
+
let r = flatIO.getLine(n)
|
|
44
|
+
if ( r ) {
|
|
45
|
+
let [ name, d, breadcrumbs, links ] = r;
|
|
46
|
+
let copy;
|
|
47
|
+
if ( d instanceof Array ) copy = [...d ]
|
|
48
|
+
else copy = {...d }
|
|
49
|
+
return [ name, copy, breadcrumbs, [...links] ]
|
|
50
|
+
}
|
|
51
|
+
else return null
|
|
52
|
+
} // index func.
|
|
53
|
+
|
|
54
|
+
objectAPI [ 'extractList' ] = extractList ( dependencies, flatIO, index )
|
|
48
55
|
return objectAPI
|
|
49
56
|
} // flatObject func.
|
|
50
57
|
|
package/src/flatObject/model.js
CHANGED
|
@@ -10,7 +10,7 @@ return function model () {
|
|
|
10
10
|
, { convert } = dependencies ()
|
|
11
11
|
, selection = flatIO.getSelection ()
|
|
12
12
|
, data = ( selection.length === 0 ) ? flatIO.export() : selection
|
|
13
|
-
, CONVERTOR_NAMES = [ 'standard', 'std', 'midFlat', 'tuple', 'tuples', 'breadcrumb', 'breadcrumbs' ]
|
|
13
|
+
, CONVERTOR_NAMES = [ 'standard', 'std', 'midFlat', 'tuple', 'tuples', 'breadcrumb', 'breadcrumbs', 'files', 'file' ]
|
|
14
14
|
, hasConvertor = as ? CONVERTOR_NAMES.includes ( as ) : false
|
|
15
15
|
;
|
|
16
16
|
|
package/src/main.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
DT object & DT Toolbox
|
|
5
5
|
=======================
|
|
6
6
|
|
|
7
|
-
Version
|
|
7
|
+
Version 7.1.0
|
|
8
8
|
|
|
9
9
|
History notes:
|
|
10
10
|
- Idea was born on March 17th, 2016.
|
|
@@ -30,6 +30,10 @@
|
|
|
30
30
|
* Data inserts;
|
|
31
31
|
* Filters for faster data scan;
|
|
32
32
|
* Model and query functions to shape results;
|
|
33
|
+
|
|
34
|
+
- Version 7.1.0 published on August 29th, 2023
|
|
35
|
+
* Some fixes on version v.7.0.0;
|
|
36
|
+
* New method `extractList` for multiple data extraction from dt-model;
|
|
33
37
|
*/
|
|
34
38
|
|
|
35
39
|
|
package/src/mainLib.js
CHANGED
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
import walk from '@peter.naydenov/walk'
|
|
6
6
|
|
|
7
7
|
import flatObject from './flatObject/index.js' // Flat data api.
|
|
8
|
-
import flatData from './flatData/index.js'
|
|
9
|
-
import convert from './convertors/index.js'
|
|
8
|
+
import flatData from './flatData/index.js' // Query data api. Used in model functions
|
|
9
|
+
import convert from './convertors/index.js' // Convertors
|
|
10
10
|
|
|
11
11
|
|
|
12
12
|
|