mirror of https://github.com/grafana/grafana
FieldValues: Implement array accessors for deprecated Vector types (#66807)
parent
9452c0d718
commit
987eff82a3
@ -1,23 +1,14 @@ |
||||
import { Vector } from '../types'; |
||||
|
||||
import { FunctionalVector } from './FunctionalVector'; |
||||
|
||||
/** |
||||
* This will force all values to be numbers |
||||
* |
||||
* @public |
||||
* @deprecated use a simple Arrays |
||||
* @deprecated use a simple Arrays. NOTE: Not used in grafana core |
||||
*/ |
||||
export class AsNumberVector extends FunctionalVector<number> { |
||||
constructor(private field: Vector) { |
||||
export class AsNumberVector extends Array<number> { |
||||
constructor(field: Vector) { |
||||
super(); |
||||
} |
||||
|
||||
get length() { |
||||
return this.field.length; |
||||
} |
||||
|
||||
get(index: number) { |
||||
return +this.field.get(index); |
||||
return field.map((v) => +v) as AsNumberVector; |
||||
} |
||||
} |
||||
|
@ -1,31 +1,18 @@ |
||||
import { Vector } from '../types/vector'; |
||||
import { BinaryOperation } from '../utils/binaryOperators'; |
||||
|
||||
import { FunctionalVector } from './FunctionalVector'; |
||||
import { vectorToArray } from './vectorToArray'; |
||||
|
||||
/** |
||||
* @public |
||||
* @deprecated use a simple Arrays |
||||
* @deprecated use a simple Arrays. NOTE: Not used in grafana core |
||||
*/ |
||||
export class BinaryOperationVector extends FunctionalVector<number> { |
||||
constructor(private left: Vector<number>, private right: Vector<number>, private operation: BinaryOperation) { |
||||
export class BinaryOperationVector extends Array<number> { |
||||
constructor(left: Vector<number>, right: Vector<number>, operation: BinaryOperation) { |
||||
super(); |
||||
} |
||||
|
||||
get length(): number { |
||||
return this.left.length; |
||||
} |
||||
|
||||
get(index: number): number { |
||||
return this.operation(this.left.get(index), this.right.get(index)); |
||||
} |
||||
|
||||
toArray(): number[] { |
||||
return vectorToArray(this); |
||||
} |
||||
|
||||
toJSON(): number[] { |
||||
return vectorToArray(this); |
||||
const arr = new Array(left.length); |
||||
for (let i = 0; i < arr.length; i++) { |
||||
arr[i] = operation(left[i], right[i]); |
||||
} |
||||
return arr as BinaryOperationVector; |
||||
} |
||||
} |
||||
|
@ -1,28 +1,10 @@ |
||||
import { FunctionalVector } from './FunctionalVector'; |
||||
|
||||
/** |
||||
* @public |
||||
* @deprecated use a simple Arrays |
||||
* @deprecated use a simple Arrays. NOTE: Not used in grafana core. |
||||
*/ |
||||
export class ConstantVector<T = any> extends FunctionalVector<T> { |
||||
constructor(private value: T, private len: number) { |
||||
export class ConstantVector<T = any> extends Array<T> { |
||||
constructor(value: T, len: number) { |
||||
super(); |
||||
} |
||||
|
||||
get length() { |
||||
return this.len; |
||||
} |
||||
|
||||
get(index: number): T { |
||||
return this.value; |
||||
} |
||||
|
||||
toArray(): T[] { |
||||
const arr = new Array<T>(this.length); |
||||
return arr.fill(this.value); |
||||
} |
||||
|
||||
toJSON(): T[] { |
||||
return this.toArray(); |
||||
return new Array<T>(len).fill(value) as ConstantVector<T>; |
||||
} |
||||
} |
||||
|
@ -1,33 +0,0 @@ |
||||
import { Vector } from '../types'; |
||||
|
||||
import { FunctionalVector } from './FunctionalVector'; |
||||
import { vectorToArray } from './vectorToArray'; |
||||
|
||||
/** |
||||
* RowVector makes the row values look like a vector |
||||
* @internal |
||||
* @deprecated use a simple Arrays |
||||
*/ |
||||
export class RowVector extends FunctionalVector<number> { |
||||
constructor(private columns: Vector[]) { |
||||
super(); |
||||
} |
||||
|
||||
rowIndex = 0; |
||||
|
||||
get length(): number { |
||||
return this.columns.length; |
||||
} |
||||
|
||||
get(index: number): number { |
||||
return this.columns[index].get(this.rowIndex); |
||||
} |
||||
|
||||
toArray(): number[] { |
||||
return vectorToArray(this); |
||||
} |
||||
|
||||
toJSON(): number[] { |
||||
return vectorToArray(this); |
||||
} |
||||
} |
@ -0,0 +1,13 @@ |
||||
import { ArrayVector } from './ArrayVector'; |
||||
import { SortedVector } from './SortedVector'; |
||||
|
||||
describe('SortedVector', () => { |
||||
it('Should support sorting', () => { |
||||
const values = new ArrayVector([1, 5, 2, 4]); |
||||
const sorted = new SortedVector(values, [0, 2, 3, 1]); |
||||
expect(sorted.toArray()).toEqual([1, 2, 4, 5]); |
||||
|
||||
// The proxy should still be an instance of SortedVector (used in timeseries)
|
||||
expect(sorted instanceof SortedVector).toBeTruthy(); |
||||
}); |
||||
}); |
Loading…
Reference in new issue