@featurevisor/sdk 2.3.1 → 2.3.3
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/CHANGELOG.md +16 -0
- package/coverage/clover.xml +34 -36
- package/coverage/coverage-final.json +2 -2
- package/coverage/lcov-report/bucketer.ts.html +1 -1
- package/coverage/lcov-report/child.ts.html +1 -1
- package/coverage/lcov-report/conditions.ts.html +1 -1
- package/coverage/lcov-report/datafileReader.ts.html +28 -28
- package/coverage/lcov-report/emitter.ts.html +1 -1
- package/coverage/lcov-report/evaluate.ts.html +1 -1
- package/coverage/lcov-report/events.ts.html +1 -1
- package/coverage/lcov-report/helpers.ts.html +39 -54
- package/coverage/lcov-report/hooks.ts.html +1 -1
- package/coverage/lcov-report/index.html +23 -23
- package/coverage/lcov-report/instance.ts.html +1 -1
- package/coverage/lcov-report/logger.ts.html +1 -1
- package/coverage/lcov.info +62 -64
- package/dist/helpers.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.gz +0 -0
- package/dist/index.mjs.map +1 -1
- package/lib/helpers.d.ts +1 -1
- package/package.json +2 -2
- package/src/datafileReader.spec.ts +19 -0
- package/src/helpers.spec.ts +49 -0
- package/src/helpers.ts +20 -25
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { getValueByType, ValueType } from "./helpers";
|
|
2
|
+
|
|
3
|
+
describe("sdk: helpers", function () {
|
|
4
|
+
it("should return null for type mismatch", function () {
|
|
5
|
+
expect(getValueByType(1, "string")).toEqual(null);
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
it("should return the value as is if it is a string", function () {
|
|
9
|
+
expect(getValueByType("1", "string")).toEqual("1");
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it("should return the value as is if it is a boolean", function () {
|
|
13
|
+
expect(getValueByType(true, "boolean")).toEqual(true);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("should return the value as is if it is an object", function () {
|
|
17
|
+
expect(getValueByType({ a: 1, b: 2 }, "object")).toEqual({ a: 1, b: 2 });
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("should return the value as is if it is a json", function () {
|
|
21
|
+
expect(getValueByType(JSON.stringify({ a: 1, b: 2 }), "json")).toEqual(
|
|
22
|
+
JSON.stringify({ a: 1, b: 2 }),
|
|
23
|
+
);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("should return array if the value is an array", function () {
|
|
27
|
+
expect(getValueByType(["1", "2", "3"], "array")).toEqual(["1", "2", "3"]);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("should return integer if the value is an integer", function () {
|
|
31
|
+
expect(getValueByType("1", "integer")).toEqual(1);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("should return double if the value is a double", function () {
|
|
35
|
+
expect(getValueByType("1.1", "double")).toEqual(1.1);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("should return null if the value is undefined", function () {
|
|
39
|
+
expect(getValueByType(undefined, "string")).toEqual(null);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("should return null if the value is null", function () {
|
|
43
|
+
expect(getValueByType(null, "string")).toEqual(null);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("should return null when a function is passed", function () {
|
|
47
|
+
expect(getValueByType(function () {} as unknown as ValueType, "string")).toEqual(null);
|
|
48
|
+
});
|
|
49
|
+
});
|
package/src/helpers.ts
CHANGED
|
@@ -1,33 +1,28 @@
|
|
|
1
1
|
import type { VariableType, VariableValue } from "@featurevisor/types";
|
|
2
2
|
|
|
3
3
|
type FieldType = string | VariableType;
|
|
4
|
-
type ValueType = VariableValue;
|
|
4
|
+
export type ValueType = VariableValue;
|
|
5
5
|
|
|
6
6
|
export function getValueByType(value: ValueType, fieldType: FieldType): ValueType {
|
|
7
|
-
|
|
8
|
-
if (value === undefined) {
|
|
9
|
-
return null;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
switch (fieldType) {
|
|
13
|
-
case "string":
|
|
14
|
-
return typeof value === "string" ? value : null;
|
|
15
|
-
case "integer":
|
|
16
|
-
return parseInt(value as string, 10);
|
|
17
|
-
case "double":
|
|
18
|
-
return parseFloat(value as string);
|
|
19
|
-
case "boolean":
|
|
20
|
-
return value === true;
|
|
21
|
-
case "array":
|
|
22
|
-
return Array.isArray(value) ? value : null;
|
|
23
|
-
case "object":
|
|
24
|
-
return typeof value === "object" ? value : null;
|
|
25
|
-
// @NOTE: `json` is not handled here intentionally
|
|
26
|
-
default:
|
|
27
|
-
return value;
|
|
28
|
-
}
|
|
29
|
-
// eslint-disable-next-line
|
|
30
|
-
} catch (e) {
|
|
7
|
+
if (value === undefined) {
|
|
31
8
|
return null;
|
|
32
9
|
}
|
|
10
|
+
|
|
11
|
+
switch (fieldType) {
|
|
12
|
+
case "string":
|
|
13
|
+
return typeof value === "string" ? value : null;
|
|
14
|
+
case "integer":
|
|
15
|
+
return parseInt(value as string, 10);
|
|
16
|
+
case "double":
|
|
17
|
+
return parseFloat(value as string);
|
|
18
|
+
case "boolean":
|
|
19
|
+
return value === true;
|
|
20
|
+
case "array":
|
|
21
|
+
return Array.isArray(value) ? value : null;
|
|
22
|
+
case "object":
|
|
23
|
+
return typeof value === "object" ? value : null;
|
|
24
|
+
// @NOTE: `json` is not handled here intentionally
|
|
25
|
+
default:
|
|
26
|
+
return value;
|
|
27
|
+
}
|
|
33
28
|
}
|