dt-toolbox 7.2.0 → 7.4.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 +16 -0
- package/README.md +9 -3
- package/package.json +3 -3
- package/src/convertors/index.js +5 -0
- package/src/flatObject/extractList.js +4 -3
- package/src/flatObject/index.js +1 -1
- package/src/flatObject/model.js +6 -5
- package/src/flatObject/setupFilter.js +3 -2
- package/src/main.js +9 -5
- package/src/mainLib.js +100 -20
package/Changelog.md
CHANGED
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
## Release History
|
|
2
2
|
|
|
3
3
|
|
|
4
|
+
|
|
5
|
+
### 7.4.0 ( 2023-11-3)
|
|
6
|
+
- [x] Types were added to the project;
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### 7.3.0 ( 2023-10-19 )
|
|
12
|
+
- [x] Model function recognize a `dt-model` model. Result is equal to result of `export`;
|
|
13
|
+
- [x] Model function recognize a `dt-object` model. Returns a new dt-object;
|
|
14
|
+
- [x] Method `extractList` recognize a `dt-model` and `dt-object` models;
|
|
15
|
+
- [x] Method `init` recognize a `dt-model` model. Result is equal to result of `load`;
|
|
16
|
+
- [x] Update of @peter.naydenov/walk to version 4.2.0;
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
4
20
|
### 7.2.0 (2023-09-18)
|
|
5
21
|
- [x] Convertors recognize HTML DOM nodes. Makes a copy by reference;
|
|
6
22
|
- [x] Convertors recognize functions. Makes a copy by reference;
|
package/README.md
CHANGED
|
@@ -2,12 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|

|
|
4
4
|

|
|
5
|
+

|
|
6
|
+

|
|
7
|
+

|
|
8
|
+

|
|
9
|
+

