infer-primitive.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /**
  2. * Module dependencies
  3. */
  4. var types = require('./types');
  5. /**
  6. * Infer the type of a primitive exemplar.
  7. * (note that while the inferred type schema is always a string,
  8. * this doesn't mean that it _represents a string type_)
  9. *
  10. * @param {JSON} eg [rttc exemplar (as a number, boolean, or string)]
  11. * @return {String} [type schema]
  12. */
  13. module.exports = function inferPrimitive(eg) {
  14. // Check for `type: 'ref'` (===)
  15. if (types.ref.isExemplar(eg)) {
  16. return 'ref';
  17. }
  18. // Check for `type: 'lamda'` (->)
  19. else if (types.lamda.isExemplar(eg)) {
  20. return 'lamda';
  21. }
  22. // Check for `type: 'json'` (*)
  23. else if (types.json.isExemplar(eg)){
  24. return 'json';
  25. }
  26. // Check for string
  27. else if (types.string.isExemplar(eg)) {
  28. return 'string';
  29. }
  30. // Check for number
  31. else if (types.number.isExemplar(eg)) {
  32. return 'number';
  33. }
  34. // Check for boolean
  35. else if (types.boolean.isExemplar(eg)) {
  36. return 'boolean';
  37. }
  38. // This return value of undefined means the inference failed.
  39. else {
  40. return;
  41. }
  42. };