@rokkit/data 1.0.0-next.98 → 1.0.1

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/src/rollup.js CHANGED
@@ -1,27 +1,3 @@
1
- import { omit, pick, mergeLeft, map, difference, uniq, pipe } from 'ramda'
2
-
3
- /**
4
- * Groups data by specified keys.
5
- *
6
- * @param {Array} data - An array of data points.
7
- * @param {Array} groupByKeys - Keys to group the data by.
8
- * @param {Array} summaries - An array of summary objects to define aggregation operations.
9
- * @returns {Object} Grouped data object with aggregation placeholders.
10
- */
11
- export function groupDataByKeys(data, groupByKeys, summaries) {
12
- const keysExtractor = pick(groupByKeys)
13
- const groupedData = data.reduce((grouped, row) => {
14
- const key = JSON.stringify(keysExtractor(row))
15
- if (!grouped[key]) {
16
- grouped[key] = { ...keysExtractor(row), ...initialValues(summaries) }
17
- }
18
- addToSummaries(grouped[key], row, summaries)
19
- return grouped
20
- }, {})
21
-
22
- return Object.values(groupedData)
23
- }
24
-
25
1
  /**
26
2
  * Sets initial values for summaries.
27
3
  *
@@ -49,6 +25,33 @@ export function addToSummaries(group, row, summaries) {
49
25
  })
50
26
  }
51
27
 
28
+ /**
29
+ * Groups data by specified keys using a composite string key (Phase 3: replaces JSON.stringify).
30
+ *
31
+ * @param {Array} data - An array of data points.
32
+ * @param {Array} groupByKeys - Keys to group the data by.
33
+ * @param {Array} summaries - An array of summary objects to define aggregation operations.
34
+ * @returns {Object} Grouped data object with aggregation placeholders.
35
+ */
36
+ export function groupDataByKeys(data, groupByKeys, summaries) {
37
+ const groupedData = new Map()
38
+
39
+ for (const row of data) {
40
+ // Composite key: avoids JSON.stringify overhead
41
+ const key = groupByKeys.map((k) => row[k]).join('\x00')
42
+
43
+ if (!groupedData.has(key)) {
44
+ const group = {}
45
+ for (const k of groupByKeys) group[k] = row[k]
46
+ Object.assign(group, initialValues(summaries))
47
+ groupedData.set(key, group)
48
+ }
49
+ addToSummaries(groupedData.get(key), row, summaries)
50
+ }
51
+
52
+ return [...groupedData.values()]
53
+ }
54
+
52
55
  /**
53
56
  * Fills grouped data with aligned data if specified in configuration.
54
57
  *
@@ -57,9 +60,8 @@ export function addToSummaries(group, row, summaries) {
57
60
  * @returns {Array} Aligned data array.
58
61
  */
