You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

shared.js 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. "use strict";;
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. var types_1 = __importDefault(require("./types"));
  7. function default_1(fork) {
  8. var types = fork.use(types_1.default);
  9. var Type = types.Type;
  10. var builtin = types.builtInTypes;
  11. var isNumber = builtin.number;
  12. // An example of constructing a new type with arbitrary constraints from
  13. // an existing type.
  14. function geq(than) {
  15. return Type.from(function (value) { return isNumber.check(value) && value >= than; }, isNumber + " >= " + than);
  16. }
  17. ;
  18. // Default value-returning functions that may optionally be passed as a
  19. // third argument to Def.prototype.field.
  20. var defaults = {
  21. // Functions were used because (among other reasons) that's the most
  22. // elegant way to allow for the emptyArray one always to give a new
  23. // array instance.
  24. "null": function () { return null; },
  25. "emptyArray": function () { return []; },
  26. "false": function () { return false; },
  27. "true": function () { return true; },
  28. "undefined": function () { },
  29. "use strict": function () { return "use strict"; }
  30. };
  31. var naiveIsPrimitive = Type.or(builtin.string, builtin.number, builtin.boolean, builtin.null, builtin.undefined);
  32. var isPrimitive = Type.from(function (value) {
  33. if (value === null)
  34. return true;
  35. var type = typeof value;
  36. if (type === "object" ||
  37. type === "function") {
  38. return false;
  39. }
  40. return true;
  41. }, naiveIsPrimitive.toString());
  42. return {
  43. geq: geq,
  44. defaults: defaults,
  45. isPrimitive: isPrimitive,
  46. };
  47. }
  48. exports.default = default_1;
  49. module.exports = exports["default"];