prefix-umd.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. (function (factory) {
  2. if (typeof define === 'function' && define.amd) {
  3. // AMD. Register as an anonymous module.
  4. define([], factory);
  5. } else if (typeof module === 'object' && module.exports) {
  6. // Node/CommonJS
  7. module.exports = factory(require());
  8. } else {
  9. // Browser globals
  10. window.Prefix = factory();
  11. }
  12. }(function () {
  13. var style = typeof document != 'undefined'
  14. ? document.createElement('p').style
  15. : {}
  16. var prefixes = ['O', 'ms', 'Moz', 'Webkit']
  17. var upper = /([A-Z])/g
  18. var memo = {}
  19. /**
  20. * prefix `key`
  21. * prefix('transform') // => WebkitTransform
  22. *
  23. * @param {String} key
  24. * @return {String}
  25. * @api public
  26. */
  27. function prefix(key){
  28. // Camel case
  29. key = key.replace(/-([a-z])/g, function(_, char){
  30. return char.toUpperCase()
  31. })
  32. // Without prefix
  33. if (style[key] !== undefined) return key
  34. // With prefix
  35. var Key = key.charAt(0).toUpperCase() + key.slice(1)
  36. var i = prefixes.length
  37. while (i--) {
  38. var name = prefixes[i] + Key
  39. if (style[name] !== undefined) return name
  40. }
  41. return key
  42. }
  43. /**
  44. * Memoized version of `prefix`
  45. *
  46. * @param {String} key
  47. * @return {String}
  48. * @api public
  49. */
  50. function prefixMemozied(key){
  51. return key in memo
  52. ? memo[key]
  53. : memo[key] = prefix(key)
  54. }
  55. /**
  56. * Create a dashed prefix
  57. *
  58. * @param {String} key
  59. * @return {String}
  60. * @api public
  61. */
  62. function prefixDashed(key){
  63. key = prefix(key)
  64. if (upper.test(key)) {
  65. key = key.replace(upper, '-$1')
  66. upper.lastIndex = 0
  67. }
  68. return key.toLowerCase()
  69. }
  70. return {
  71. prefix : prefixMemozied,
  72. dash : prefixDashed
  73. }
  74. }));