@dataloop-ai/components 0.19.106 → 0.19.107

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": "@dataloop-ai/components",
3
- "version": "0.19.106",
3
+ "version": "0.19.107",
4
4
  "exports": {
5
5
  ".": "./index.ts",
6
6
  "./models": "./models.ts",
@@ -124,7 +124,9 @@ import {
124
124
  replaceJSDatesWithStringifiedDates,
125
125
  replaceStringifiedDatesWithJSDates,
126
126
  setAliases,
127
- revertAliases
127
+ revertAliases,
128
+ setValueAliases,
129
+ revertValueAliases
128
130
  } from '../utils'
129
131
  import { v4 } from 'uuid'
130
132
  import {
@@ -569,7 +571,9 @@ export default defineComponent({
569
571
  const replacedDate = replaceStringifiedDatesWithJSDates(value)
570
572
  const json = parseSmartQuery(replacedDate ?? searchQuery.value)
571
573
 
572
- return isValidJSON(json) ? json : searchQuery.value
574
+ return isValidJSON(json)
575
+ ? revertValueAliases(json, schema.value)
576
+ : searchQuery.value
573
577
  }
574
578
 
575
579
  const fromJSON = (value: { [key: string]: any }) => {
@@ -579,7 +583,9 @@ export default defineComponent({
579
583
  dateKeys.value
580
584
  )
581
585
 
582
- const stringQuery = stringifySmartQuery(replacedDate)
586
+ const stringQuery = stringifySmartQuery(
587
+ setValueAliases(replacedDate, schema.value)
588
+ )
583
589
  const aliased = setAliases(stringQuery, aliases.value)
584
590
  return aliased
585
591
  } catch (e) {
@@ -5,6 +5,7 @@ import { ColorSchema, SyntaxColorSchema, Filters } from '../types'
5
5
  import {
6
6
  operators,
7
7
  Alias,
8
+ Data,
8
9
  datePattern,
9
10
  datePatternNoBrackets,
10
11
  removeBrackets
@@ -187,6 +188,62 @@ export function setAliases(str: string, aliases: Alias[]) {
187
188
  return str.replace(regex, replacement)
188
189
  }
189
190
 
191
+ function valueAliases(schema: Data, field: string) {
192
+ let aliases: Data = {}
193
+ const type: any = schema[field]
194
+ if (Array.isArray(type)) {
195
+ for (const element of type) {
196
+ if (typeof element === 'object')
197
+ aliases = Object.assign(aliases, element)
198
+ }
199
+ } else {
200
+ if (typeof type === 'object') aliases = Object.assign(aliases, type)
201
+ }
202
+ return aliases
203
+ }
204
+
205
+ export function revertValueAliases(json: Data, schema: Data) {
206
+ const clone = cloneDeep(json)
207
+ const replaceAliases = (where: Data) => {
208
+ for (const key in where) {
209
+ if (typeof where[key] === 'object') {
210
+ replaceAliases(where[key])
211
+ } else {
212
+ const aliases = valueAliases(schema, key)
213
+ const value = aliases[where[key] as string]
214
+ if (value) {
215
+ where[key] = value
216
+ }
217
+ }
218
+ }
219
+ }
220
+
221
+ replaceAliases(clone)
222
+ return clone
223
+ }
224
+
225
+ export function setValueAliases(json: Data, schema: Data) {
226
+ const clone = cloneDeep(json)
227
+ const replaceValues = (where: Data) => {
228
+ for (const key in where) {
229
+ if (typeof where[key] === 'object') {
230
+ replaceValues(where[key])
231
+ } else {
232
+ const aliases = valueAliases(schema, key)
233
+ for (const alias in aliases) {
234
+ if (where[key] === aliases[alias]) {
235
+ where[key] = alias
236
+ break
237
+ }
238
+ }
239
+ }
240
+ }
241
+ }
242
+
243
+ replaceValues(clone)
244
+ return clone
245
+ }
246
+
190
247
  export function createColorSchema(
191
248
  colorSchema: ColorSchema,
192
249
  aliases: Alias[]
@@ -140,6 +140,13 @@ export default defineComponent({
140
140
  test2: ['true', 'false']
141
141
  }
142
142
  const schema2: any = {
143
+ value: [
144
+ 'string',
145
+ {
146
+ John: 'AB34',
147
+ Connor: '42'
148
+ }
149
+ ],
143
150
  type: [
144
151
  'class',
145
152
  'point',
@@ -3,12 +3,16 @@ import { splitByQuotes } from '../utils/splitByQuotes'
3
3
  import { flatten } from 'flat'
4
4
  import { isObject } from 'lodash'
5
5
 
6
+ export type Data = {
7
+ [key: string]: any
8
+ }
9
+
6
10
  export type Schema = {
7
11
  [key: string]:
8
12
  | string
9
13
  | number
10
14
  | boolean
11
- | (number | boolean | string)[]
15
+ | (number | boolean | string | Data)[]
12
16
  | Schema
13
17
  }
14
18
 
@@ -245,8 +249,10 @@ export const useSuggestions = (
245
249
 
246
250
  if (Array.isArray(dataType)) {
247
251
  localSuggestions = dataType.filter(
248
- (type) => !knownDataTypes.includes(type)
249
- )
252
+ (type) =>
253
+ typeof type === 'string' &&
254
+ !knownDataTypes.includes(type)
255
+ ) as string[]
250
256
 
251
257
  if (!value) continue
252
258
 
@@ -402,7 +408,7 @@ const getError = (
402
408
 
403
409
  const isValidByDataType = (
404
410
  str: string | string[],
405
- dataType: string | string[],
411
+ dataType: string | (string | Data)[],
406
412
  operator: string
407
413
  ): boolean => {
408
414
  if (dataType === 'any') {
@@ -423,9 +429,16 @@ const isValidByDataType = (
423
429
  */
424
430
 
425
431
  if (Array.isArray(dataType)) {
426
- let isOneOf = !!getValueMatch(dataType, str)
432
+ let isOneOf = !!getValueMatch(
433
+ dataType.filter((type) => typeof type !== 'object') as string[],
434
+ str
435
+ )
427
436
  for (const type of dataType) {
428
- isOneOf = isOneOf || isValidByDataType(str, type, operator)
437
+ if (typeof type === 'object') {
438
+ isOneOf = isOneOf || !!getValueMatch(Object.keys(type), str)
439
+ } else {
440
+ isOneOf = isOneOf || isValidByDataType(str, type, operator)
441
+ }
429
442
  }
430
443
  return isOneOf
431
444
  }
@@ -507,7 +520,7 @@ const getDataType = (
507
520
  schema: Schema,
508
521
  aliases: Alias[],
509
522
  key: string
510
- ): string | string[] | null => {
523
+ ): string | (string | Data)[] | null => {
511
524
  const aliasedKey = getAliasObjByAlias(aliases, key)?.key ?? key
512
525
 
513
526
  const nestedKey = aliasedKey.split('.').filter((el) => el)
@@ -529,7 +542,7 @@ const getDataType = (
529
542
  return 'object'
530
543
  }
531
544
 
532
- return value as unknown as string | string[] | null
545
+ return value as unknown as string | (string | Data)[] | null
533
546
  }
534
547
 
535
548
  const getAliasObjByAlias = (aliases: Alias[], alias: string): Alias | null => {
@@ -666,13 +679,22 @@ export const removeLeadingExpression = (str: string) => {
666
679
  return str.match(/\s+(.*)$/)?.[1] || ''
667
680
  }
668
681
 
669
- const getValueSuggestions = (dataType: string | string[], operator: string) => {
670
- const types: string[] = Array.isArray(dataType) ? dataType : [dataType]
682
+ const getValueSuggestions = (
683
+ dataType: string | (string | Data)[],
684
+ operator: string
685
+ ) => {
686
+ const types: (string | Data)[] = Array.isArray(dataType)
687
+ ? dataType
688
+ : [dataType]
671
689
  const suggestion: string[] = []
672
690
 
673
691
  if (Array.isArray(dataType)) {
674
692
  suggestion.push(
675
- ...dataType.filter((type) => !knownDataTypes.includes(type))
693
+ ...(dataType.filter(
694
+ (type) =>
695
+ !knownDataTypes.includes(type as string) &&
696
+ typeof type !== 'object'
697
+ ) as string[])
676
698
  )
677
699
  }
678
700
 
@@ -688,7 +710,10 @@ const getValueSuggestions = (dataType: string | string[], operator: string) => {
688
710
  case 'datetime':
689
711
  suggestion.push(dateSuggestionPattern)
690
712
  default:
691
- // do nothing
713
+ if (typeof type === 'object') {
714
+ // value aliases: key is the alias, value is the actual value
715
+ for (const key in type) suggestion.push(key)
716
+ }
692
717
  break
693
718
  }
694
719
  }