convertAttrs.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 'use strict'
  2. const defs = {
  3. archive: 'IS_ARCHIVED',
  4. hidden: 'IS_HIDDEN',
  5. readonly: 'IS_READ_ONLY',
  6. system: 'IS_SYSTEM',
  7. }
  8. function convert (attrs, from) {
  9. const output = {}
  10. eachAttribute(attrs, function (attrValue, attrName) {
  11. eachDefinition(function (defValue, defName) {
  12. if (from === true) {
  13. if (defValue === attrName) {
  14. output[defName] = attrValue
  15. return false
  16. }
  17. } else {
  18. if (defName === attrName) {
  19. output[defValue] = attrValue
  20. return false
  21. }
  22. }
  23. })
  24. })
  25. return output
  26. }
  27. function convertFrom (attrs) {
  28. return convert(attrs, true)
  29. }
  30. function convertTo (attrs) {
  31. return convert(attrs, false)
  32. }
  33. function eachAttribute (attrs, callback) {
  34. for (let i in attrs) {
  35. if (attrs.hasOwnProperty(i) === true) {
  36. let stop = callback(attrs[i], i, attrs)
  37. if (stop === false) break
  38. }
  39. }
  40. }
  41. function eachDefinition (callback) {
  42. for (let i in defs) {
  43. if (defs.hasOwnProperty(i) === true) {
  44. let stop = callback(defs[i], i, defs)
  45. if (stop === false) break
  46. }
  47. }
  48. }
  49. module.exports = {
  50. from: convertFrom,
  51. to: convertTo,
  52. }