|
|
5
10
|
|
|
6
11
|
|
|
7
12
|
|
|
8
|
-
## Last Updates
|
|
9
13
|
|
|
10
|
-
|
|
14
|
+
## Last Updates
|
|
15
|
+
- After version 7.3.0 extractList options.as can receive a `dt-model` and `dt-object`;
|
|
16
|
+
- 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.
|
|
11
17
|
|
|
12
18
|
|
|
13
19
|
|
|
@@ -543,7 +549,7 @@ const storage = dtbox.init ( first ); // first will become a root segment
|
|
|
543
549
|
storage.insertSegment ( 'second', second );
|
|
544
550
|
storage.insertSegment ( 'third', third );
|
|
545
551
|
|
|
546
|
-
const [ a, b c, firstData, otherData ] = storage.extractList ( ['first', 'second', 'third', 'data', 'secondData' ], {
|
|
552
|
+
const [ a, b c, firstData, otherData ] = storage.extractList ( ['first', 'second', 'third', 'data', 'secondData' ], { as: 'std' } ));
|
|
547
553
|
// a -> { name: 'first', data: 'first data' }
|
|
548
554
|
// b -> { name: 'second', data: 'second data' }
|
|
549
555
|
// c -> { name: 'third', data: 'third data' }
|
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.4.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Peter Naydenov",
|
|
7
7
|
"keywords": [
|
|
@@ -28,11 +28,11 @@
|
|
|
28
28
|
},
|
|
29
29
|
"homepage": "https://github.com/PeterNaydenov/dt-toolbox#readme",
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@peter.naydenov/walk": "4.
|
|
31
|
+
"@peter.naydenov/walk": "4.2.0"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"c8": "^8.0.1",
|
|
35
|
-
"chai": "^4.3.
|
|
35
|
+
"chai": "^4.3.10",
|
|
36
36
|
"mocha": "^10.2.0"
|
|
37
37
|
},
|
|
38
38
|
"c8": {
|
package/src/convertors/index.js
CHANGED
|
@@ -50,14 +50,19 @@ function from ( type ) { // convert from something to flat...
|
|
|
50
50
|
function to ( type, dependencies, inData ) { // convert from flat to 'data-type'...
|
|
51
51
|
const
|
|
52
52
|
breadcrumbs = {}
|
|
53
|
+
, { walk } = dependencies ()
|
|
53
54
|
, check = new Set()
|
|
54
55
|
, extend = new Set()
|
|
55
56
|
;
|
|
56
57
|
let tups, count = 0;
|
|
57
58
|
switch ( type ) {
|
|
59
|
+
case 'flat':
|
|
60
|
+
case 'dt-model':
|
|
61
|
+
return walk ({data:inData })
|
|
58
62
|
case 'std' :
|
|
59
63
|
case 'standard':
|
|
60
64
|
return std.toType ( inData )
|
|
65
|
+
case 'midflat' :
|
|
61
66
|
case 'midFlat' :
|
|
62
67
|
return midFlat.toType ( inData )
|
|
63
68
|
case 'tuple':
|
|
@@ -7,6 +7,7 @@ return function extractList ( list, options ) {
|
|
|
7
7
|
const
|
|
8
8
|
root = indexFn ( 'root' )
|
|
9
9
|
, { main:{load}, INIT_DATA_TYPES } = dependencies ()
|
|
10
|
+
, asTypes = [ ...INIT_DATA_TYPES, 'dt-object' ]
|
|
10
11
|
;
|
|
11
12
|
let
|
|
12
13
|
error = false
|
|
@@ -18,9 +19,9 @@ return function extractList ( list, options ) {
|
|
|
18
19
|
error = true
|
|
19
20
|
errorMsg = `Options should be an object and property "as" is required`
|
|
20
21
|
}
|
|
21
|
-
if ( !error && !
|
|
22
|
+
if ( !error && !asTypes.includes(options.as) ) {
|
|
22
23
|
error = true
|
|
23
|
-
errorMsg = `Invalid option "as" value: ${options.as}
|
|
24
|
+
errorMsg = `Invalid option "as" value: ${options.as}.`
|
|
24
25
|
}
|
|
25
26
|
if ( error ) throw new Error ( errorMsg )
|
|
26
27
|
}
|
|
@@ -34,7 +35,7 @@ return function extractList ( list, options ) {
|
|
|
34
35
|
.map ( item => {
|
|
35
36
|
if ( item == null ) return null
|
|
36
37
|
if ( !(item instanceof Array)) return item
|
|
37
|
-
return
|
|
38
|
+
return load(item).model(() => options )
|
|
38
39
|
})
|
|
39
40
|
}} // extractList func.
|
|
40
41
|
|
package/src/flatObject/index.js
CHANGED
|
@@ -21,7 +21,7 @@ function flatObject ( dependencies, d ) {
|
|
|
21
21
|
, objectAPI = {
|
|
22
22
|
// I/O Operations
|
|
23
23
|
insertSegment : insert ( dependencies, flatIO ) // Extends available data with new data segment;
|
|
24
|
-
, export : ex ( dependencies, flatIO ) // Returns flat data;
|
|
24
|
+
, export : ex ( dependencies, flatIO ) // Returns a flat data;
|
|
25
25
|
, copy : copy ( dependencies, flatIO ) // Creates deep copy of original data
|
|
26
26
|
, model : model ( dependencies, flatIO, flatStore ) // Arrange data according specific data-model. Model should come as a function;
|
|
27
27
|
, query : query ( dependencies, flatIO, flatStore ) // Request, and evaluate data. Returns a new flat object;
|
package/src/flatObject/model.js
CHANGED
|
@@ -9,7 +9,7 @@ return function model () {
|
|
|
9
9
|
, selectionIx = {}
|
|
10
10
|
;
|
|
11
11
|
const
|
|
12
|
-
{ convert, draft } = dependencies ()
|
|
12
|
+
{ convert, draft, INIT_DATA_TYPES, main:{load} } = dependencies ()
|
|
13
13
|
, draft_API = {
|
|
14
14
|
set : draft.set ( selection, selectionIx ) // Selection: Creates a new row;
|
|
15
15
|
, connect : draft.connect ( selectionIx ) // Selection: Creates an edges among rows;
|
|
@@ -19,18 +19,19 @@ return function model () {
|
|
|
19
19
|
, [ fn, ...args ] = arguments
|
|
20
20
|
, { as } = fn ( {...flatStore,...draft_API}, ...args ) || {}
|
|
21
21
|
, data = ( selection.length === 0 ) ? flatIO.export() : selection
|
|
22
|
-
,
|
|
23
|
-
, hasConvertor = as ? CONVERTOR_NAMES.includes ( as ) : false
|
|
22
|
+
, hasConvertor = as ? INIT_DATA_TYPES.includes ( as ) : false
|
|
24
23
|
;
|
|
25
24
|
|
|
26
25
|
let result;
|
|
26
|
+
if ( as === 'dt-object' ) {
|
|
27
|
+
return load ( data )
|
|
28
|
+
}
|
|
27
29
|
if ( as && !hasConvertor ) {
|
|
28
30
|
console.error ( `Model '${as}' is unknown data-model.` )
|
|
29
31
|
return null
|
|
30
32
|
}
|
|
31
|
-
|
|
32
33
|
if ( as ) result = convert.to ( as, dependencies, data )
|
|
33
|
-
else result = data
|
|
34
|
+
else result = load ( data )
|
|
34
35
|
flatIO.resetScan ()
|
|
35
36
|
return result
|
|
36
37
|
}} // model func.
|
package/src/main.js
CHANGED
|
@@ -43,12 +43,16 @@ const { init, load, flating, converting, getWalk } = mainLib;
|
|
|
43
43
|
|
|
44
44
|
|
|
45
45
|
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
46
50
|
const dtbox = {
|
|
47
|
-
|
|
48
|
-
,
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
51
|
+
init,
|
|
52
|
+
load,
|
|
53
|
+
flat : flating,
|
|
54
|
+
convert : converting,
|
|
55
|
+
getWalk
|
|
52
56
|
} // dtbox func.
|
|
53
57
|
|
|
54
58
|
|
package/src/mainLib.js
CHANGED
|
@@ -16,13 +16,54 @@ const INIT_DATA_TYPES = [
|
|
|
16
16
|
, 'tuple', 'tuples'
|
|
17
17
|
, 'breadcrumb', 'breadcrumbs'
|
|
18
18
|
, 'file', 'files'
|
|
19
|
-
, 'midFlat'
|
|
20
|
-
, 'flat'
|
|
19
|
+
, 'midFlat', 'midflat'
|
|
20
|
+
, 'flat', 'dt-model'
|
|
21
21
|
]
|
|
22
22
|
;
|
|
23
23
|
|
|
24
24
|
|
|
25
25
|
|
|
26
|
+
/**
|
|
27
|
+
* @typedef {(number|string|boolean)} Primitives
|
|
28
|
+
* @typedef {('std'|'tuples'|'breadcrumbs'|'files'|'midFlat'|'flat'|'dt-model')} DtmodelNames
|
|
29
|
+
* @typedef {(filterName:string,fn:function)=>void} setupFilterFn
|
|
30
|
+
* @typedef {(segmentName:string, segment:DTObject)=>void} insertSegmentFn
|
|
31
|
+
* @typedef {(request:Array<string>,options:{as:DtmodelNames}=) => Array<RequestedModel>} extractListFn;
|
|
32
|
+
* @typedef {(fn:queryFunction, ...args)=>DTObject} QueryFn
|
|
33
|
+
* @typedef {(breadcrumbs:string) => dtLine } IndexFn
|
|
34
|
+
*
|
|
35
|
+
*
|
|
36
|
+
*
|
|
37
|
+
* @typedef {Object} DTObject
|
|
38
|
+
* @property {insertSegmentFn} insertSegment - Extends available data with new data segment;
|
|
39
|
+
* @property {(segmentName:string)=>Dtmodel} export - Returns a dt-model of the data;
|
|
40
|
+
* @property {Function} copy - Creates deep copy of original data segment;
|
|
41
|
+
* @property {Function} model - Arrange data according specific data-model. Model should come as a function;
|
|
42
|
+
* @property {QueryFn} query - Request, and evaluate data. Returns a new flat object;
|
|
43
|
+
* @property {setupFilterFn} setupFilter - Functions should filter content according some criteria. Generated indexes will help for data search in;
|
|
44
|
+
* @property {Function} listSegments - Returns list of segments in flat data;
|
|
45
|
+
* @property {IndexFn} index - Returns dt-line by breadcrumbs;
|
|
46
|
+
* @property {extractListFn} extractList - Extracts list of data segments;
|
|
47
|
+
*
|
|
48
|
+
*
|
|
49
|
+
*
|
|
50
|
+
*
|
|
51
|
+
*
|
|
52
|
+
* @typedef {{0:string,1:(Array<Primitives>|Object<string,Primitives>),2:string,3:string[]}} Dtmodel - dt-model
|
|
53
|
+
* @param Dtmodel[]0 - name of the dt-line;
|
|
54
|
+
* @param Dtmodel[]1 - flat data;
|
|
55
|
+
* @param Dtmodel[]2 - breadcrumbs of the dt-line;
|
|
56
|
+
* @param Dtmodel[]3 - children breadcrumbs(edges);
|
|
57
|
+
*
|
|
58
|
+
*
|
|
59
|
+
*
|
|
60
|
+
* @typedef {Object} Initoptions
|
|
61
|
+
* @description dtbox.init options
|
|
62
|
+
* @property {string} [model='std'] model - data model according library's data models.(Look at the documentation)
|
|
63
|
+
*/
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
26
67
|
const mainLib = {
|
|
27
68
|
|
|
28
69
|
dependencies () {
|
|
@@ -43,11 +84,19 @@ const mainLib = {
|
|
|
43
84
|
}
|
|
44
85
|
, draft
|
|
45
86
|
}
|
|
46
|
-
}
|
|
87
|
+
}, // dependencies func.
|
|
47
88
|
|
|
48
89
|
|
|
49
90
|
|
|
50
|
-
|
|
91
|
+
/**
|
|
92
|
+
* @function init
|
|
93
|
+
* @description Create a dt-object from data source
|
|
94
|
+
* @param {(Array|Object)} inData - data source
|
|
95
|
+
* @param {Object} options - options {}
|
|
96
|
+
* @param {('std'|'tuples'|'breadcrumbs'|'files'|'midFlat'|'flat'|'dt-model')} options.model - model of the data source
|
|
97
|
+
* @returns {DTObject} - dt-object
|
|
98
|
+
*/
|
|
99
|
+
init ( inData, options={} ) {
|
|
51
100
|
let
|
|
52
101
|
defaultOptions = { model : 'std' }
|
|
53
102
|
, { model } = Object.assign ( {}, defaultOptions, options )
|
|
@@ -57,13 +106,23 @@ const mainLib = {
|
|
|
57
106
|
console.error ( `Can't understand your data-model: ${model}. Please, find what is possible on https://github.com/PeterNaydenov/dt-toolbox` )
|
|
58
107
|
return null
|
|
59
108
|
}
|
|
60
|
-
const d =
|
|
109
|
+
const d = ['flat', 'dt-model'].includes ( model ) ?
|
|
110
|
+
mainLib.load ( inData ) :
|
|
111
|
+
convert.from ( model ).toFlat ( dependencies, inData );
|
|
61
112
|
return flatObject ( dependencies, d )
|
|
62
|
-
} // init func.
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
113
|
+
}, // init func.
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* @function load
|
|
117
|
+
* @description Load data from dt-model
|
|
118
|
+
* @param {{0:string,1:(Array<Primitives>|Object<string,Primitives>),2:string,3:string[]}} inData - dt-model:
|
|
119
|
+
* @param inData[]0 - name of the dt-line;
|
|
120
|
+
* @param inData[]1 - flat data;
|
|
121
|
+
* @param inData[]2 - breadcrumbs of the dt-line;
|
|
122
|
+
* @param inData[]3 - children breadcrumbs(edges);
|
|
123
|
+
* @returns {DTObject} - dt-object
|
|
124
|
+
*/
|
|
125
|
+
load ( inData ) {
|
|
67
126
|
const index = {};
|
|
68
127
|
inData.forEach ( line => {
|
|
69
128
|
const [ , ,breadcrumbs ] = line;
|
|
@@ -71,11 +130,20 @@ const mainLib = {
|
|
|
71
130
|
})
|
|
72
131
|
const copy = walk ({data: inData });
|
|
73
132
|
return flatObject ( mainLib.dependencies, [copy, index, inData ] )
|
|
74
|
-
} // load func.
|
|
133
|
+
}, // load func.
|
|
75
134
|
|
|
76
135
|
|
|
77
136
|
|
|
78
|
-
|
|
137
|
+
/**
|
|
138
|
+
*
|
|
139
|
+
* @function flating
|
|
140
|
+
* @description Create a dt-model from data source
|
|
141
|
+
* @param {(Array|Object)} inData - data source. Object or array.
|
|
142
|
+
* @param {Object} options - options.
|
|
143
|
+
* @param {('std'|'tuples'|'breadcrumbs'|'files'|'midFlat'|'flat'|'dt-model')} options.model - model of the data source
|
|
144
|
+
* @returns {Dtmodel[]} - dt-model
|
|
145
|
+
*/
|
|
146
|
+
flating ( inData, options={} ) {
|
|
79
147
|
let
|
|
80
148
|
defaultOptions = { model : 'std' }
|
|
81
149
|
, { model } = Object.assign ( {}, defaultOptions, options )
|
|
@@ -83,11 +151,18 @@ const mainLib = {
|
|
|
83
151
|
if ( !INIT_DATA_TYPES.includes(model) ) return null
|
|
84
152
|
let [,,dt] = convert.from ('std').toFlat ( mainLib.dependencies, inData );
|
|
85
153
|
return dt
|
|
86
|
-
} // flating func.
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
154
|
+
}, // flating func.
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* @function converting
|
|
158
|
+
* @description Convert data from one model to another
|
|
159
|
+
* @param {Array|Object} inData - data source
|
|
160
|
+
* @param {Object} options
|
|
161
|
+
* @param {string} options.model - model of the data source
|
|
162
|
+
* @param {string} options.as - model to be converted to
|
|
163
|
+
* @returns {Array|Object} - converted data
|
|
164
|
+
*/
|
|
165
|
+
converting ( inData, options={} ) {
|
|
91
166
|
let
|
|
92
167
|
defaultOptions = { model : 'std', as: 'std' }
|
|
93
168
|
, { model, as } = Object.assign ( {}, defaultOptions, options )
|
|
@@ -97,11 +172,16 @@ const mainLib = {
|
|
|
97
172
|
let [,,dt] = convert.from ('std').toFlat ( mainLib.dependencies, inData );
|
|
98
173
|
|
|
99
174
|
return convert.to ( as, mainLib.dependencies, dt )
|
|
100
|
-
} // flating func.
|
|
175
|
+
}, // flating func.
|
|
176
|
+
|
|
101
177
|
|
|
102
178
|
|
|
103
|
-
|
|
104
|
-
|
|
179
|
+
/**
|
|
180
|
+
* @function getWalk
|
|
181
|
+
* @description Extract the 'walk' (@peter.naydenov/walk) dependency from dt-toolbox library
|
|
182
|
+
* @returns {Function} - the 'walk' function
|
|
183
|
+
*/
|
|
184
|
+
getWalk : () => walk
|
|
105
185
|
|
|
106
186
|
} // mainLib
|
|
107
187
|
|