Нема описа

map.js 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict';
  2. var Node = require('../../nodes/Node.js');
  3. var Pair = require('../../nodes/Pair.js');
  4. var YAMLMap = require('../../nodes/YAMLMap.js');
  5. function createMap(schema, obj, ctx) {
  6. const { keepUndefined, replacer } = ctx;
  7. const map = new YAMLMap.YAMLMap(schema);
  8. const add = (key, value) => {
  9. if (typeof replacer === 'function')
  10. value = replacer.call(obj, key, value);
  11. else if (Array.isArray(replacer) && !replacer.includes(key))
  12. return;
  13. if (value !== undefined || keepUndefined)
  14. map.items.push(Pair.createPair(key, value, ctx));
  15. };
  16. if (obj instanceof Map) {
  17. for (const [key, value] of obj)
  18. add(key, value);
  19. }
  20. else if (obj && typeof obj === 'object') {
  21. for (const key of Object.keys(obj))
  22. add(key, obj[key]);
  23. }
  24. if (typeof schema.sortMapEntries === 'function') {
  25. map.items.sort(schema.sortMapEntries);
  26. }
  27. return map;
  28. }
  29. const map = {
  30. collection: 'map',
  31. createNode: createMap,
  32. default: true,
  33. nodeClass: YAMLMap.YAMLMap,
  34. tag: 'tag:yaml.org,2002:map',
  35. resolve(map, onError) {
  36. if (!Node.isMap(map))
  37. onError('Expected a mapping for this tag');
  38. return map;
  39. }
  40. };
  41. exports.map = map;