@rokkit/core 1.0.0-next.35 → 1.0.0-next.37

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rokkit/core",
3
- "version": "1.0.0-next.35",
3
+ "version": "1.0.0-next.37",
4
4
  "description": "Core components, actions and stores for svelte apps.",
5
5
  "author": "Jerry Thomas <me@jerrythomas.name>",
6
6
  "license": "MIT",
@@ -13,18 +13,19 @@
13
13
  "access": "public"
14
14
  },
15
15
  "devDependencies": {
16
- "@sveltejs/vite-plugin-svelte": "^2.4.2",
16
+ "@sveltejs/vite-plugin-svelte": "^2.4.3",
17
17
  "@testing-library/svelte": "^4.0.3",
18
- "@vitest/coverage-c8": "^0.32.3",
19
- "@vitest/coverage-istanbul": "^0.32.3",
20
- "@vitest/ui": "~0.32.3",
18
+ "@vitest/coverage-c8": "^0.33.0",
19
+ "@vitest/coverage-istanbul": "^0.33.0",
20
+ "@vitest/coverage-v8": "^0.33.0",
21
+ "@vitest/ui": "~0.33.0",
21
22
  "jsdom": "^22.1.0",
22
- "svelte": "^4.0.1",
23
+ "svelte": "^4.1.1",
23
24
  "typescript": "^5.1.6",
24
25
  "validators": "latest",
25
- "vite": "^4.3.9",
26
- "vitest": "~0.32.3",
27
- "shared-config": "1.0.0-next.35"
26
+ "vite": "^4.4.7",
27
+ "vitest": "~0.33.0",
28
+ "shared-config": "1.0.0-next.37"
28
29
  },
