@prismatic-io/spectral
Advanced tools
Comparing version 2.0.0-beta.8 to 2.0.0-beta.9
@@ -9,2 +9,4 @@ import { DataPayload } from "./server-types"; | ||
toInt: (value: unknown, defaultValue?: number | undefined) => number; | ||
isNumber: (value: unknown) => boolean; | ||
toNumber: (value: unknown, defaultValue?: number | undefined) => number; | ||
isBigInt: (value: unknown) => value is bigint; | ||
@@ -11,0 +13,0 @@ toBigInt: (value: unknown) => bigint; |
@@ -24,6 +24,3 @@ "use strict"; | ||
} | ||
if (typeof value === "undefined") { | ||
return Boolean(defaultValue); | ||
} | ||
throw new Error(`Value '${value}' cannot be coerced to bool.`); | ||
return Boolean(value || defaultValue); | ||
}; | ||
@@ -34,2 +31,6 @@ const isInt = (value) => Number.isInteger(value); | ||
return value; | ||
// Turn a float into an int | ||
if (typeof value === "number") { | ||
return ~~value; | ||
} | ||
if (typeof value === "string") { | ||
@@ -41,3 +42,3 @@ const intValue = Number.parseInt(value); | ||
} | ||
if (typeof value === "undefined") { | ||
if (typeof value === "undefined" || value === "") { | ||
return defaultValue || 0; | ||
@@ -47,2 +48,12 @@ } | ||
}; | ||
const isNumber = (value) => !Number.isNaN(Number(value)); | ||
const toNumber = (value, defaultValue) => { | ||
if (isNumber(value)) { | ||
return Number(value); | ||
} | ||
if (typeof value === "undefined" || value === "") { | ||
return defaultValue || 0; | ||
} | ||
throw new Error(`Value '${value}' cannot be coerced to a number.`); | ||
}; | ||
const isBigInt = (value) => typeof value === "bigint"; | ||
@@ -116,2 +127,4 @@ const toBigInt = (value) => { | ||
toInt, | ||
isNumber, | ||
toNumber, | ||
isBigInt, | ||
@@ -118,0 +131,0 @@ toBigInt, |
@@ -14,6 +14,7 @@ "use strict"; | ||
const unknowns = () => fast_check_1.default.constantFrom(undefined); | ||
const emptyStrings = () => fast_check_1.default.constantFrom(""); | ||
describe("boolean", () => { | ||
const booleanStringValues = { | ||
truthy: ["true", "t", "T", "yes", "y", "Y"], | ||
falsy: ["false", "f", "F", "no", "n", "N"], | ||
falsy: ["false", "f", "F", "no", "n", "N", ""], | ||
}; | ||
@@ -38,5 +39,2 @@ const truthy = () => fast_check_1.default.constantFrom(true, ...booleanStringValues.truthy); | ||
}); | ||
it("throws when coercing non-boolean values", () => { | ||
fast_check_1.default.assert(fast_check_1.default.property(invalidValues, (v) => expect(() => util_1.default.types.toBool(v)).toThrow("cannot be coerced to bool"))); | ||
}); | ||
it("allows for boolean default to false for undefined inputs and undefined default", () => { | ||
@@ -51,5 +49,14 @@ fast_check_1.default.assert(fast_check_1.default.property(unknowns(), (v) => expect(util_1.default.types.toBool(v)).toStrictEqual(false))); | ||
}); | ||
it("allows for boolean default to false for empty string inputs and undefined default", () => { | ||
fast_check_1.default.assert(fast_check_1.default.property(emptyStrings(), (v) => expect(util_1.default.types.toBool(v)).toStrictEqual(false))); | ||
}); | ||
it("allows for boolean default of false for empty string inputs", () => { | ||
fast_check_1.default.assert(fast_check_1.default.property(emptyStrings(), (v) => expect(util_1.default.types.toBool(v, false)).toStrictEqual(false))); | ||
}); | ||
it("allows for boolean default of true for empty string inputs", () => { | ||
fast_check_1.default.assert(fast_check_1.default.property(emptyStrings(), (v) => expect(util_1.default.types.toBool(v, true)).toStrictEqual(true))); | ||
}); | ||
}); | ||
describe("integer", () => { | ||
const invalidValues = fast_check_1.default.oneof(fast_check_1.default.string().filter((v) => Number.isNaN(Number.parseInt(v)))); | ||
const invalidValues = fast_check_1.default.oneof(fast_check_1.default.string().filter((v) => Number.isNaN(Number.parseInt(v)) && v !== "")); | ||
it("detects integer value", () => { | ||
@@ -64,2 +71,5 @@ fast_check_1.default.assert(fast_check_1.default.property(fast_check_1.default.integer(), (v) => expect(util_1.default.types.isInt(v)).toStrictEqual(true))); | ||
}); | ||
it("coerces a float to an int", () => { | ||
fast_check_1.default.assert(fast_check_1.default.property(fast_check_1.default.float(), (v) => expect(util_1.default.types.toInt(v)).toStrictEqual(~~v))); | ||
}); | ||
it("throws when coercing non-integer values", () => { | ||
@@ -74,3 +84,31 @@ fast_check_1.default.assert(fast_check_1.default.property(invalidValues, (v) => expect(() => util_1.default.types.toInt(v)).toThrow("cannot be coerced to int"))); | ||
}); | ||
it("Allows for default value of 0 when value is empty string", () => { | ||
fast_check_1.default.assert(fast_check_1.default.property(emptyStrings(), (v) => expect(util_1.default.types.toInt(v)).toStrictEqual(0))); | ||
}); | ||
it("Allows for default values when value is empty string", () => { | ||
fast_check_1.default.assert(fast_check_1.default.property(emptyStrings(), (v) => expect(util_1.default.types.toInt(v, 20)).toStrictEqual(20))); | ||
}); | ||
}); | ||
describe("number", () => { | ||
const validValues = fast_check_1.default.string().filter((v) => !Number.isNaN(Number(v))); | ||
const invalidValues = fast_check_1.default.string().filter((v) => Number.isNaN(Number(v))); | ||
it("detects things that can be cast to a number", () => { | ||
fast_check_1.default.assert(fast_check_1.default.property(validValues, (v) => expect(util_1.default.types.isNumber(v)).toStrictEqual(true))); | ||
}); | ||
it("detects things that cannot be cast to a number", () => { | ||
fast_check_1.default.assert(fast_check_1.default.property(invalidValues, (v) => expect(util_1.default.types.isNumber(v)).toStrictEqual(false))); | ||
}); | ||
it("returns a number when given a number", () => { | ||
fast_check_1.default.assert(fast_check_1.default.property(fast_check_1.default.float(), (v) => expect(util_1.default.types.toNumber(v)).toStrictEqual(v))); | ||
}); | ||
it("returns a number when given something that can be cast to number", () => { | ||
fast_check_1.default.assert(fast_check_1.default.property(validValues, (v) => expect(util_1.default.types.toNumber(v)).toStrictEqual(Number(v)))); | ||
}); | ||
it("throws when coercing non-number values", () => { | ||
fast_check_1.default.assert(fast_check_1.default.property(invalidValues, (v) => expect(() => util_1.default.types.toNumber(v)).toThrow("cannot be coerced to a number"))); | ||
}); | ||
it("returns the default value when a value is missing", () => { | ||
fast_check_1.default.assert(fast_check_1.default.property(unknowns(), (v) => expect(util_1.default.types.toNumber(v, 5.5)).toStrictEqual(5.5))); | ||
}); | ||
}); | ||
describe("bigint", () => { | ||
@@ -77,0 +115,0 @@ const invalidValues = fast_check_1.default.string().filter((v) => { |
{ | ||
"name": "@prismatic-io/spectral", | ||
"version": "2.0.0-beta.8", | ||
"version": "2.0.0-beta.9", | ||
"description": "Utility library for building Prismatic components", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
62816
1182