Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | 3x 137x 10x 10x 137x 3x 31x 31x 31x 1x 1x 1x 1x 30x 30x 30x 6x 30x 30x 17x 30x 30x 27x 2x 9x 9x 27x 3x 2x 3x 3x 4x 3x 2x 139x 139x 2x 137x 120x | import {
Feature,
Segment,
DatafileContentV1,
DatafileContentV2,
Attribute,
AttributeKey,
SegmentKey,
FeatureKey,
} from "@featurevisor/types";
export function parseJsonConditionsIfStringified<T>(record: T, key: string): T {
if (typeof record[key] === "string" && record[key] !== "*") {
try {
record[key] = JSON.parse(record[key]);
} catch (e) {
console.error("Error parsing JSON", e);
}
}
return record;
}
export class DatafileReader {
private schemaVersion: string;
private revision: string;
private attributes: Record<AttributeKey, Attribute>;
private segments: Record<SegmentKey, Segment>;
private features: Record<FeatureKey, Feature>;
constructor(datafileJson: DatafileContentV1 | DatafileContentV2) {
this.schemaVersion = datafileJson.schemaVersion;
this.revision = datafileJson.revision;
if (this.schemaVersion === "2") {
// v2
const datafileJsonV2 = datafileJson as DatafileContentV2;
this.attributes = datafileJsonV2.attributes;
this.segments = datafileJsonV2.segments;
this.features = datafileJsonV2.features;
} else {
// v1
const datafileJsonV1 = datafileJson as DatafileContentV1;
this.attributes = {};
datafileJsonV1.attributes.forEach((a) => {
this.attributes[a.key] = a;
});
this.segments = {};
datafileJsonV1.segments.forEach((s) => {
this.segments[s.key] = s;
});
this.features = {};
datafileJsonV1.features.forEach((f) => {
if (Array.isArray(f.variablesSchema)) {
f.variablesSchema = f.variablesSchema.reduce((acc, variable) => {
acc[variable.key] = variable;
return acc;
}, {});
}
this.features[f.key] = f;
});
}
}
getRevision(): string {
return this.revision;
}
getSchemaVersion(): string {
return this.schemaVersion;
}
getAllAttributes(): Attribute[] {
const result: Attribute[] = [];
Object.keys(this.attributes).forEach((key) => {
result.push(this.attributes[key]);
});
return result;
}
getAttribute(attributeKey: AttributeKey): Attribute | undefined {
return this.attributes[attributeKey];
}
getSegment(segmentKey: SegmentKey): Segment | undefined {
const segment = this.segments[segmentKey];
if (!segment) {
return undefined;
}
return parseJsonConditionsIfStringified(segment, "conditions");
}
getFeature(featureKey: FeatureKey): Feature | undefined {
return this.features[featureKey];
}
}
|