dt-toolbox 5.0.0 → 6.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.
Files changed (82) hide show
  1. package/Changelog.md +7 -15
  2. package/LICENSE +1 -1
  3. package/Migration.guide.md +14 -1
  4. package/README.md +978 -347
  5. package/README_v.2.x.x.md +0 -1
  6. package/README_v.4.x.x.md +532 -0
  7. package/package.json +22 -25
  8. package/src/convertors/index.js +43 -34
  9. package/src/convertors/midflat.js +76 -132
  10. package/src/convertors/standard.js +61 -86
  11. package/src/convertors/tuples.js +171 -0
  12. package/src/flatData/connect.js +50 -0
  13. package/src/flatData/export.js +46 -0
  14. package/src/flatData/filters/list.js +11 -0
  15. package/src/flatData/filters/listObject.js +13 -0
  16. package/src/flatData/filters/object.js +12 -0
  17. package/src/flatData/filters/root.js +12 -0
  18. package/src/flatData/find.js +20 -0
  19. package/src/flatData/from.js +33 -0
  20. package/src/flatData/get.js +29 -0
  21. package/src/flatData/index.js +101 -0
  22. package/src/flatData/insert.js +24 -0
  23. package/src/flatData/like.js +24 -0
  24. package/src/flatData/look.js +49 -0
  25. package/src/flatData/push.js +19 -0
  26. package/src/flatData/save.js +17 -0
  27. package/src/flatData/set.js +26 -0
  28. package/src/flatData/setupFilter.js +21 -0
  29. package/src/flatData/use.js +16 -0
  30. package/src/flatObject/copy.js +19 -0
  31. package/src/flatObject/export.js +13 -0
  32. package/src/flatObject/index.js +47 -0
  33. package/src/flatObject/insert.js +28 -0
  34. package/src/flatObject/model.js +32 -0
  35. package/src/flatObject/query.js +24 -0
  36. package/src/flatObject/setupFilter.js +12 -0
  37. package/src/main.js +21 -689
  38. package/src/mainLib.js +105 -0
  39. package/src/compareMethod/change.js +0 -21
  40. package/src/compareMethod/different.js +0 -18
  41. package/src/compareMethod/identical.js +0 -21
  42. package/src/compareMethod/index.js +0 -13
  43. package/src/compareMethod/missing.js +0 -18
  44. package/src/compareMethod/same.js +0 -18
  45. package/src/convertors/breadcrumbs.js +0 -148
  46. package/src/help/extractSelection.js +0 -12
  47. package/src/help/filterObject.js +0 -25
  48. package/src/help/hasNumbers.js +0 -14
  49. package/src/help/index.js +0 -35
  50. package/src/help/isItPrimitive.js +0 -10
  51. package/src/help/objectsByLevel.js +0 -16
  52. package/src/help/reduceTuples.js +0 -29
  53. package/src/help/sanitizeFlatKeys.js +0 -30
  54. package/src/help/toBreadcrumbsKeys.js +0 -28
  55. package/src/help/tuplesToBreadcrumbs.js +0 -25
  56. package/src/help/updateSelection.js +0 -12
  57. package/src/help/zipObject.js +0 -24
  58. package/src/lib/assemble.js +0 -45
  59. package/src/lib/attach.js +0 -20
  60. package/src/lib/block.js +0 -33
  61. package/src/lib/combine.js +0 -38
  62. package/src/lib/deep.js +0 -23
  63. package/src/lib/find.js +0 -36
  64. package/src/lib/folder.js +0 -42
  65. package/src/lib/index.js +0 -56
  66. package/src/lib/modify.js +0 -22
  67. package/src/lib/parent.js +0 -51
  68. package/src/lib/purify.js +0 -37
  69. package/src/lib/transform.js +0 -55
  70. package/src/modifiers/add.js +0 -18
  71. package/src/modifiers/append.js +0 -18
  72. package/src/modifiers/combineShallow.js +0 -119
  73. package/src/modifiers/flatten.js +0 -38
  74. package/src/modifiers/index.js +0 -34
  75. package/src/modifiers/insert.js +0 -21
  76. package/src/modifiers/keyPrefix.js +0 -27
  77. package/src/modifiers/mix.js +0 -25
  78. package/src/modifiers/overwrite.js +0 -18
  79. package/src/modifiers/prepend.js +0 -19
  80. package/src/modifiers/reverse.js +0 -23
  81. package/src/modifiers/update.js +0 -18
  82. package/src/simple.js +0 -49
