-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: variable flickers when changing value from right sidebar panel
- Loading branch information
1 parent
5456858
commit d503939
Showing
6 changed files
with
109 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
interface ExtractedRange { | ||
minValue: string; | ||
delimiter: string; | ||
maxValue: string; | ||
} | ||
|
||
const extractRange = (value: string): ExtractedRange | null => { | ||
const numberRegex = /(-?\d+)/; | ||
const firstNumberMatch = numberRegex.exec(value); | ||
|
||
if (!firstNumberMatch) { | ||
return null; | ||
} | ||
|
||
const firstNumber = firstNumberMatch[1]; | ||
const restOfString = value.slice(firstNumberMatch.index + firstNumber.length); | ||
|
||
const delimiterRegex = /(.+?)(-?\d+)([^\d]*)$/; | ||
const delimiterMatch = delimiterRegex.exec(restOfString); | ||
|
||
if (!delimiterMatch || delimiterMatch.length < 3) { | ||
return null; | ||
} | ||
|
||
return { | ||
minValue: firstNumber, | ||
delimiter: delimiterMatch[1].trim(), | ||
maxValue: delimiterMatch[2], | ||
}; | ||
}; | ||
|
||
export default extractRange; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,11 @@ | ||
export default function findMinAndMaxValues(variableValue: string) { | ||
const numberRegex = /(-?\d+)/; | ||
const firstNumberMatch = numberRegex.exec(variableValue); | ||
|
||
if (!firstNumberMatch) { | ||
throw new Error('Could not find any number'); | ||
} | ||
import extractRange from './extractRange'; | ||
|
||
const firstNumber = firstNumberMatch[1]; | ||
const restOfString = variableValue.slice(firstNumberMatch.index + firstNumber.length); | ||
|
||
const delimiterRegex = /(.+?)(-?\d+)([^\d]*)$/; | ||
const delimiterMatch = delimiterRegex.exec(restOfString); | ||
export default function findMinAndMaxValues(variableValue: string) { | ||
const extracted = extractRange(variableValue); | ||
|
||
if (delimiterMatch && delimiterMatch.length >= 3) { | ||
return { | ||
minValue: firstNumber, | ||
delimiter: delimiterMatch[1].trim(), | ||
maxValue: delimiterMatch[2], | ||
}; | ||
} else { | ||
if (!extracted) { | ||
throw new Error('Could not find min and max values'); | ||
} | ||
|
||
return extracted; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { isValidVariable } from './isValidVariable'; | ||
|
||
describe('isValidVariable', () => { | ||
it('should return true for valid RangeVariable with valid range format', () => { | ||
const validVariable = { current: { value: '10-90' } }; | ||
expect(isValidVariable(validVariable)).toBe(true); | ||
}); | ||
|
||
it('should return false for RangeVariable with invalid range format', () => { | ||
const invalidVariable = { current: { value: 'invalid range' } }; | ||
expect(isValidVariable(invalidVariable)).toBe(false); | ||
}); | ||
|
||
it('should return false for RangeVariable with no value property', () => { | ||
const invalidVariable = { current: {} }; | ||
expect(isValidVariable(invalidVariable)).toBe(false); | ||
}); | ||
|
||
it('should return false for non-object input', () => { | ||
expect(isValidVariable(null)).toBe(false); | ||
expect(isValidVariable(undefined)).toBe(false); | ||
expect(isValidVariable('not an object')).toBe(false); | ||
expect(isValidVariable(123)).toBe(false); | ||
}); | ||
|
||
it('should return false for RangeVariable where current is null', () => { | ||
const invalidVariable = { current: null }; | ||
expect(isValidVariable(invalidVariable)).toBe(false); | ||
}); | ||
|
||
it('should return false for RangeVariable where value is not a string', () => { | ||
const invalidVariable = { current: { value: 123 } }; | ||
expect(isValidVariable(invalidVariable)).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import extractRange from './extractRange'; | ||
|
||
const isValidRangeFormat = (value: string): boolean => { | ||
try { | ||
return !!extractRange(value); | ||
} catch { | ||
return false; | ||
} | ||
}; | ||
|
||
export interface RangeVariable { | ||
current: { | ||
value: string; | ||
}; | ||
} | ||
|
||
export const isValidVariable = (v: any): v is RangeVariable => { | ||
if (v === null || v === undefined) { | ||
return false; | ||
} | ||
|
||
if (!v.current || v.current === null) { | ||
return false; | ||
} | ||
|
||
if (typeof v.current.value !== 'string') { | ||
return false; | ||
} | ||
|
||
return isValidRangeFormat(v.current.value); | ||
}; |