dt-toolbox 6.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 +27 -0
- package/Migration.guide.md +68 -2
- package/README.md +81 -43
- package/README_v.6.x.x.md +1158 -0
- package/package.json +4 -4
- package/src/convertors/index.js +2 -2
- package/src/convertors/tuples.js +3 -3
- package/src/flatData/look.js +12 -5
- package/src/flatObject/extractList.js +47 -0
- package/src/flatObject/index.js +28 -13
- package/src/flatObject/insert.js +16 -6
- package/src/flatObject/model.js +1 -1
- package/src/main.js +5 -1
- package/src/mainLib.js +12 -3
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dt-toolbox",
|
|
3
3
|
"description": "Data manipulation tool",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "7.1.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Peter Naydenov",
|
|
7
7
|
"keywords": [
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"scripts": {
|
|
18
18
|
"start": "node src/main.js",
|
|
19
19
|
"test": "mocha test",
|
|
20
|
-
"cover"
|
|
20
|
+
"cover": "c8 mocha test"
|
|
21
21
|
},
|
|
22
22
|
"repository": {
|
|
23
23
|
"type": "git",
|
|
@@ -31,8 +31,8 @@
|
|
|
31
31
|
"@peter.naydenov/walk": "4.0.0"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"c8": "^
|
|
35
|
-
"chai": "^4.3.
|
|
34
|
+
"c8": "^8.0.1",
|
|
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
|
|
package/src/flatData/look.js
CHANGED
|
@@ -8,7 +8,10 @@ return function look ( fn ) {
|
|
|
8
8
|
const
|
|
9
9
|
[ name, flatData, breadcrumbs, edges ] = line
|
|
10
10
|
, isArray = flatData instanceof Array ? true : false
|
|
11
|
+
, next = () => '$__NEXT__LOOK_'
|
|
12
|
+
, finish = () => '$__FINISH__WITH__THE__LOOKING_'
|
|
11
13
|
;
|
|
14
|
+
let endLooking = false;
|
|
12
15
|
|
|
13
16
|
const links = edges.map ( edge => {
|
|
14
17
|
let child = edge.replace ( `${breadcrumbs}/`, '')
|
|
@@ -19,8 +22,10 @@ return function look ( fn ) {
|
|
|
19
22
|
if ( flatData.length === 0 ) empty([])
|
|
20
23
|
else {
|
|
21
24
|
flatData.every ( (value,key) => {
|
|
22
|
-
|
|
23
|
-
|
|
25
|
+
if ( endLooking ) return false // Cancel other iterations
|
|
26
|
+
const callback = fn({ value, key, name, flatData, breadcrumbs, links, next, finish });
|
|
27
|
+
if ( callback === finish() ) endLooking = true
|
|
28
|
+
return ( [ finish(), next() ].includes ( callback )) ? false : true
|
|
24
29
|
})
|
|
25
30
|
}
|
|
26
31
|
}
|
|
@@ -29,14 +34,16 @@ return function look ( fn ) {
|
|
|
29
34
|
if ( test.length === 0 ) empty({})
|
|
30
35
|
else {
|
|
31
36
|
test.every ( ([key, value]) => {
|
|
32
|
-
|
|
33
|
-
|
|
37
|
+
if ( endLooking ) return false // Cancel other iterations
|
|
38
|
+
const callback = fn ({ value, key, name, flatData, breadcrumbs, links, next, finish });
|
|
39
|
+
if ( callback === finish() ) endLooking = true
|
|
40
|
+
return ( [ finish(), next()].includes ( callback )) ? false : true
|
|
34
41
|
})
|
|
35
42
|
}
|
|
36
43
|
|
|
37
44
|
}
|
|
38
45
|
function empty ( flatData ) {
|
|
39
|
-
fn ({ value:null, key:null, name, flatData, breadcrumbs, links, empty: true })
|
|
46
|
+
fn ({ value:null, key:null, name, flatData, breadcrumbs, links, empty: true, next, finish })
|
|
40
47
|
} // empty func.
|
|
41
48
|
flatIO.resetScan ()
|
|
42
49
|
})
|
|
@@ -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
|
|
|
@@ -17,26 +20,38 @@ function flatObject ( dependencies, d ) {
|
|
|
17
20
|
, [ flatIO, flatStore ] = flatData ( dependencies, d )
|
|
18
21
|
, objectAPI = {
|
|
19
22
|
// I/O Operations
|
|
20
|
-
|
|
23
|
+
insertSegment : insert ( dependencies, flatIO ) // Extends available data with new data segment;
|
|
21
24
|
, export : ex ( dependencies, flatIO ) // Returns flat data;
|
|
22
25
|
, copy : copy ( dependencies, flatIO ) // Creates deep copy of original data
|
|
23
26
|
, model : model ( dependencies, flatIO, flatStore ) // Arrange data according specific data-model. Model should come as a function;
|
|
24
27
|
, query : query ( dependencies, flatIO, flatStore ) // Request, and evaluate data. Returns a new flat object;
|
|
25
28
|
, setupFilter : setupFilter ( flatIO ) // Functions should filter content according some criteria. Generated indexes will help for data search in
|
|
26
|
-
,
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
else return null
|
|
37
|
-
} // index
|
|
29
|
+
, listSegments : () => {
|
|
30
|
+
const ix = Object.keys ( flatIO.getIndexes() );
|
|
31
|
+
return ix.reduce ( (res,item) => {
|
|
32
|
+
if ( item.includes('/') ) return res
|
|
33
|
+
res.push ( item )
|
|
34
|
+
return res
|
|
35
|
+
}, [] )
|
|
36
|
+
}
|
|
37
|
+
, index
|
|
38
38
|
}
|
|
39
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 )
|
|
40
55
|
return objectAPI
|
|
41
56
|
} // flatObject func.
|
|
42
57
|
|
package/src/flatObject/insert.js
CHANGED
|
@@ -2,12 +2,22 @@
|
|
|
2
2
|
|
|
3
3
|
function insert ( dependencies, flatIO ) {
|
|
4
4
|
return function insert ( name, inData ) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
5
|
+
const { convert, isDTO, isDTM, walk } = dependencies();
|
|
6
|
+
let d, copy;
|
|
7
|
+
|
|
8
|
+
if ( isDTO(inData) ) {
|
|
9
|
+
d = inData.export ()
|
|
10
|
+
copy = inData.copy ()
|
|
11
|
+
|
|
12
|
+
}
|
|
13
|
+
else if ( isDTM(inData) ) {
|
|
14
|
+
d = walk ({ data : inData })
|
|
15
|
+
copy = walk ({ data : inData })
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
[ copy,, d ] = convert.from ( 'std' ).toFlat ( dependencies, inData )
|
|
19
|
+
console.warn ( 'A non "dt-object" data segment was inserted. Autoconverted to "dt-object".' )
|
|
20
|
+
}
|
|
11
21
|
|
|
12
22
|
const search = new RegExp ( `^root\/` );
|
|
13
23
|
d.forEach ( line => { // Word 'root' should be changed to the argument 'name'.
|
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
|
@@ -4,9 +4,9 @@
|
|
|
4
4
|
|
|
5
5
|
import walk from '@peter.naydenov/walk'
|
|
6
6
|
|
|
7
|
-
import flatObject from './flatObject/index.js'
|
|
8
|
-
import flatData
|
|
9
|
-
import convert
|
|
7
|
+
import flatObject from './flatObject/index.js' // Flat data api.
|
|
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
|
|
|
@@ -32,6 +32,15 @@ const mainLib = {
|
|
|
32
32
|
, convert
|
|
33
33
|
, INIT_DATA_TYPES
|
|
34
34
|
, main : () => ({ load : mainLib.load })
|
|
35
|
+
, isDTO : d => typeof d.insertSegment === 'function'
|
|
36
|
+
, isDTM : d => {
|
|
37
|
+
if ( !(d instanceof Array)) return false
|
|
38
|
+
if ( !(d[0] instanceof Array)) return false
|
|
39
|
+
if ( d[0].length !== 4 ) return false
|
|
40
|
+
if ( d[0][0] !== 'root' ) return false
|
|
41
|
+
return true
|
|
42
|
+
}
|
|
43
|
+
,
|
|
35
44
|
}
|
|
36
45
|
} // dependencies func.
|
|
37
46
|
|