package/src/mainLib.js ADDED
@@ -0,0 +1,105 @@
1
+ 'use strict'
2
+
3
+
4
+
5
+ import walk from '@peter.naydenov/walk'
6
+
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
+
11
+
12
+
13
+ const INIT_DATA_TYPES = [
14
+ 'std', 'standard'
15
+ , 'tuple', 'tuples'
16
+ , 'breadcrumb', 'breadcrumbs'
17
+ , 'file', 'files'
18
+ , 'midFlat'
19
+ , 'flat'
20
+ ]
21
+ ;
22
+
23
+
24
+
25
+ const mainLib = {
26
+
27
+ dependencies () {
28
+ return {
29
+ walk
30
+ , flatData
31
+ , flatObject
32
+ , convert
33
+ , INIT_DATA_TYPES
34
+ , main : () => ({ load : mainLib.load })
35
+ }
36
+ } // dependencies func.
37
+
38
+
39
+
40
+ , init ( inData, options={} ) {
41
+ let
42
+ defaultOptions = { model : 'std' }
43
+ , { model } = Object.assign ( {}, defaultOptions, options )
44
+ , dependencies = mainLib.dependencies
45
+ ;
46
+ if ( !INIT_DATA_TYPES.includes(model) ) {
47
+ console.error ( `Can't understand your data-model: ${model}. Please, find what is possible on https://github.com/PeterNaydenov/dt-toolbox` )
48
+ return null
49
+ }
50
+ const
51
+ d = convert.from(model).toFlat ( dependencies, inData )
52
+ , flat = flatObject ( dependencies, d )
53
+ ;
54
+ return flat
55
+ } // init func.
56
+
57
+
58
+
59
+ , load ( inData ) {
60
+ const index = {};
61
+ inData.forEach ( line => {
62
+ const [ , ,breadcrumbs ] = line;
63
+ index[breadcrumbs] = line
64
+ })
65
+ const copy = walk ({data: inData });
66
+ return flatObject ( mainLib.dependencies, [copy, index, inData ] )
67
+ } // load func.
68
+
69
+
70
+
71
+ , flating ( inData, options={} ) {
72
+ let
73
+ defaultOptions = { model : 'std' }
74
+ , { model } = Object.assign ( {}, defaultOptions, options )
75
+ ;
76
+ if ( !INIT_DATA_TYPES.includes(model) ) return null
77
+ let [,,dt] = convert.from ('std').toFlat ( mainLib.dependencies, inData );
78
+ return dt
79
+ } // flating func.
80
+
81
+
82
+
83
+ , converting ( inData, options={} ) {
84
+ let
85
+ defaultOptions = { model : 'std', as: 'std' }
86
+ , { model, as } = Object.assign ( {}, defaultOptions, options )
87
+ ;
88
+ if ( !INIT_DATA_TYPES.includes(model) ) return null
89
+ if ( !INIT_DATA_TYPES.includes(as) ) return null
90
+ let [,,dt] = convert.from ('std').toFlat ( mainLib.dependencies, inData );
91
+
92
+ return convert.to ( as, mainLib.dependencies, dt )
93
+ } // flating func.
94
+
95
+
96
+
97
+ , getWalk : () => walk
98
+
99
+ } // mainLib
100
+
101
+
102
+
103
+ export default mainLib
104
+
105
+
@@ -1,21 +0,0 @@
1
- function change ( mainData, checkData ) {
2
- // *** Compare values. Reduce to key/value pairs with different values.
3
- let
4
- result = {}
5
- , mainKeys = Object.keys ( mainData )
6
- ;
7
- mainKeys.forEach ( k => {
8
- let
9
- HAS_KEY = ( checkData.hasOwnProperty(k) )
10
- , EQUAL_VALUES = HAS_KEY && ( mainData[k] === checkData[k] )
11
- ;
12
- if ( !EQUAL_VALUES ) result[k] = checkData[k]
13
- })
14
- return result
15
- } // change func.
16
-
17
-
18
-
19
- export default change
20
-
21
-
@@ -1,18 +0,0 @@
1
- function different ( mainData, checkData ) {
2
- // *** Key compare. Returns key/value pairs where key does not exist.
3
- let
4
- result = {}
5
- , checkKeys = Object.keys ( checkData )
6
- ;
7
- checkKeys.forEach ( k => {
8
- let HAS_KEY = mainData.hasOwnProperty(k);
9
- if ( !HAS_KEY ) result[k] = checkData[k]
10
- })
11
- return result
12
- } // different func.
13
-
14
-
15
-
16
- export default different
17
-
18
-
@@ -1,21 +0,0 @@
1
- function identical ( mainData, checkData ) {
2
- // *** Compare values. Reduce data to identical key/value pairs.
3
- let
4
- result = {}
5
- , mainKeys = Object.keys ( mainData )
6
- ;
7
- mainKeys.forEach ( k => {
8
- let
9
- HAS_KEY = ( checkData.hasOwnProperty(k) )
10
- , EQUAL_VALUES = HAS_KEY && ( mainData[k] === checkData[k] )
11
- ;
12
- if ( EQUAL_VALUES ) result[k] = mainData[k]
13
- })
14
- return result
15
- } // identical func.
16
-
17
-
18
-
19
- export default identical
20
-
21
-
@@ -1,13 +0,0 @@
1
- import identical from './identical.js'
2
- import change from './change.js'
3
- import same from './same.js'
4
- import different from './different.js'
5
- import missing from './missing.js'
6
-
7
- export default {
8
- identical
9
- , change
10
- , same
11
- , different
12
- , missing
13
- }
@@ -1,18 +0,0 @@
1
- function missing ( mainData, checkData ) {
2
- // *** Key compare. Returns key/value pairs that are missing
3
- let
4
- result = {}
5
- , mainKeys = Object.keys ( mainData )
6
- ;
7
- mainKeys.forEach ( k => {
8
- let HAS_KEY = checkData.hasOwnProperty(k);
9
- if ( !HAS_KEY ) result[k] = mainData[k]
10
- })
11
- return result
12
- } // missing func.
13
-
14
-
15
-
16
- export default missing
17
-
18
-
@@ -1,18 +0,0 @@
1
- function same ( mainData, checkData ) {
2
- // *** Compare keys. Returns key/value pairs where keys are the same.
3
- let
4
- result = {}
5
- , mainKeys = Object.keys ( mainData )
6
- ;
7
- mainKeys.forEach ( k => {
8
- let HAS_KEY = checkData.hasOwnProperty(k);
9
- if ( HAS_KEY ) result[k] = checkData[k]
10
- })
11
- return result
12
- } // same func.
13
-
14
-
15
-
16
- export default same
17
-
18
-
@@ -1,148 +0,0 @@
1
- 'use strict'
2
-
3
-
4
-
5
- /**
6
- * File data structure example:
7
- * [
8
- 'rootFolder/profile/name/Peter',
9
- 'rootFolder/profile/age/42',
10
- 'rootFolder/profile/gender/male',
11
- 'rootFolder/logs/log.txt',
12
- 'rootFolder/friends/Dimitar',
13
- 'rootFolder/friends/Stefan',
14
- 'rootFolder/friends/Ivan',
15
- 'rootFolder/friends/Grigor'
16
- ]
17
- *
18
- * Looks like breadcrumbs but last element is the value. Key and value together.
19
- *
20
- */
21
-
22
- function toFlat ( dependencies, rawValue ) {
23
- if ( Object.keys(rawValue).length === 0 ) {
24
- let
25
- structure = [ [ 'object', 0 ] ]
26
- , value = {}
27
- ;
28
- return [ structure, value ]
29
- }
30
- let
31
- { help } = dependencies
32
- , structure = []
33
- , value = {}
34
- , keyList = Object // Provides keys in order: from larger to shorter
35
- .keys ( rawValue )
36
- .map ( x => x.split('/') )
37
- .sort ( (a,b) => b.length - a.length )
38
- , tProps = {} // { objectLocation<string> : childObjectNames<set> }
39
- , structRaw = [] // objectLocation strings
40
- , propDone = []
41
- ;
42
-
43
- keyList.forEach ( keyArr => { // Setup our temp structures: tProps and structRaw
44
- keyArr.forEach ( (k,i) => {
45
- let parent = keyArr.slice( 0, i ).join ( '/' );
46
- if ( !parent ) return
47
- if ( !tProps[parent] ) {
48
- tProps[parent] = new Set()
49
- structRaw.push ( parent )
50
- }
51
- tProps[parent].add ( k )
52
- })
53
- })
54
- structRaw.forEach ( (el,i) => { // Build the structure rows
55
- let elChildren = false;
56
- if ( !structure[i] ) structure[i] = [ 'object', i ]
57
- if ( propDone[i] ) el = propDone[i]
58
- elChildren = Array.from ( tProps[el] )
59
- if ( el === 'root' ) {
60
- const mainType = help.hasNumbers ( elChildren ) ? 'array' : 'object';
61
- structure[i][0] = mainType
62
- }
63
- elChildren.forEach ( name => { // Setup connections between object
64
- let test = tProps[`${el}/${name}`];
65
- if ( !test ) return
66
- let
67
- childProps = Array.from (test)
68
- , type = help.hasNumbers ( childProps ) ? 'array' : 'object'
69
- , lastIndex = structure.length
70
- ;
71
- propDone[lastIndex] = `${el}/${name}`
72
- structure.push ([type, lastIndex])
73
- structure[i].push ( [lastIndex, name])
74
- })
75
- })
76
-
77
- keyList.forEach ( k => { // Create flat keys and write values
78
- let
79
- name = k.pop()
80
- , keySearch = k.join ( '/' )
81
- , objNumber = null
82
- , theKey = ''
83
- ;
84
- objNumber = propDone.indexOf ( keySearch )
85
-
86
- if ( objNumber < 0 ) objNumber = structRaw.indexOf(keySearch)
87
- theKey = `root/${objNumber}/${name}`
88
- value[theKey] = rawValue[`${keySearch}/${name}`]
89
- })
90
- return [ structure, value ]
91
- } // toFlat func.
92
-
93
-
94
-
95
-
96
-
97
-
98
-
99
-
100
-
101
-
102
- function toType ( dependencies, [ structure, value ]) {
103
- let
104
- keys = Object.keys ( value )
105
- , breadcrumbObject = {}
106
- , flatNames = {}
107
- ;
108
- structure.forEach ( ([type, id, ...items]) => { // Create data-structures
109
- items.forEach ( item => {
110
- let
111
- [num,name] = item
112
- , breadcrumbName = name
113
- , checkID = id
114
- ;
115
- for (let i=id; i >= 0; i-- ) {
116
- let [type,x,...items] = structure[i];
117
- items.forEach ( ([n,name]) => {
118
- if ( n === checkID ) {
119
- breadcrumbName = `${name}/${breadcrumbName}`
120
- checkID = x
121
- }
122
- })
123
- }
124
- flatNames[num] = breadcrumbName
125
- })
126
- })
127
- keys.forEach ( key => { // Conect data-structures
128
- let
129
- keyArray = key.split('/')
130
- , breadcrumbKey = keyArray[1]
131
- , breadcrumb = flatNames [ breadcrumbKey ]
132
- ;
133
- if ( breadcrumbKey == '0' ) {
134
- keyArray = keyArray.filter ( (segment,i) => i!=1 )
135
- }
136
- else {
137
- keyArray[1] = breadcrumb
138
- }
139
- breadcrumbObject [ keyArray.join('/') ] = value [key]
140
- })
141
- return breadcrumbObject
142
- } // toType func.
143
-
144
-
145
-
146
- export default { toFlat, toType }
147
-
148
-
@@ -1,12 +0,0 @@
1
- function extractSelection ( data ) {
2
- let selectionKeys = data._select.value;
3
- return selectionKeys.reduce ( (res,key) => {
4
- res[key] = data.value[key]
5
- return res
6
- },{})
7
- } // extractSelection func.
8
-
9
-
10
- export default extractSelection
11
-
12
-
@@ -1,25 +0,0 @@
1
- function filterObject ( id, keySelection, value={} ) {
2
- // *** Filter object with by id
3
- let
4
- local = {} // Filtered object
5
- , localSelection = [] // List of filtered object props
6
- ;
7
- keySelection.forEach ( key => {
8
- let
9
- splited = key.split ( '/' )
10
- , num = splited [ 1 ]
11
- , prop = splited.pop()
12
- ;
13
- if ( num == id ) {
14
- local[prop] = value[key]
15
- localSelection.push ( key )
16
- }
17
- })
18
- return [ local, localSelection ]
19
- } // filterObject
20
-
21
-
22
-
23
- export default filterObject
24
-
25
-
@@ -1,14 +0,0 @@
1
- function hasNumbers ( arr ) {
2
- return !arr.every ( el => {
3
- const r = Number(el);
4
- if ( r === 0 ) return false
5
- if ( r ) return false
6
- return true
7
- })
8
- } // hasNumbers func.
9
-
10
-
11
-
12
- export default hasNumbers
13
-
14
-
package/src/help/index.js DELETED
@@ -1,35 +0,0 @@
1
- 'use strict'
2
-
3
- import isItPrimitive from './isItPrimitive.js'
4
- import hasNumbers from './hasNumbers.js'
5
- import sanitizeFlatKeys from './sanitizeFlatKeys.js'
6
- import objectsByLevel from './objectsByLevel.js'
7
- import filterObject from './filterObject.js'
8
- import toBreadcrumbKeys from './toBreadcrumbsKeys.js'
9
- import updateSelection from './updateSelection.js'
10
- import zipObject from './zipObject.js'
11
- import extractSelection from './extractSelection.js'
12
- import reduceTuples from './reduceTuples.js'
13
- import tuplesToBradcrumbs from './tuplesToBreadcrumbs.js'
14
-
15
- const findType = d => ( d instanceof Array ) ? 'array' : 'object'
16
-
17
-
18
-
19
- export default {
20
- findType // Is it array or object. Returns strings: 'array' | 'object'
21
- , hasNumbers // Check if array contain number members. Returns boolean
22
- , isItPrimitive // Returns boolean
23
- , sanitizeFlatKeys // Sanitize 'file' format keys
24
- , objectsByLevel // Object IDs organized by levels
25
- , filterObject // Find object and object props in a value
26
- , toBreadcrumbKeys // Breadcrumbs keys
27
- , updateSelection // Updates selection list. Checks if element is already selected
28
- , zipObject // Conect two arrays in a single object
29
- , extractSelection // Creates 'value' object with selected keys only
30
- , reduceTuples
31
- , tuplesToBradcrumbs
32
- }
33
-
34
-
35
-
@@ -1,10 +0,0 @@
1
- function isItPrimitive (d) {
2
- if ( typeof d == 'function' ) return true // it's not really primitive but we want function to be treated as primitive
3
- return ( typeof d != 'object' ) ? true : false
4
- } // isItPrimitive func.
5
-
6
-
7
-
8
- export default isItPrimitive
9
-
10
-
@@ -1,16 +0,0 @@
1
- function objectsByLevel ( structure ) {
2
- return structure.reduce ( (res,row) => {
3
- let level = [];
4
- row.forEach ( (item,i) => {
5
- if ( i > 1 ) level.push ( item[0] )
6
- })
7
- if ( level.length > 0 ) res.push ( level )
8
- return res
9
- },[[0]] );
10
- } // objectsByLevel func.
11
-
12
-
13
-
14
- export default objectsByLevel
15
-
16
-
@@ -1,29 +0,0 @@
1
- 'use strict'
2
-
3
- function reduceTuples ( dependencies, breadcrumbData ) {
4
- // *** Converts breadcrumbs to array of tuples
5
- const
6
- { help, walk } = dependencies
7
- , result = []
8
- ;
9
- walk ({
10
- data : breadcrumbData,
11
- keyCallback : ({ value:v, key:k }) => {
12
- let
13
- arr = k.substr(5).split('/')
14
- , ln = arr.length
15
- , lastEl = arr[ln-1]
16
- ;
17
- if ( help.hasNumbers([lastEl]) ) arr.pop ()
18
- result.push ( [ arr.join('/'), v])
19
- return v
20
- }
21
- })
22
- return result
23
- } // reduceTuples func.
24
-
25
-
26
-
27
- export default reduceTuples
28
-
29
-
@@ -1,30 +0,0 @@
1
- function sanitizeFlatKeys ( list ) { // fn(string[]) -> string[]
2
- // *** Sanitize 'file' and 'breadcrumb' data-type keys. Adds 'root/' and indexes where it is needed
3
- let keys = list.map ( x => { // Keys should start with 'root/'
4
- let key = x.split ( '/' )
5
- if ( key[0] != 'root' ) return [ 'root', ...key].join('/')
6
- else return key.join ( '/' )
7
- })
8
- let
9
- usedKeys = []
10
- , duplicatedKeys = new Set()
11
- ;
12
- keys.forEach ( k => {
13
- if ( usedKeys.includes(k) ) duplicatedKeys.add(k)
14
- else usedKeys.push ( k )
15
- })
16
- for (const marker of duplicatedKeys ) {
17
- let counter = 0;
18
- keys = keys.map ( k => {
19
- if ( k == marker ) return `${k}/${counter++}`
20
- else return k
21
- })
22
- }
23
- return keys
24
- } // sanitizeFlatKeys func.
25
-
26
-
27
-
28
- export default sanitizeFlatKeys
29
-
30
-
@@ -1,28 +0,0 @@
1
- function toBreadcrumbKeys ( keys, structure ) { // (flatKey[], structure ) => breadcrumbKeys[]
2
- // *** Map convertion of flat keys to breadcrumb keys.
3
- let containerNames = [ 'root' ];
4
- structure.forEach ( level => {
5
- level.forEach ( (obj,i) => { // Find container names
6
- let
7
- objectID = level[1]
8
- , container = containerNames[objectID]
9
- ;
10
- if ( i > 1 ) containerNames.push ( `${container}/${obj[1]}`)
11
- })
12
- })
13
- return keys.map ( k => {
14
- let
15
- splited = k.split ( '/' )
16
- , containerID = splited [ 1 ]
17
- , prop = splited [ 2 ]
18
- , long = containerNames [ containerID ]
19
- ;
20
- return `${long}/${prop}`
21
- })
22
- } // toBreadcrumbKeys func.
23
-
24
-
25
-
26
- export default toBreadcrumbKeys
27
-
28
-
@@ -1,25 +0,0 @@
1
- 'use strict'
2
-
3
- function tuplesToBreadcrumbs ( dependencies, data ) {
4
- const { walk } = dependencies;
5
- let result = {};
6
-
7
- function objectCalls ({ value:obj, breadcrumbs }) {
8
- if ( breadcrumbs === 'root' ) return obj; // ignore root object
9
- let [ key, val ] = obj;
10
- result[key] = val
11
- return obj
12
- } // objectCalls func.
13
-
14
- walk ({
15
- data,
16
- objectCallback : objectCalls
17
- })
18
- return result
19
- } // tuplesToBradcrumbs func.
20
-
21
-
22
-
23
- export default tuplesToBreadcrumbs
24
-
25
-
@@ -1,12 +0,0 @@
1
- function updateSelection ( [...selection], updates ) {
2
- updates.forEach ( key => {
3
- if ( !selection.includes(key) ) selection.push ( key )
4
- })
5
- return selection
6
- } // updateSelection
7
-
8
-
9
-
10
- export default updateSelection
11
-
12
-
@@ -1,24 +0,0 @@
1
- function zipObject ( keys, values ) { // (string[], string[] ) -> object
2
- // *** Conect two arrays in a single object
3
- return keys.reduce ( (res,k,i) => {
4
- let
5
- income = values[i]
6
- , val
7
- ;
8
- switch ( typeof income ) {
9
- case 'string' :
10
- val = Number(income) || income
11
- break
12
- default:
13
- val = income
14
- }
15
- res[k] = val
16
- return res
17
- }, {} )
18
- } // zipObject func.
19
-
20
-
21
-
22
- export default zipObject
23
-
24
-