@rokkit/core 1.0.0-next.106 → 1.0.0-next.108

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.
@@ -1,5 +1,5 @@
1
1
  export class FieldMapper {
2
- constructor(fields?: any, componentMap?: {});
2
+ constructor(fields?: any);
3
3
  hasIcon: ((obj: unknown) => obj is {
4
4
  [x: string]: unknown;
5
5
  }) | undefined;
@@ -23,24 +23,27 @@ export class FieldMapper {
23
23
  }) | undefined;
24
24
  withPrefix: ((x: any) => string) | undefined;
25
25
  excludeFlags: (<U extends Partial<Record<any, any>>>(obj: any extends keyof U ? U : never) => any extends keyof U ? Omit<U, any> : never) | undefined;
26
+ getChildMapper(): null;
27
+ /**
28
+ * @private
29
+ */
30
+ private prop;
31
+ /**
32
+ * Gets a mapped attribute from the original item
33
+ *
34
+ * @param {string} fieldName - Name of the field to get
35
+ * @returns {any|null} - The attribute value or null if not found
36
+ */
37
+ get(fieldName: string, value: any, defaultValue?: null): any | null;
26
38
  set fields(fields: any);
27
39
  get fields(): any;
28
- set componentMap(components: {});
29
- get componentMap(): {};
30
- getComponent(value: any): any;
31
40
  getIcon(value: any): string | null;
32
- getImage(value: any): any;
33
41
  getValue(value: any): any;
34
- getText(value: any): any;
35
- getLabel(value: any): any;
36
- getAttribute(value: any, attr: any): any;
37
42
  getFormattedText(value: any, formatter: any): any;
38
43
  hasChildren(item: any): item is never;
39
- isExpanded(item: any): false;
40
44
  isHidden(item: any): unknown;
41
45
  isNested(items: any): boolean;
42
46
  toggleVisibility(items: any, visible: any): void;
43
- toggleExpansion(item: any): void;
44
47
  getChildren(item: any): never[];
45
48
  /**
46
49
  * Finds children by an index path
package/dist/index.d.ts CHANGED
@@ -10,3 +10,4 @@ export { getLineTypes } from "./connector.js";
10
10
  export { generateTicks } from "./ticks.js";
11
11
  export { getItemAtIndex, getIndexForItem } from "./mapped-items.js";
12
12
  export { weekdays, getCalendarDays } from "./calendar.js";
13
+ export { getValue, getNestedFields, hasChildren, isExpanded } from "./mapping.js";
package/dist/mapping.d.ts CHANGED
@@ -66,3 +66,10 @@ export function hasChildren(item: any, fields: import("./types").FieldMapping):
66
66
  * @returns {boolean}
67
67
  */
68
68
  export function isExpanded(item: any, fields: import("./types").FieldMapping): boolean;
69
+ /**
70
+ * Fetches the fieldmapping for a child node
71
+ *
72
+ * @param {import('./types').FieldMapping} fields
73
+ * @returns {import('./types').FieldMapping}
74
+ */
75
+ export function getNestedFields(fields: import("./types").FieldMapping): import("./types").FieldMapping;
package/dist/utils.d.ts CHANGED
@@ -59,3 +59,11 @@ export function getKeyFromPath(path: string[]): string;
59
59
  * @returns {string[]}
60
60
  */
61
61
  export function getPathFromKey(key: string): string[];
62
+ /**
63
+ * Get snippet function from an object
64
+ * @param {Object} obj
65
+ * @param {string} key
66
+ * @param {null|Function} defaultSnippet
67
+ * @returns {Function|undefined}
68
+ */
69
+ export function getSnippet(obj: Object, key: string, defaultSnippet?: null | Function): Function | undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rokkit/core",
3
- "version": "1.0.0-next.106",
3
+ "version": "1.0.0-next.108",
4
4
  "description": "Contains core utility functions and classes that can be used in various components.",
5
5
  "author": "Jerry Thomas <me@jerrythomas.name>",
6
6
  "license": "MIT",
