index.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import filesize from 'filesize.js'
  2. /* global siteConfig */
  3. const _ = require('./lodash')
  4. const helpers = {
  5. /**
  6. * Minimal set of lodash functions
  7. */
  8. _,
  9. /**
  10. * Convert bytes to humanized form
  11. * @param {number} rawSize Size in bytes
  12. * @returns {string} Humanized file size
  13. */
  14. filesize (rawSize) {
  15. return _.toUpper(filesize(rawSize))
  16. },
  17. /**
  18. * Convert raw path to safe path
  19. * @param {string} rawPath Raw path
  20. * @returns {string} Safe path
  21. */
  22. makeSafePath (rawPath) {
  23. let rawParts = _.split(_.trim(rawPath), '/')
  24. rawParts = _.map(rawParts, (r) => {
  25. return _.kebabCase(_.deburr(_.trim(r)))
  26. })
  27. return _.join(_.filter(rawParts, (r) => { return !_.isEmpty(r) }), '/')
  28. },
  29. resolvePath (path) {
  30. if (_.startsWith(path, '/')) { path = path.substring(1) }
  31. return `${siteConfig.path}${path}`
  32. },
  33. /**
  34. * Set Input Selection
  35. * @param {DOMElement} input The input element
  36. * @param {number} startPos The starting position
  37. * @param {nunber} endPos The ending position
  38. */
  39. setInputSelection (input, startPos, endPos) {
  40. input.focus()
  41. if (typeof input.selectionStart !== 'undefined') {
  42. input.selectionStart = startPos
  43. input.selectionEnd = endPos
  44. } else if (document.selection && document.selection.createRange) {
  45. // IE branch
  46. input.select()
  47. var range = document.selection.createRange()
  48. range.collapse(true)
  49. range.moveEnd('character', endPos)
  50. range.moveStart('character', startPos)
  51. range.select()
  52. }
  53. }
  54. }
  55. export default {
  56. install(Vue) {
  57. Vue.$helpers = helpers
  58. Object.defineProperties(Vue.prototype, {
  59. $helpers: {
  60. get() {
  61. return helpers
  62. }
  63. }
  64. })
  65. }
  66. }