index.js 937 B

1234567891011121314151617181920212223242526272829303132333435
  1. var prefix = function (name) {
  2. return '_' + name
  3. }
  4. var defined = function (name) {
  5. return name
  6. }
  7. exports.stringify = function (data) {
  8. if (typeof data === 'object' && data && data.name) return exports.stringify(data.name, data.protocol, data.subtypes)
  9. return Array.prototype.concat.apply([], arguments).filter(defined).map(prefix).join('.')
  10. }
  11. exports.parse = function (str) {
  12. var parts = str.split('.')
  13. for (var i = 0; i < parts.length; i++) {
  14. if (parts[i][0] !== '_') continue
  15. parts[i] = parts[i].slice(1)
  16. }
  17. return {
  18. name: parts.shift(),
  19. protocol: parts.shift() || null,
  20. subtypes: parts
  21. }
  22. }
  23. exports.tcp = function (name) {
  24. return exports.stringify(name, 'tcp', Array.prototype.concat.apply([], Array.prototype.slice.call(arguments, 1)))
  25. }
  26. exports.udp = function (name) {
  27. return exports.stringify(name, 'udp', Array.prototype.concat.apply([], Array.prototype.slice.call(arguments, 1)))
  28. }