identity.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536
  1. const ALIAS = Symbol.for('yaml.alias');
  2. const DOC = Symbol.for('yaml.document');
  3. const MAP = Symbol.for('yaml.map');
  4. const PAIR = Symbol.for('yaml.pair');
  5. const SCALAR = Symbol.for('yaml.scalar');
  6. const SEQ = Symbol.for('yaml.seq');
  7. const NODE_TYPE = Symbol.for('yaml.node.type');
  8. const isAlias = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === ALIAS;
  9. const isDocument = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === DOC;
  10. const isMap = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === MAP;
  11. const isPair = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === PAIR;
  12. const isScalar = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === SCALAR;
  13. const isSeq = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === SEQ;
  14. function isCollection(node) {
  15. if (node && typeof node === 'object')
  16. switch (node[NODE_TYPE]) {
  17. case MAP:
  18. case SEQ:
  19. return true;
  20. }
  21. return false;
  22. }
  23. function isNode(node) {
  24. if (node && typeof node === 'object')
  25. switch (node[NODE_TYPE]) {
  26. case ALIAS:
  27. case MAP:
  28. case SCALAR:
  29. case SEQ:
  30. return true;
  31. }
  32. return false;
  33. }
  34. const hasAnchor = (node) => (isScalar(node) || isCollection(node)) && !!node.anchor;
  35. export { ALIAS, DOC, MAP, NODE_TYPE, PAIR, SCALAR, SEQ, hasAnchor, isAlias, isCollection, isDocument, isMap, isNode, isPair, isScalar, isSeq };