identity.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. 'use strict';
  2. const ALIAS = Symbol.for('yaml.alias');
  3. const DOC = Symbol.for('yaml.document');
  4. const MAP = Symbol.for('yaml.map');
  5. const PAIR = Symbol.for('yaml.pair');
  6. const SCALAR = Symbol.for('yaml.scalar');
  7. const SEQ = Symbol.for('yaml.seq');
  8. const NODE_TYPE = Symbol.for('yaml.node.type');
  9. const isAlias = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === ALIAS;
  10. const isDocument = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === DOC;
  11. const isMap = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === MAP;
  12. const isPair = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === PAIR;
  13. const isScalar = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === SCALAR;
  14. const isSeq = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === SEQ;
  15. function isCollection(node) {
  16. if (node && typeof node === 'object')
  17. switch (node[NODE_TYPE]) {
  18. case MAP:
  19. case SEQ:
  20. return true;
  21. }
  22. return false;
  23. }
  24. function isNode(node) {
  25. if (node && typeof node === 'object')
  26. switch (node[NODE_TYPE]) {
  27. case ALIAS:
  28. case MAP:
  29. case SCALAR:
  30. case SEQ:
  31. return true;
  32. }
  33. return false;
  34. }
  35. const hasAnchor = (node) => (isScalar(node) || isCollection(node)) && !!node.anchor;
  36. exports.ALIAS = ALIAS;
  37. exports.DOC = DOC;
  38. exports.MAP = MAP;
  39. exports.NODE_TYPE = NODE_TYPE;
  40. exports.PAIR = PAIR;
  41. exports.SCALAR = SCALAR;
  42. exports.SEQ = SEQ;
  43. exports.hasAnchor = hasAnchor;
  44. exports.isAlias = isAlias;
  45. exports.isCollection = isCollection;
  46. exports.isDocument = isDocument;
  47. exports.isMap = isMap;
  48. exports.isNode = isNode;
  49. exports.isPair = isPair;
  50. exports.isScalar = isScalar;
  51. exports.isSeq = isSeq;