getKeysInSchemaOrder.js 621 B

12345678910111213141516171819202122232425262728
  1. 'use strict';
  2. const get = require('../get');
  3. module.exports = function getKeysInSchemaOrder(schema, val, path) {
  4. const schemaKeys = path != null ? Object.keys(get(schema.tree, path, {})) : Object.keys(schema.tree);
  5. const valKeys = new Set(Object.keys(val));
  6. let keys;
  7. if (valKeys.size > 1) {
  8. keys = new Set();
  9. for (const key of schemaKeys) {
  10. if (valKeys.has(key)) {
  11. keys.add(key);
  12. }
  13. }
  14. for (const key of valKeys) {
  15. if (!keys.has(key)) {
  16. keys.add(key);
  17. }
  18. }
  19. keys = Array.from(keys);
  20. } else {
  21. keys = Array.from(valKeys);
  22. }
  23. return keys;
  24. };