@dataloop-ai/components 0.19.106 → 0.19.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.
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.108",
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
 
@@ -357,7 +363,9 @@ const getError = (
357
363
  for (const key of Object.keys(schema)) {
358
364
  if (isObject(schema[key]) && !Array.isArray(schema[key])) {
359
365
  const flattened = flatten({ [key]: schema[key] })
360
- keys.push(...Object.keys(flattened))
366
+ for (const k of Object.keys(flattened)) {
367
+ keys.push(k)
368
+ }
361
369
  } else {
362
370
  keys.push(key)
363
371
  }
@@ -402,7 +410,7 @@ const getError = (
402
410
 
403
411
  const isValidByDataType = (
404
412
  str: string | string[],
405
- dataType: string | string[],
413
+ dataType: string | (string | Data)[],
406
414
  operator: string
407
415
  ): boolean => {
408
416
  if (dataType === 'any') {
@@ -423,9 +431,16 @@ const isValidByDataType = (
423
431
  */
424
432
 
425
433
  if (Array.isArray(dataType)) {
426
- let isOneOf = !!getValueMatch(dataType, str)
434
+ let isOneOf = !!getValueMatch(
435
+ dataType.filter((type) => typeof type !== 'object') as string[],
436
+ str
437
+ )
427
438
  for (const type of dataType) {
428
- isOneOf = isOneOf || isValidByDataType(str, type, operator)
439
+ if (typeof type === 'object') {
440
+ isOneOf = isOneOf || !!getValueMatch(Object.keys(type), str)
441
+ } else {
442
+ isOneOf = isOneOf || isValidByDataType(str, type, operator)
443
+ }
429
444
  }
430
445
  return isOneOf
431
446
  }
@@ -507,7 +522,7 @@ const getDataType = (
507
522
  schema: Schema,
508
523
  aliases: Alias[],
509
524
  key: string
510
- ): string | string[] | null => {
525
+ ): string | (string | Data)[] | null => {
511
526
  const aliasedKey = getAliasObjByAlias(aliases, key)?.key ?? key
512
527
 
513
528
  const nestedKey = aliasedKey.split('.').filter((el) => el)
@@ -529,7 +544,7 @@ const getDataType = (
529
544
  return 'object'
530
545
  }
531
546
 
532
- return value as unknown as string | string[] | null
547
+ return value as unknown as string | (string | Data)[] | null
533
548
  }
534
549
 
535
550
  const getAliasObjByAlias = (aliases: Alias[], alias: string): Alias | null => {
@@ -666,14 +681,24 @@ export const removeLeadingExpression = (str: string) => {
666
681
  return str.match(/\s+(.*)$/)?.[1] || ''
667
682
  }
668
683
 
669
- const getValueSuggestions = (dataType: string | string[], operator: string) => {
670
- const types: string[] = Array.isArray(dataType) ? dataType : [dataType]
684
+ const getValueSuggestions = (
685
+ dataType: string | (string | Data)[],
686
+ operator: string
687
+ ) => {
688
+ const types: (string | Data)[] = Array.isArray(dataType)
689
+ ? dataType
690
+ : [dataType]
671
691
  const suggestion: string[] = []
672
692
 
673
693
  if (Array.isArray(dataType)) {
674
- suggestion.push(
675
- ...dataType.filter((type) => !knownDataTypes.includes(type))
676
- )
694
+ for (const type of dataType) {
695
+ if (
696
+ !knownDataTypes.includes(type as string) &&
697
+ typeof type !== 'object'
698
+ ) {
699
+ suggestion.push(type)
700
+ }
701
+ }
677
702
  }
678
703
 
679
704
  for (const type of types) {
@@ -688,7 +713,10 @@ const getValueSuggestions = (dataType: string | string[], operator: string) => {
688
713
  case 'datetime':
689
714
  suggestion.push(dateSuggestionPattern)
690
715
  default:
691
- // do nothing
716
+ if (typeof type === 'object') {
717
+ // value aliases: key is the alias, value is the actual value
718
+ for (const key in type) suggestion.push(key)
719
+ }
692
720
  break
693
721
  }
694
722
  }