29
30
  "files": [
30
31
  "src/**/*.js",
package/src/constants.js CHANGED
@@ -1,3 +1,26 @@
1
+ /**
2
+ * @type {import('./types).FieldMapping} Fields
3
+ */
4
+ export const defaultFields = {
5
+ id: 'id',
6
+ url: 'url',
7
+ value: 'value',
8
+ text: 'text',
9
+ children: 'children',
10
+ icon: 'icon',
11
+ image: 'image',
12
+ component: 'component',
13
+ summary: 'summary',
14
+ notes: 'notes',
15
+ props: 'props',
16
+ target: 'target',
17
+ state: 'state',
18
+ isOpen: '_open',
19
+ isDeleted: '_deleted',
20
+ level: 'level',
21
+ parent: 'parent'
22
+ }
23
+
1
24
  export const defaultIcons = [
2
25
  'accordion-opened',
3
26
  'accordion-closed',
@@ -23,7 +46,16 @@ export const defaultIcons = [
23
46
  'navigate-left',
24
47
  'navigate-right',
25
48
  'navigate-up',
26
- 'navigate-down'
49
+ 'navigate-down',
50
+ 'state-error',
51
+ 'state-warning',
52
+ 'state-success',
53
+ 'state-info',
54
+ 'state-unknown',
55
+ 'validity-failed',
56
+ 'validity-warning',
57
+ 'validity-passed',
58
+ 'validity-unknown'
27
59
  ]
28
60
 
29
61
  export const defaultOptions = {
@@ -33,44 +65,6 @@ export const defaultOptions = {
33
65
  checked: 'checked'
34
66
  }
35
67
 
36
- /**
37
- * Structure to map custom fields for rendering
38
- *
39
- * @typedef FieldMapping
40
- * @property {string} [id='id'] - Unique id for the item
41
- * @property {string} [text='text'] - Attribute to identify the text to render
42
- * @property {string} [url='url'] - Attribute to identify a URL
43
- * @property {string} [icon='icon'] - Attribute to identify an icon class to render
44
- * @property {string} [image='image'] - Attribute to identify an image to render
45
- * @property {string} [children='children'] - Attribute to identify children of the current item
46
- * @property {string} [summary='summary']
47
- * @property {string} [notes='notes']
48
- * @property {string} [props='props']
49
- * @property {string} [isOpen='_open'] - Attribute to identify if the current item is open
50
- * @property {string} [level='level'] - Attribute to identify level of current item
51
- * @property {string} [parent='parent'] - Attribute to identify if the current item is a parent
52
- * @property {string} [isDeleted='_deleted'] - Attribute to identify if the current item is deleted
53
- * @property {FieldMapping} [fields?] - Field mapping to be used on children in the next level
54
- */
55
- export const defaultFields = {
56
- id: 'id',
57
- url: 'url',
58
- text: 'text',
59
- children: 'children',
60
- icon: 'icon',
61
- image: 'image',
62
- component: 'component',
63
- summary: 'summary',
64
- notes: 'notes',
65
- props: 'props',
66
- target: 'target',
67
- state: 'state',
68
- isOpen: '_open',
69
- isDeleted: '_deleted',
70
- level: 'level',
71
- parent: 'parent'
72
- }
73
-
74
68
  export const defaultKeyMap = {
75
69
  ArrowRight: 'open',
76
70
  ArrowLeft: 'close',
package/src/hacks.js ADDED
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Function to change the viewport height on mobile browsers to account for the address bar
3
+ *
4
+ * @param {boolean} [browser=false] indicates whether the device is a browser
5
+ * @param {boolean} [small=false] indicates whether the device is a small mobile device
6
+ */
7
+ export function adjustViewport(browser = false, small = false) {
8
+ if (browser) {
9
+ const viewportHeight =
10
+ window.innerHeight != window.outerHeight && small
11
+ ? window.innerHeight + 'px'
12
+ : '100vh'
13
+ document.body.style.visibility = 'hidden'
14
+ document.body.style.setProperty('--viewport-height', viewportHeight)
15
+ document.body.style.visibility = 'visible'
16
+ }
17
+ }
package/src/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ import './types'
1
2
  export * from './constants'
2
3
  export * from './nested'
3
4
  export * from './mapping'
@@ -5,3 +6,6 @@ export * from './connector'
5
6
  export * from './ticks'
6
7
  export * from './calendar'
7
8
  export * from './utils'
9
+ export * from './parser'
10
+ export * from './string'
11
+ export * from './hacks'
package/src/mapping.js CHANGED
@@ -1,11 +1,25 @@
1
1
  import { defaultFields } from './constants'
2
2
 
3
+ /**
4
+ * Get the component to be used to render the item.
5
+ * If the component is null or undefined, it will return the default component.
6
+ *
7
+ * @param {object|string} value
8
+ * @param {import('./types.js').FieldMapping} fields
9
+ * @param {import('./types.js').ComponentMap} using
10
+ */
3
11
  export function getComponent(value, fields, using) {
4
12
  return fields.component && typeof value == 'object'
5
13
  ? using[value[fields.component]] ?? using.default
6
14
  : using.default
7
15
  }
8
16
 
17
+ /**
18
+ * Get the icon for the item. If the icon is an object, it will use the state to determine which icon to use.
19
+ *
20
+ * @param {object|string} value
21
+ * @param {import('./types.js').FieldMapping} fields
22
+ */
9
23
  export function getIcon(value, fields = defaultFields) {
10
24
  if (fields.icon === undefined || typeof (value ?? '') !== 'object')
11
25
  return null
@@ -14,3 +28,13 @@ export function getIcon(value, fields = defaultFields) {
14
28
  ? value[fields.icon][value[fields.state]]
15
29
  : value[fields.icon]
16
30
  }
31
+
32
+ export function getValue(node, fields = defaultFields) {
33
+ return typeof node === 'object' && node !== null
34
+ ? node[fields.value] ?? node[fields.text]
35
+ : node
36
+ }
37
+
38
+ export function getText(node, fields = defaultFields) {
39
+ return typeof node === 'object' && node !== null ? node[fields.text] : node
40
+ }
package/src/parser.js ADDED
@@ -0,0 +1,53 @@
1
+ export function getRegex() {
2
+ let column = '[\\w]+'
3
+ let operator = ':|>|<|>=|<=|=<|=>|=|!=|~|~\\*|!~|!~\\*'
4
+ let value = '("[^"]+"|[^\\s=:<>!~*]+)'
5
+
6
+ let pattern = `(?<group>((?<column>${column})\\s?(?<operator>${operator})\\s?)(?<value>${value}))`
7
+
8
+ return new RegExp(pattern, 'gm')
9
+ }
10
+
11
+ export function parseFilters(string) {
12
+ const results = []
13
+ const regex = getRegex()
14
+
15
+ // Split the string into an array of tokens
16
+ const tokens = string.matchAll(regex)
17
+ let search = string
18
+ // Iterate over the tokens
19
+ for (const token of tokens) {
20
+ // Extract the named groups from the token
21
+ let { group, column, operator, value } = token.groups
22
+ search = search.replace(group, '').trim()
23
+
24
+ operator = operator
25
+ .replace(':', '~*')
26
+ .replace('=>', '>=')
27
+ .replace('=<', '<=')
28
+
29
+ if (value) {
30
+ value = !isNaN(parseInt(value)) ? parseInt(value) : removeQuotes(value)
31
+
32
+ if (operator.includes('~')) {
33
+ value = operator.includes('*')
34
+ ? new RegExp(value, 'i')
35
+ : new RegExp(value)
36
+ }
37
+ }
38
+ if (column && value) results.push({ column, operator, value })
39
+ }
40
+ if (search.length > 0) {
41
+ results.push({
42
+ operator: '~*',
43
+ value: new RegExp(removeQuotes(search), 'i')
44
+ })
45
+ }
46
+
47
+ return results
48
+ }
49
+
50
+ function removeQuotes(str) {
51
+ const quoteMatch = str.match(/^"([^"]+)"$/)
52
+ return quoteMatch ? quoteMatch[1] : str
53
+ }
package/src/string.js ADDED
@@ -0,0 +1,96 @@
1
+ import { filter } from 'ramda'
2
+ /**
3
+ * Capitalizes the first letter of input string
4
+ *
5
+ * @param {String} str
6
+ * @returns {String}
7
+ */
8
+ export function toInitCapCase(text) {
9
+ return text.charAt(0).toUpperCase() + text.slice(1).toLowerCase()
10
+ }
11
+
12
+ /**
13
+ * Convert a hyphen separated string to PascalCase
14
+ *
15
+ * @param {String} text
16
+ * @returns
17
+ */
18
+ export function toPascalCase(text) {
19
+ return text
20
+ .split('-')
21
+ .map((part) => toInitCapCase(part))
22
+ .join('')
23
+ }
24
+
25
+ /**
26
+ * Convert a PascalCase string to snake case with separator as hyphen
27
+ *
28
+ * @param {Strin} text
29
+ * @returns
30
+ */
31
+ export function toHyphenCase(text) {
32
+ return text
33
+ .replace(/\s+/, '-')
34
+ .replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`)
35
+ .replace(/^-/, '')
36
+ }
37
+
38
+ /**
39
+ * Sort by splitting hyphen separated strings while keeping strings with same number of parts together
40
+ *
41
+ * @param {String} a hyphen separates string
42
+ * @param {String} b hyphen separates string
43
+ * @returns
44
+ */
45
+ export function sortByParts(a, b) {
46
+ const partsOfA = a.split('-')
47
+ const partsOfB = b.split('-')
48
+
49
+ let result = compareStrings(partsOfA[0], partsOfB[0])
50
+ if (result == 0) result = partsOfA.length - partsOfB.length
51
+ if (result == 0) result = compareStrings(a, b)
52
+ return result
53
+ }
54
+
55
+ /**
56
+ * Simple comparison for two strings
57
+ *
58
+ * @param {String} a
59
+ * @param {String} b
60
+ * @returns
61
+ */
62
+ export function compareStrings(a, b) {
63
+ return a > b ? 1 : a < b ? -1 : 0
64
+ }
65
+
66
+ /**
67
+ * Generates a unique id from current timestamp
68
+ *
69
+ * @returns {String} timestamp based unique id
70
+ */
71
+ export function uniqueId(prefix = '', separator = '-') {
72
+ let pair = prefix && prefix.length > 0 ? [prefix] : []
73
+ pair.push(Date.now().toString(36))
74
+ return pair.join(separator)
75
+ }
76
+
77
+ /**
78
+ * Removes undefined and null values from the input object.
79
+ *
80
+ * @param {Object} obj
81
+ * @returns {Object}
82
+ */
83
+ export function compact(obj) {
84
+ return filter((x) => x !== undefined && x !== null, obj)
85
+ }
86
+
87
+ /**
88
+ * Converts an input number into it's hexadecimal representation, with optional left padded zeroes based on the `size`
89
+ *
90
+ * @param {number} value
91
+ * @param {number} size
92
+ * @returns
93
+ */
94
+ export function toHexString(value, size = 2) {
95
+ return value.toString(16).padStart(size, '0')
96
+ }
package/src/ticks.js CHANGED
@@ -2,15 +2,19 @@ export function generateTicks(
2
2
  lowerBound,
3
3
  upperBound,
4
4
  minorTickStep,
5
- majorTickStep = 1
5
+ majorTickStep = 1,
6
+ formatter = new Intl.NumberFormat('en-US', { CompactDisplay: 'short' })
6
7
  ) {
7
8
  minorTickStep = minorTickStep ? minorTickStep : upperBound - lowerBound
8
9
  const length = 1 + Math.ceil((upperBound - lowerBound) / minorTickStep)
9
10
  const ticks = Array.from({ length }, (_, i) => ({
10
11
  value: i == length - 1 ? upperBound : lowerBound + minorTickStep * i,
11
12
  major: i == 0 || i == length - 1 || i % majorTickStep == 0
13
+ })).map(({ value, major }) => ({
14
+ value,
15
+ label: major ? formatter.format(value) : '',
16
+ major
12
17
  }))
13
- //.map(({ value, show }) => ({ value, label: show ? value : '', major }))
14
18
 
15
19
  return ticks
16
20
  }
package/src/types.js ADDED
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Structure to map custom fields for rendering. This is used to identofy the attributes for various purposes.
3
+ *
4
+ * @typedef FieldMapping
5
+ * @property {string} [id='id'] Unique id for the item
6
+ * @property {string} [text='text'] the text to render
7
+ * @property {string} [url='url'] a URL
8
+ * @property {string} [icon='icon'] icon to render
9
+ * @property {string} [image='image'] the image to render
10
+ * @property {string} [children='children'] children of the item
11
+ * @property {string} [summary='summary']
12
+ * @property {string} [notes='notes']
13
+ * @property {string} [props='props']
14
+ * @property {string} [isOpen='_open'] item is open or closed
15
+ * @property {string} [level='level'] level of item
16
+ * @property {string} [parent='parent'] item is a parent
17
+ * @property {string} [isDeleted='_deleted'] item is deleted
18
+ * @property {FieldMapping} [fields] Field mapping to be used on children in the next level
19
+ */
20
+
21
+ /**
22
+ * Component map to be used to render the item.
23
+ * @typedef {Object<string, import('svelte').SvelteComponent>} ComponentMap
24
+ */
package/src/utils.js CHANGED
@@ -1,3 +1,86 @@
1
+ const modifiers = {
2
+ hsl: (value) => `hsl(${value})`,
3
+ rgb: (value) => `rgb(${value})`,
4
+ none: (value) => value
5
+ }
6
+
1
7
  export function id() {
2
8
  return Math.random().toString(36).substr(2, 9)
3
9
  }
10
+
11
+ /**
12
+ * Generate shades for a color using css varuable
13
+ *
14
+ * @param {string} name
15
+ * @returns
16
+ */
17
+ export function shadesOf(name, modifier = 'none') {
18
+ const shades = [50, 100, 200, 300, 400, 500, 600, 700, 800, 900]
19
+ const fn = modifier in modifiers ? modifiers[modifier] : modifiers.none
20
+
21
+ return shades.reduce(
22
+ (result, shade) => ({
23
+ ...result,
24
+ [shade]: fn(`var(--${name}-${shade})`)
25
+ }),
26
+ {
27
+ DEFAULT: fn(`var(--${name}-500)`),
28
+ inset: fn(`var(--${name}-50)`),
29
+ sunken: fn(`var(--${name}-50)`),
30
+ recessed: fn(`var(--${name}-50)`),
31
+ base: fn(`var(--${name}-100)`),
32
+ subtle: fn(`var(--${name}-200)`),
33
+ muted: fn(`var(--${name}-300)`),
34
+ raised: fn(`var(--${name}-400)`),
35
+ elevated: fn(`var(--${name}-500)`),
36
+ floating: fn(`var(--${name}-600)`),
37
+ contrast: fn(`var(--${name}-700)`)
38
+ }
39
+ )
40
+ }
41
+
42
+ export function stateColors(name, modifier = 'none') {
43
+ const fn = modifier in modifiers ? modifiers[modifier] : modifiers.none
44
+ return {
45
+ DEFAULT: fn(`var(--${name}-500)`),
46
+ light: fn(`var(--${name}-100)`),
47
+ dark: fn(`var(--${name}-800)`)
48
+ }
49
+ }
50
+
51
+ export function themeColors(modifier = 'none') {
52
+ const fn = modifier in modifiers ? modifiers[modifier] : modifiers.none
53
+
54
+ let states = ['info', 'error', 'warn', 'pass']
55
+ let variants = ['skin', 'primary', 'secondary', 'accent']
56
+ let colors = states.reduce(
57
+ (acc, state) => ({ ...acc, [state]: stateColors(state, modifier) }),
58
+ {}
59
+ )
60
+ colors = variants.reduce(
61
+ (acc, variant) => ({ ...acc, [variant]: shadesOf(variant, modifier) }),
62
+ colors
63
+ )
64
+ colors.skin = {
65
+ ...colors.skin,
66
+ contrast: fn(`var(--skin-800)`),
67
+ zebra: fn(`var(--skin-zebra)`)
68
+ }
69
+
70
+ return colors
71
+ }
72
+
73
+ export function iconShortcuts(icons, collection, variants) {
74
+ const suffix = variants ? `-${variants}` : ''
75
+ const shortcuts = !collection
76
+ ? {}
77
+ : icons.reduce(
78
+ (acc, name) => ({
79
+ ...acc,
80
+ [name]: [collection, name].join(':') + suffix
81
+ }),
82
+ {}
83
+ )
84
+
85
+ return shortcuts
86
+ }