package/src/constants.js CHANGED
@@ -12,7 +12,6 @@ export const defaultFields = {
12
12
  icon: 'icon',
13
13
  iconPrefix: null,
14
14
  image: 'image',
15
- component: 'component',
16
15
  summary: 'summary',
17
16
  notes: 'notes',
18
17
  props: 'props',
@@ -22,12 +21,12 @@ export const defaultFields = {
22
21
  parent: 'parent',
23
22
  currency: 'currency',
24
23
  label: 'label',
24
+ component: 'component',
25
+ snippet: '_snippet',
25
26
  /* flag fields */
26
- isSelected: '_selected',
27
- isHidden: '_hidden',
28
- isOpen: '_open',
29
- isDeleted: '_deleted',
30
- isFiltered: '_filtered'
27
+ deleted: '_deleted',
28
+ expanded: '_expanded',
29
+ selected: '_selected'
31
30
  }
32
31
 
33
32
  export const defaultIcons = [
@@ -1,14 +1,13 @@
1
- import { defaultFields } from './constants.js'
2
1
  import { isNil, has, omit } from 'ramda'
2
+ import { defaultFields } from './constants.js'
3
3
  import { isObject } from './utils.js'
4
4
 
5
5
  export class FieldMapper {
6
6
  #fields = { ...defaultFields }
7
- #componentMap = {}
7
+ #childMapper = null
8
8
 
9
- constructor(fields = defaultFields, componentMap = {}) {
9
+ constructor(fields = defaultFields) {
10
10
  this.#updateFields(fields)
11
- this.#updateComponentMap(componentMap)
12
11
  }
13
12
 
14
13
  #updateFields(fields) {
@@ -32,34 +31,41 @@ export class FieldMapper {
32
31
  ])
33
32
  }
34
33
 
35
- #updateComponentMap(components) {
36
- if (typeof components === 'object' && components) {
37
- Object.keys(components).forEach((key) => {
38
- this.#componentMap[key] = components[key]
39
- })
34
+ getChildMapper() {
35
+ if (!this.#childMapper) {
36
+ this.#childMapper = new FieldMapper(this.fields.fields ?? this.fields)
40
37
  }
38
+ return this.#childMapper
41
39
  }
40
+ /**
41
+ * @private
42
+ */
43
+ prop(fieldName, value) {
44
+ if (isNil(value)) return null
42
45
 
43
- get fields() {
44
- return this.#fields
45
- }
46
+ if (typeof value === 'object') {
47
+ return value[this.fields[fieldName]]
48
+ }
46
49
 
47
- set fields(fields) {
48
- this.#updateFields(fields)
50
+ return fieldName === 'text' ? value : null
49
51
  }
50
52
 
51
- get componentMap() {
52
- return this.#componentMap
53
+ /**
54
+ * Gets a mapped attribute from the original item
55
+ *
56
+ * @param {string} fieldName - Name of the field to get
57
+ * @returns {any|null} - The attribute value or null if not found
58
+ */
59
+ get(fieldName, value, defaultValue = null) {
60
+ return this.prop(fieldName, value) ?? defaultValue
53
61
  }
54
62
 
55
- set componentMap(components) {
56
- this.#updateComponentMap(components)
63
+ get fields() {
64
+ return this.#fields
57
65
  }
58
66
 
59
- getComponent(value) {
60
- if (this.hasComponent(value))
61
- return this.componentMap[value[this.fields.component]] ?? this.componentMap.default
62
- return this.componentMap.default
67
+ set fields(fields) {
68
+ this.#updateFields(fields)
63
69
  }
64
70
 
65
71
  getIcon(value) {
@@ -68,9 +74,6 @@ export class FieldMapper {
68
74
  if (isObject(icon)) return this.withPrefix(icon[value[this.fields.state]])
69
75
  return this.withPrefix(icon)
70
76
  }
71
- getImage(value) {
72
- return this.getAttribute(value, 'image')
73
- }
74
77
 
75
78
  getValue(value) {
76
79
  if (this.hasValue(value)) {
@@ -79,32 +82,15 @@ export class FieldMapper {
79
82
  return value
80
83
  }
81
84
 
82
- getText(value) {
83
- if (this.hasText(value)) {
84
- return value[this.fields.text]
85
- }
86
- return typeof value === 'object' ? null : value
87
- }
88
-
89
- getLabel(value) {
90
- return this.getAttribute(value, 'label') ?? this.getText(value)
91
- }
92
-
93
- getAttribute(value, attr) {
94
- if (has(attr, this.fields)) {
95
- return has(this.fields[attr], value) ? value[this.fields[attr]] : null
96
- }
97
- return null
98
- }
99
-
100
85
  getFormattedText(value, formatter) {
101
- const text = this.getText(value)
86
+ const text = this.get('text', value)
87
+
102
88
  if (isNil(text)) return ''
103
89
 
104
90
  if (typeof formatter !== 'function') return text.toString()
105
91
 
106
92
  if (this.hasCurrency(value)) {
107
- return formatter(text, this.getAttribute(value, 'currency'))
93
+ return formatter(text, this.get('currency', value))
108
94
  }
109
95
  return formatter(text)
110
96
  }
@@ -118,13 +104,6 @@ export class FieldMapper {
118
104
  )
119
105
  }
120
106
 
121
- isExpanded(item) {
122
- if (this.hasChildren(item)) {
123
- return has(this.fields.isOpen, item) && item[this.fields.isOpen]
124
- }
125
- return false
126
- }
127
-
128
107
  isHidden(item) {
129
108
  return has(this.fields.isHidden, item) && item[this.fields.isHidden]
130
109
  }
@@ -142,13 +121,6 @@ export class FieldMapper {
142
121
  })
143
122
  }
144
123
 
145
- toggleExpansion(item) {
146
- if (this.hasChildren(item)) {
147
- item[this.fields.isOpen] = !item[this.fields.isOpen]
148
- this.toggleVisibility(item[this.fields.children], item[this.fields.isOpen])
149
- }
150
- }
151
-
152
124
  getChildren(item) {
153
125
  return this.hasChildren(item) ? item[this.fields.children] : []
154
126
  }
package/src/index.js CHANGED
@@ -16,3 +16,4 @@ export { createEmitter } from './events.js'
16
16
  export { getLineTypes } from './connector.js'
17
17
  export { weekdays, getCalendarDays } from './calendar.js'
18
18
  export { generateTicks } from './ticks.js'
19
+ export { getValue, getNestedFields, hasChildren, isExpanded } from './mapping.js'
package/src/mapping.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { defaultFields } from './constants.js'
2
2
  import { toString, isObject } from './utils.js'
3
-
3
+ import { has } from 'ramda'
4
4
  /**
5
5
  * Get the component to be used to render the item.
6
6
  * If the component is null or undefined, it will return the default component.
@@ -104,12 +104,7 @@ export function getFormattedText(node, fields = defaultFields, formatter = toStr
104
104
  * @returns {boolean}
105
105
  */
106
106
  export function hasChildren(item, fields) {
107
- return (
108
- item !== null &&
109
- typeof item === 'object' &&
110
- fields.children in item &&
111
- Array.isArray(item[fields.children])
112
- )
107
+ return has(fields.children, item) && Array.isArray(item[fields.children])
113
108
  }
114
109
 
115
110
  /**
@@ -122,8 +117,18 @@ export function hasChildren(item, fields) {
122
117
  export function isExpanded(item, fields) {
123
118
  if (item === null) return false
124
119
  if (!hasChildren(item, fields)) return false
125
- if (fields.isOpen in item) {
126
- return item[fields.isOpen]
120
+ if (has(fields.expanded, item)) {
121
+ return item[fields.expanded]
127
122
  }
128
123
  return false
129
124
  }
125
+
126
+ /**
127
+ * Fetches the fieldmapping for a child node
128
+ *
129
+ * @param {import('./types').FieldMapping} fields
130
+ * @returns {import('./types').FieldMapping}
131
+ */
132
+ export function getNestedFields(fields) {
133
+ return { ...defaultFields, ...(fields.fields ?? fields) }
134
+ }
package/src/utils.js CHANGED
@@ -1,3 +1,5 @@
1
+ import { has, isNil } from 'ramda'
2
+
1
3
  /**
2
4
  * Finds the closest ancestor of the given element that has the given attribute.
3
5
  *
@@ -34,7 +36,7 @@ export function id() {
34
36
  * @returns {boolean}
35
37
  */
36
38
  export function isObject(val) {
37
- return typeof val === 'object' && val !== null && !(val instanceof Date)
39
+ return typeof val === 'object' && !isNil(val) && !(val instanceof Date)
38
40
  }
39
41
 
40
42
  /**
@@ -90,7 +92,7 @@ export function scaledPath(size, x) {
90
92
  * @returns {string}
91
93
  */
92
94
  export function getKeyFromPath(path) {
93
- return path.join('-')
95
+ return Array.isArray(path) ? path.join('-') : [path].join('-')
94
96
  }
95
97
 
96
98
  /**
@@ -101,3 +103,17 @@ export function getKeyFromPath(path) {
101
103
  export function getPathFromKey(key) {
102
104
  return key.split('-').map(Number)
103
105
  }
106
+
107
+ /**
108
+ * Get snippet function from an object
109
+ * @param {Object} obj
110
+ * @param {string} key
111
+ * @param {null|Function} defaultSnippet
112
+ * @returns {Function|undefined}
113
+ */
114
+ export function getSnippet(obj, key, defaultSnippet = null) {
115
+ if (has(key, obj) && typeof obj[key] === 'function') {
116
+ return obj[key]
117
+ }
118
+ return defaultSnippet
119
+ }