@rokkit/core 1.0.0-next.93 → 1.0.0-next.95
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/package.json +6 -6
- package/src/index.js +1 -1
- package/src/mapped-list.js +18 -3
- package/src/mapping.js +27 -11
- package/src/nested.js +2 -2
- package/src/parser.js +4 -4
- package/src/string.js +8 -7
- package/src/theme.js +3 -3
- package/src/types.js +3 -1
- package/src/utils.js +35 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rokkit/core",
|
|
3
|
-
"version": "1.0.0-next.
|
|
3
|
+
"version": "1.0.0-next.95",
|
|
4
4
|
"description": "Core components, actions and stores for svelte apps.",
|
|
5
5
|
"author": "Jerry Thomas <me@jerrythomas.name>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -14,16 +14,16 @@
|
|
|
14
14
|
"devDependencies": {
|
|
15
15
|
"@sveltejs/vite-plugin-svelte": "^3.0.2",
|
|
16
16
|
"@testing-library/svelte": "^4.1.0",
|
|
17
|
-
"@types/ramda": "^0.29.
|
|
17
|
+
"@types/ramda": "^0.29.12",
|
|
18
18
|
"@vitest/coverage-v8": "^1.4.0",
|
|
19
19
|
"@vitest/ui": "~1.4.0",
|
|
20
20
|
"jsdom": "^24.0.0",
|
|
21
21
|
"svelte": "^4.2.12",
|
|
22
|
-
"typescript": "^5.4.
|
|
23
|
-
"vite": "^5.2.
|
|
22
|
+
"typescript": "^5.4.4",
|
|
23
|
+
"vite": "^5.2.8",
|
|
24
24
|
"vitest": "~1.4.0",
|
|
25
|
-
"shared-config": "1.0.0-next.
|
|
26
|
-
"validators": "1.0.0-next.
|
|
25
|
+
"shared-config": "1.0.0-next.95",
|
|
26
|
+
"validators": "1.0.0-next.95"
|
|
27
27
|
},
|
|
28
28
|
"files": [
|
|
29
29
|
"src/**/*.js",
|
package/src/index.js
CHANGED
package/src/mapped-list.js
CHANGED
|
@@ -2,11 +2,25 @@ import { defaultFields } from './constants'
|
|
|
2
2
|
import { isExpanded, hasChildren, getAttribute } from './mapping'
|
|
3
3
|
import { equals } from 'ramda'
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Checks if a specifc attribute of an item matches a value.
|
|
7
|
+
*
|
|
8
|
+
* @param {Object} item - The item.
|
|
9
|
+
* @param {string} attr - The attribute to check.
|
|
10
|
+
*/
|
|
5
11
|
function isMatch(item, attr, value) {
|
|
6
12
|
const itemValue = attr ? getAttribute(item, attr) : item
|
|
7
13
|
return equals(itemValue, value)
|
|
8
14
|
}
|
|
9
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Traverses the tree to find an item by value.
|
|
18
|
+
* @param {Array} items - The items array.
|
|
19
|
+
* @param {Object} fields - The fields mapping.
|
|
20
|
+
* @param {any} value - The value to find.
|
|
21
|
+
* @param {Array} position - The current position in the tree.
|
|
22
|
+
* @returns {Object} The found item, or null if not found.
|
|
23
|
+
*/
|
|
10
24
|
function findInChildren(item, index, fields, value, attr, position) {
|
|
11
25
|
if (hasChildren(item, fields)) {
|
|
12
26
|
return findItemByValue(value, item[fields.children], fields.fields ?? fields, attr, [
|
|
@@ -69,7 +83,7 @@ export function findNearestItemBefore(position, items, fields) {
|
|
|
69
83
|
if (position.length === 0) return { item: items[0], position: [0], fields }
|
|
70
84
|
|
|
71
85
|
let index = position[position.length - 1]
|
|
72
|
-
let result
|
|
86
|
+
let result = null
|
|
73
87
|
if (index > 0) {
|
|
74
88
|
index -= 1
|
|
75
89
|
if (position.length === 1) {
|
|
@@ -85,6 +99,7 @@ export function findNearestItemBefore(position, items, fields) {
|
|
|
85
99
|
}
|
|
86
100
|
|
|
87
101
|
/**
|
|
102
|
+
* Returns the next sibling of the current item.
|
|
88
103
|
*
|
|
89
104
|
* @param {*} parent
|
|
90
105
|
* @param {Array<integer>} position
|
|
@@ -114,8 +129,8 @@ export function findNearestItemAfter(position, items, fields) {
|
|
|
114
129
|
if (items.length === 0) return null
|
|
115
130
|
if (position.length === 0) return { item: items[0], position: [0], fields }
|
|
116
131
|
|
|
117
|
-
|
|
118
|
-
let result
|
|
132
|
+
const current = findItemByIndexArray(position, items, fields)
|
|
133
|
+
let result = null
|
|
119
134
|
if (isExpanded(current.item, current.fields)) {
|
|
120
135
|
result = getFirstChild(current, position)
|
|
121
136
|
} else if (position.length === 1) {
|
package/src/mapping.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { defaultFields } from './constants'
|
|
2
|
-
import { isObject } from '
|
|
2
|
+
import { toString, isObject } from './utils'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Get the component to be used to render the item.
|
|
@@ -10,7 +10,7 @@ import { isObject } from '.'
|
|
|
10
10
|
* @param {import('./types.js').ComponentMap} using
|
|
11
11
|
*/
|
|
12
12
|
export function getComponent(value, fields, using) {
|
|
13
|
-
return fields.component &&
|
|
13
|
+
return fields.component && isObject(value)
|
|
14
14
|
? using[value[fields.component]] ?? using.default
|
|
15
15
|
: using.default
|
|
16
16
|
}
|
|
@@ -47,20 +47,36 @@ export function getValue(node, fields = defaultFields) {
|
|
|
47
47
|
* Get the text for the item. If the text is an object,
|
|
48
48
|
* it will use the field mapping to determine which attribute to get.
|
|
49
49
|
*
|
|
50
|
-
* @param {*}
|
|
50
|
+
* @param {*} node
|
|
51
51
|
* @param {import('./types').FieldMapping} fields
|
|
52
|
-
* @returns {
|
|
52
|
+
* @returns {string}
|
|
53
53
|
*/
|
|
54
54
|
export function getText(node, fields = defaultFields) {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
return value != null
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
55
|
+
const value = isObject(node) ? node[fields.text] : node
|
|
56
|
+
return value
|
|
57
|
+
// return value != null
|
|
58
|
+
// ? isObject(value)
|
|
59
|
+
// ? JSON.stringify(value, null, 2)
|
|
60
|
+
// : value.toString()
|
|
61
|
+
// : value
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
+
/**
|
|
65
|
+
* Get the formatted text for the item. If the text is an object, use the field mapping to determine
|
|
66
|
+
* which attribute to get currency. Use the formatter or identity function to format the text.
|
|
67
|
+
*
|
|
68
|
+
* @param {*} node
|
|
69
|
+
* @param {import('./types').FieldMapping} fields
|
|
70
|
+
* @param {function} formatter
|
|
71
|
+
* @returns {*}
|
|
72
|
+
*/
|
|
73
|
+
export function getFormattedText(node, fields = defaultFields, formatter = toString) {
|
|
74
|
+
const value = isObject(node) ? node[fields.text] : node
|
|
75
|
+
const currency = getAttribute(node, fields.currency)
|
|
76
|
+
const formatValue = typeof formatter === 'function' ? formatter : toString
|
|
77
|
+
|
|
78
|
+
return currency ? formatValue(value, currency) : formatValue(value)
|
|
79
|
+
}
|
|
64
80
|
/**
|
|
65
81
|
* Gets the attribute from the node
|
|
66
82
|
* @param {*} node
|
package/src/nested.js
CHANGED
|
@@ -4,7 +4,7 @@ import { defaultFields } from './constants'
|
|
|
4
4
|
export function flattenNestedList(items, fields = defaultFields, level = 0) {
|
|
5
5
|
fields = { ...defaultFields, ...fields }
|
|
6
6
|
let data = []
|
|
7
|
-
items.
|
|
7
|
+
items.forEach((item) => {
|
|
8
8
|
const children = item[fields.children] ?? []
|
|
9
9
|
data = [
|
|
10
10
|
...data,
|
|
@@ -31,7 +31,7 @@ export function findValueFromPath(slug, data, fields) {
|
|
|
31
31
|
let items = data
|
|
32
32
|
let value = null
|
|
33
33
|
|
|
34
|
-
keys.
|
|
34
|
+
keys.forEach((key, index) => {
|
|
35
35
|
const match = items.find((item) => item[fields.key] === key)
|
|
36
36
|
if (match) {
|
|
37
37
|
if (index < keys.length - 1) {
|
package/src/parser.js
CHANGED
|
@@ -19,11 +19,11 @@
|
|
|
19
19
|
* @returns {RegExp} - The regular expression pattern to identify search filter elements.
|
|
20
20
|
*/
|
|
21
21
|
export function getRegex() {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
const column = '[\\w]+'
|
|
23
|
+
const operator = ':|>|<|>=|<=|=<|=>|=|!=|~|~\\*|!~|!~\\*'
|
|
24
|
+
const value = '("[^"]+"|[^\\s=:<>!~*]+)'
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
const pattern = `(?<group>((?<column>${column})\\s?(?<operator>${operator})\\s?)(?<value>${value}))`
|
|
27
27
|
|
|
28
28
|
return new RegExp(pattern, 'gm')
|
|
29
29
|
}
|
package/src/string.js
CHANGED
|
@@ -25,8 +25,8 @@ export function toPascalCase(text) {
|
|
|
25
25
|
/**
|
|
26
26
|
* Convert a PascalCase string to snake case with separator as hyphen
|
|
27
27
|
*
|
|
28
|
-
* @param {
|
|
29
|
-
* @returns
|
|
28
|
+
* @param {string} text
|
|
29
|
+
* @returns {string}
|
|
30
30
|
*/
|
|
31
31
|
export function toHyphenCase(text) {
|
|
32
32
|
return text
|
|
@@ -40,11 +40,12 @@ export function toHyphenCase(text) {
|
|
|
40
40
|
*
|
|
41
41
|
* @param {String} a hyphen separates string
|
|
42
42
|
* @param {String} b hyphen separates string
|
|
43
|
-
* @
|
|
43
|
+
* @param {string} separator - separator to split the string
|
|
44
|
+
* @returns {Number} -1, 0, 1 based on comparison
|
|
44
45
|
*/
|
|
45
|
-
export function sortByParts(a, b) {
|
|
46
|
-
const partsOfA = a.split(
|
|
47
|
-
const partsOfB = b.split(
|
|
46
|
+
export function sortByParts(a, b, separator = '-') {
|
|
47
|
+
const partsOfA = a.split(separator)
|
|
48
|
+
const partsOfB = b.split(separator)
|
|
48
49
|
|
|
49
50
|
let result = compareStrings(partsOfA[0], partsOfB[0])
|
|
50
51
|
if (result === 0) result = partsOfA.length - partsOfB.length
|
|
@@ -69,7 +70,7 @@ export function compareStrings(a, b) {
|
|
|
69
70
|
* @returns {String} timestamp based unique id
|
|
70
71
|
*/
|
|
71
72
|
export function uniqueId(prefix = '', separator = '-') {
|
|
72
|
-
|
|
73
|
+
const pair = prefix && prefix.length > 0 ? [prefix] : []
|
|
73
74
|
pair.push(Date.now().toString(36))
|
|
74
75
|
return pair.join(separator)
|
|
75
76
|
}
|
package/src/theme.js
CHANGED
|
@@ -47,8 +47,8 @@ export function stateColors(name, modifier = 'none') {
|
|
|
47
47
|
export function themeColors(modifier = 'none') {
|
|
48
48
|
const fn = modifier in modifiers ? modifiers[modifier] : modifiers.none
|
|
49
49
|
|
|
50
|
-
|
|
51
|
-
|
|
50
|
+
const states = ['info', 'danger', 'warning', 'success', 'error']
|
|
51
|
+
const variants = ['neutral', 'primary', 'secondary', 'accent']
|
|
52
52
|
let colors = states.reduce(
|
|
53
53
|
(acc, state) => ({ ...acc, [state]: stateColors(state, modifier) }),
|
|
54
54
|
{}
|
|
@@ -82,7 +82,7 @@ function createShadeMappings(variant, mode, valueCondition) {
|
|
|
82
82
|
return shades.map((shade) => ({
|
|
83
83
|
key: `--on-${variant}-${shade}`,
|
|
84
84
|
value: valueCondition(shade),
|
|
85
|
-
mode
|
|
85
|
+
mode
|
|
86
86
|
}))
|
|
87
87
|
}
|
|
88
88
|
|
package/src/types.js
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
|
-
* @typedef {checked|unchecked|indeterminate} SelectionState
|
|
13
|
+
* @typedef {'checked'|'unchecked'|'indeterminate'} SelectionState
|
|
14
14
|
*/
|
|
15
15
|
|
|
16
16
|
/**
|
|
@@ -85,3 +85,5 @@
|
|
|
85
85
|
* @typedef {Object} RowStateMap
|
|
86
86
|
* @property {RowState[]} rows - Flat list of hierarchy nodes.
|
|
87
87
|
*/
|
|
88
|
+
|
|
89
|
+
export default {}
|
package/src/utils.js
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A function that performs no operations.
|
|
3
|
+
*/
|
|
4
|
+
export function noop() {
|
|
5
|
+
// intentionally empty to support default actions
|
|
6
|
+
}
|
|
7
|
+
|
|
1
8
|
/**
|
|
2
9
|
* Generates a random id
|
|
3
10
|
*
|
|
@@ -17,6 +24,26 @@ export function isObject(val) {
|
|
|
17
24
|
return typeof val === 'object' && val !== null && !(val instanceof Date)
|
|
18
25
|
}
|
|
19
26
|
|
|
27
|
+
/**
|
|
28
|
+
* Converts the value to a string. If the value is an object, it will convert it to a JSON string.
|
|
29
|
+
*
|
|
30
|
+
* @param {*} value
|
|
31
|
+
* @returns {string}
|
|
32
|
+
*/
|
|
33
|
+
export function toString(value) {
|
|
34
|
+
if (value === null || value === undefined) return value
|
|
35
|
+
if (isObject(value)) return JSON.stringify(value, null, 2)
|
|
36
|
+
return value.toString()
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Generates icon shortcuts for a collection of icons
|
|
41
|
+
*
|
|
42
|
+
* @param {string[]} icons
|
|
43
|
+
* @param {string} collection
|
|
44
|
+
* @param {string} variants
|
|
45
|
+
* @returns {Object}
|
|
46
|
+
*/
|
|
20
47
|
export function iconShortcuts(icons, collection, variants) {
|
|
21
48
|
const suffix = variants ? `-${variants}` : ''
|
|
22
49
|
const shortcuts = !collection
|
|
@@ -32,7 +59,14 @@ export function iconShortcuts(icons, collection, variants) {
|
|
|
32
59
|
return shortcuts
|
|
33
60
|
}
|
|
34
61
|
|
|
62
|
+
/**
|
|
63
|
+
* Scales the path by the size
|
|
64
|
+
*
|
|
65
|
+
* @param {number} size
|
|
66
|
+
* @param {string|number} x
|
|
67
|
+
* @returns {string|number}
|
|
68
|
+
*/
|
|
35
69
|
export function scaledPath(size, x) {
|
|
36
|
-
if (Array.isArray(x)) return x.map((
|
|
70
|
+
if (Array.isArray(x)) return x.map((v) => scaledPath(size, v)).join(' ')
|
|
37
71
|
return typeof x === 'number' ? x * size : x
|
|
38
72
|
}
|