generic-type-guard
Version:
Generic type guards for TypeScript
539 lines (532 loc) • 13.8 kB
JavaScript
// dist/utils.js
var __extends = /* @__PURE__ */ function() {
var extendStatics = function(d, b) {
extendStatics = Object.setPrototypeOf || { __proto__: [] } instanceof Array && function(d2, b2) {
d2.__proto__ = b2;
} || function(d2, b2) {
for (var p in b2) if (Object.prototype.hasOwnProperty.call(b2, p)) d2[p] = b2[p];
};
return extendStatics(d, b);
};
return function(d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() {
this.constructor = d;
}
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
}();
var __values = function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function() {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var AssertionError = (
/** @class */
function(_super) {
__extends(AssertionError2, _super);
function AssertionError2(value, message) {
var _this = _super.call(this, message) || this;
_this.value = value;
_this.name = _this.constructor.name;
return _this;
}
return AssertionError2;
}(RangeError)
);
var assert = function(value, guard, message) {
if (!guard(value)) {
throw new AssertionError(value, message !== null && message !== void 0 ? message : "Invalid value provided: ".concat(JSON.stringify(value)));
}
};
var combine = function() {
var guards = [];
for (var _i = 0; _i < arguments.length; _i++) {
guards[_i] = arguments[_i];
}
return function(v) {
var e_1, _a;
try {
for (var guards_1 = __values(guards), guards_1_1 = guards_1.next(); !guards_1_1.done; guards_1_1 = guards_1.next()) {
var guard = guards_1_1.value;
if (!guard(v)) {
return false;
}
}
} catch (e_1_1) {
e_1 = { error: e_1_1 };
} finally {
try {
if (guards_1_1 && !guards_1_1.done && (_a = guards_1.return)) _a.call(guards_1);
} finally {
if (e_1) throw e_1.error;
}
}
return true;
};
};
// dist/primitives.js
var __read = function(o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
} catch (error) {
e = { error };
} finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
} finally {
if (e) throw e.error;
}
}
return ar;
};
var __spreadArray = function(to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var MINIMUM_ARRAY_INDEX = 0;
var isNumber = function(n) {
return typeof n === "number" && !isNaN(n);
};
var isFiniteNumber = function(n) {
return typeof n === "number" && !isNaN(n) && isFinite(n);
};
var isFloat = function(n) {
return typeof n === "number";
};
var isDouble = isFloat;
var isInfinity = function(n) {
return typeof n === "number" && !isNaN(n) && !isFinite(n);
};
var _isNaN = function(n) {
return typeof n === "number" && isNaN(n);
};
var isElementOf = function() {
var ss = [];
for (var _i = 0; _i < arguments.length; _i++) {
ss[_i] = arguments[_i];
}
return function(s) {
return ss.indexOf(s) >= MINIMUM_ARRAY_INDEX;
};
};
var isSingletonNumber = function(v) {
return function(n) {
return n === v;
};
};
var isSingletonNumberUnion = function() {
var ss = [];
for (var _i = 0; _i < arguments.length; _i++) {
ss[_i] = arguments[_i];
}
return function(s) {
return ss.indexOf(s) >= MINIMUM_ARRAY_INDEX;
};
};
var isString = function(s) {
return typeof s === "string";
};
var isSingletonString = function(v) {
return function(s) {
return s === v;
};
};
var isSingletonStringUnion = function() {
var ss = [];
for (var _i = 0; _i < arguments.length; _i++) {
ss[_i] = arguments[_i];
}
return function(s) {
return ss.indexOf(s) >= MINIMUM_ARRAY_INDEX;
};
};
var isBoolean = function(b) {
return typeof b === "boolean";
};
var isNull = function(o) {
return o === null;
};
var isUndefined = function(u) {
return typeof u === "undefined";
};
var isOptional = function(tgt) {
return function(o) {
return typeof o === "undefined" || tgt(o);
};
};
var isNullable = function(tgt) {
return function(o) {
return o === null || tgt(o);
};
};
var isMissing = function(tgt) {
return function(o) {
return o == null || tgt(o);
};
};
var isArray = function(valueCheck) {
return function(arr) {
return Array.isArray(arr) && arr.reduce(function(acc, v) {
return acc && valueCheck(v);
}, true);
};
};
var isNotEmptyList = function(valueCheck) {
return function(arr) {
return Array.isArray(arr) && arr.length >= 1 && arr.reduce(function(acc, v) {
return acc && valueCheck(v);
}, true);
};
};
var narrowValue = function(ptt, ptu) {
return combine(ptt, ptu);
};
var narrowArray = function(pt) {
return function(ts) {
return ts.reduce(function(acc, b) {
return acc && pt(b);
}, true);
};
};
var isSetOf = function(tg) {
return function(o) {
return o instanceof Set && Array.of.apply(Array, __spreadArray([], __read(o.values()), false)).reduce(function(acc, v) {
return acc && tg(v);
}, true);
};
};
var isObjectLike = function(obj) {
return obj != null && typeof obj === "object";
};
var isObject = function(obj) {
return obj != null && typeof obj === "object" && !(obj instanceof Array);
};
var isSet = function(obj) {
return obj != null;
};
var isNumericalEnumeration = function(e, flags) {
if (flags === void 0) {
flags = false;
}
var options = Object.values(e).filter(isNumber);
if (!flags) {
return function(obj) {
return options.includes(obj);
};
} else {
return function(obj) {
return typeof obj === "number" && obj !== 0 && options.filter(function(v) {
return (v & obj) === v;
}).reduce(function(acc, v) {
return acc | v;
}, 0) === obj;
};
}
};
var isStringEnumeration = function(e) {
var options = Object.values(e);
return function(obj) {
return options.includes(obj);
};
};
var isAny = function(_a) {
return true;
};
var isUnknown = isAny;
var isNever = function(n) {
throw Error("Unexpected value when expecting never: ".concat(n));
};
// dist/objects.js
var hasProperty = function(property, value) {
return function(o) {
return value(o[property]);
};
};
var hasOptionalProperty = function(property, value) {
return function(o) {
return !(property in o) || value(o[property]);
};
};
var isRecord = function(property, value) {
return function(o) {
return isObject(o) && hasProperty(property, value)(o);
};
};
var hasStringIndexSignature = function(value, enforce) {
if (enforce === void 0) {
enforce = true;
}
return function(o) {
var n = 0;
for (var prop in o) {
if (isNaN(parseInt(prop, 10))) {
if (value(o[prop])) {
n++;
} else {
return false;
}
}
}
return !enforce || n > 0;
};
};
var hasNumericIndexSignature = function(value, enforce) {
if (enforce === void 0) {
enforce = true;
}
return function(o) {
var n = 0;
for (var prop in o) {
if (!isNaN(parseInt(prop, 10))) {
if (value(o[prop])) {
n++;
} else {
return false;
}
}
}
return !enforce || n > 0;
};
};
var isInstance = function(klass) {
return function(o) {
return o instanceof klass;
};
};
var hasProperties = function(props) {
return function(o) {
for (var prop in props) {
if (!hasProperty(prop, props[prop])(o)) {
return false;
}
}
return true;
};
};
var hasOnlyProperties = function(props) {
return function(o) {
var found = [];
for (var prop in o) {
if (prop in props) {
var propsKey = prop;
if (!hasProperty(propsKey, props[propsKey])(o)) {
return false;
}
found.push(propsKey);
} else {
return false;
}
}
return found.length === Object.keys(props).length;
};
};
var hasOptionalProperties = function(props) {
return function(o) {
for (var prop in props) {
if (!hasOptionalProperty(prop, props[prop])(o)) {
return false;
}
}
return true;
};
};
var isLikeObject = function(props) {
return combine(isObject, hasProperties(props));
};
var isExactObject = function(props) {
return combine(isObject, hasOnlyProperties(props));
};
// dist/combinators/functions.js
var isUnion = function(ptt, ptu) {
return function(o) {
return ptt(o) || ptu(o);
};
};
var isIntersection = function(ptt, ptu) {
return function(o) {
return ptt(o) && ptu(o);
};
};
// dist/combinators/unionof.js
var UnionOf = (
/** @class */
function() {
function UnionOf2(ptt) {
this.ptt = ptt;
}
UnionOf2.prototype.get = function() {
return this.ptt;
};
UnionOf2.prototype.with = function(ptv) {
return new UnionOf2(isUnion(this.ptt, ptv));
};
return UnionOf2;
}()
);
// dist/combinators/intersectionof.js
var IntersectionOf = (
/** @class */
function() {
function IntersectionOf2(ptt) {
this.ptt = ptt;
}
IntersectionOf2.prototype.get = function() {
return this.ptt;
};
IntersectionOf2.prototype.with = function(ptu) {
return new IntersectionOf2(isIntersection(this.ptt, ptu));
};
return IntersectionOf2;
}()
);
// dist/combinators/interface.js
var InterfaceStep = (
/** @class */
function() {
function InterfaceStep2(ptt) {
this.ptt = ptt;
}
InterfaceStep2.prototype.get = function() {
var _this = this;
return function(obj) {
return isObjectLike(obj) && _this.ptt(obj);
};
};
InterfaceStep2.prototype.with = function(ptv) {
return new InterfaceStep2(isIntersection(this.ptt, ptv));
};
InterfaceStep2.prototype.withProperty = function(key, ptv) {
return new InterfaceStep2(isIntersection(this.ptt, hasProperty(key, ptv)));
};
InterfaceStep2.prototype.withOptionalProperty = function(key, ptv) {
return new InterfaceStep2(isIntersection(this.ptt, hasOptionalProperty(key, ptv)));
};
InterfaceStep2.prototype.withStringIndexSignature = function(value, enforce) {
if (enforce === void 0) {
enforce = true;
}
return new InterfaceStep2(isIntersection(this.ptt, hasStringIndexSignature(value, enforce)));
};
InterfaceStep2.prototype.withNumericIndexSignature = function(value, enforce) {
if (enforce === void 0) {
enforce = true;
}
return new InterfaceStep2(isIntersection(this.ptt, hasNumericIndexSignature(value, enforce)));
};
InterfaceStep2.prototype.withProperties = function(props) {
return new InterfaceStep2(isIntersection(this.ptt, hasProperties(props)));
};
InterfaceStep2.prototype.withOptionalProperties = function(props) {
return new InterfaceStep2(isIntersection(this.ptt, hasOptionalProperties(props)));
};
return InterfaceStep2;
}()
);
var IsInterface = (
/** @class */
function() {
function IsInterface2() {
}
IsInterface2.prototype.get = function() {
return isObjectLike;
};
IsInterface2.prototype.with = function(ptv) {
return new InterfaceStep(ptv);
};
IsInterface2.prototype.withProperty = function(key, ptv) {
return new InterfaceStep(hasProperty(key, ptv));
};
IsInterface2.prototype.withOptionalProperty = function(key, ptv) {
return new InterfaceStep(hasOptionalProperty(key, ptv));
};
IsInterface2.prototype.withStringIndexSignature = function(value, enforce) {
if (enforce === void 0) {
enforce = true;
}
return new InterfaceStep(hasStringIndexSignature(value, enforce));
};
IsInterface2.prototype.withNumericIndexSignature = function(value, enforce) {
if (enforce === void 0) {
enforce = true;
}
return new InterfaceStep(hasNumericIndexSignature(value, enforce));
};
IsInterface2.prototype.withProperties = function(props) {
return new InterfaceStep(hasProperties(props));
};
IsInterface2.prototype.withOptionalProperties = function(props) {
return new InterfaceStep(hasOptionalProperties(props));
};
return IsInterface2;
}()
);
export {
AssertionError,
IntersectionOf,
IsInterface,
UnionOf,
assert,
combine,
hasNumericIndexSignature,
hasOnlyProperties,
hasOptionalProperties,
hasOptionalProperty,
hasProperties,
hasProperty,
hasStringIndexSignature,
isAny,
isArray,
isBoolean,
isDouble,
isElementOf,
isExactObject,
isFiniteNumber,
isFloat,
isInfinity,
isInstance,
isIntersection,
isLikeObject,
isMissing,
_isNaN as isNaN,
isNever,
isNotEmptyList,
isNull,
isNullable,
isNumber,
isNumericalEnumeration,
isObject,
isObjectLike,
isOptional,
isRecord,
isSet,
isSetOf,
isSingletonNumber,
isSingletonNumberUnion,
isSingletonString,
isSingletonStringUnion,
isString,
isStringEnumeration,
isUndefined,
isUnion,
isUnknown,
narrowArray,
narrowValue
};
//# sourceMappingURL=generic-type-guard.js.map