59
62
  export function fillAlignedData(groupedData, config, fillRowsFunc) {
60
- // const fillRowsFunc = getAlignGenerator(df.data, config)
61
63
  const { actual_flag, children } = config
62
- return Object.values(groupedData).map((row) => ({
64
+ return groupedData.map((row) => ({
63
65
  ...row,
64
66
  [children]: [
65
67
  ...row[children].map((value) => ({ ...value, [actual_flag]: 1 })),
@@ -68,6 +70,49 @@ export function fillAlignedData(groupedData, config, fillRowsFunc) {
68
70
  }))
69
71
  }
70
72
 
73
+ /**
74
+ * Creates a generator that produces missing rows in a dataset based on specified columns.
75
+ *
76
+ * @param {Array<Object>} data - The array of objects representing the dataset.
77
+ * @param {Object} config - The configuration object.
78
+ * @returns {Function} A generator function that when called, produces the missing rows.
79
+ */
80
+ function buildAlignTemplate(config) {
81
+ const { align_by, group_by, actual_flag } = config
82
+ const omitKeys = new Set([...align_by, ...group_by])
83
+ const template = { [actual_flag]: 0 }
84
+ for (const [k, v] of Object.entries(config.template)) {
85
+ if (!omitKeys.has(k)) template[k] = v
86
+ }
87
+ return template
88
+ }
89
+
90
+ function collectAllCombos(data, align_by) {
91
+ const seen = new Map()
92
+ for (const row of data) {
93
+ const key = align_by.map((k) => row[k]).join('\x00')
94
+ if (!seen.has(key)) {
95
+ const combo = {}
96
+ for (const k of align_by) combo[k] = row[k]
97
+ seen.set(key, combo)
98
+ }
99
+ }
100
+ return [...seen.values()]
101
+ }
102
+
103
+ export function getAlignGenerator(data, config) {
104
+ const { align_by } = config
105
+ const template = buildAlignTemplate(config)
106
+ const allCombos = collectAllCombos(data, align_by)
107
+
108
+ return (children) => {
109
+ const childKeys = new Set(children.map((c) => align_by.map((k) => c[k]).join('\x00')))
110
+ return allCombos
111
+ .filter((combo) => !childKeys.has(align_by.map((k) => combo[k]).join('\x00')))
112
+ .map((combo) => ({ ...combo, ...template }))
113
+ }
114
+ }
115
+
71
116
  /**
72
117
  * Performs aggregation operations on grouped data.
73
118
  *
@@ -88,51 +133,3 @@ export function aggregateData(dataArray, summaries) {
88
133
  )
89
134
  }))
90
135
  }
91
-
92
- /**
93
- * Creates a generator that produces missing rows in a dataset based on specified columns.
94
- * The function determines all unique combinations of the specified columns in config.align_by
95
- * and returns a generator function to create the missing combinations, potentially with default
96
- * values for other columns.
97
- * @param {Array<Object>} data - The array of objects representing the dataset.
98
- * @param {Object} config - The configuration object.
99
- *
100
- * @returns {Function} A generator function that when called, produces the missing rows.
101
- */
102
- export function getAlignGenerator(data, config) {
103
- const { align_by, group_by, actual_flag } = config
104
- const template = { ...omit([...align_by, ...group_by], config.template), [actual_flag]: 0 }
105
- const subset = pipe(map(pick(align_by)), uniq)(data)
106
-
107
- return pipe(map(pick(align_by)), uniq, difference(subset), map(mergeLeft(template)))
108
- }
109
-
110
- /**
111
- * Returns an aggregator object with a mapper and reducer function.
112
- *
113
- * @param {Array<string>} from - The key or keys to aggregate.
114
- * @param {Function} using - The aggregation function.
115
- * @returns {import('./types').SummaryConfig} - An object with a mapper and reducer function.
116
- */
117
- // export function getAggregator(from, into, using = identity) {
118
- // // const mapper = pick(Array.isArray ? from : [from])
119
- // return {
120
- // mapper: pick(from),
121
- // reducers: [{ field: into, formula: using }]
122
- // }
123
- // }
124
-
125
- /**
126
- * Returns the default aggregator which rolls up the columns other than the group_by columns into a single object.
127
- *
128
- * @param {import('./types').Metadata} metadata - The metadata for the columns to be aggregated.
129
- * @param {Object} config - The configuration used to build the aggregator.
130
- *
131
- * @returns {Object} An object containing the default aggregator for the specified metadata and configuration.
132
- */
133
- // export function defaultAggregator(metadata, config) {
134
- // const child = metadata.filter((col) => !config.group_by.includes(col.name))
135
- // const keys = child.map((col) => col.name)
136
-
137
- // return { ...getAggregator(keys, config.children), metadata: child }
138
- // }
package/src/view.js CHANGED
@@ -1,4 +1,3 @@
1
- import { omit } from 'ramda'
2
1
  import { get, writable } from 'svelte/store'
3
2
  import { deriveMetadata, deriveSortableColumn } from './infer'
4
3
 
@@ -162,7 +161,7 @@ export function dataview(data, options) {
162
161
  let sortGroup = []
163
162
 
164
163
  return {
165
- ...omit(['set', 'update'], store),
164
+ ...Object.fromEntries(Object.entries(store).filter(([k]) => k !== 'set' && k !== 'update')),
166
165
  // filter: noop,
167
166
  /**
168
167
  * Clears the applied sort order from the hierarchy.
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2022 Jerry Thomas
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.