helpers.ts 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import * as fs from 'fs'
  2. import * as _ from 'lodash'
  3. export const slugify = (text: string) => {
  4. return text
  5. .toString()
  6. .toLowerCase()
  7. .replace(/[. )(]/g, '-') // Replace spaces and brackets -
  8. .replace(/[^\w\-]+/g, '') // Remove all non-word chars
  9. .replace(/\-\-+/g, '-') // Replace multiple - with single -
  10. .replace(/^-+/, '') // Trim - from start of text
  11. .replace(/-+$/, '') // Trim - from end of text
  12. }
  13. // Uppercase the first letter of a string
  14. export const toTitle = (text: string) => {
  15. return text.charAt(0).toUpperCase() + text.slice(1)
  16. }
  17. /**
  18. * writeToDisk()
  19. */
  20. export const writeToDisk = (fileName: string, content: any) => {
  21. return new Promise((resolve, reject) => {
  22. fs.writeFile(fileName, content, (err: any) => {
  23. if (err) return reject(err)
  24. else return resolve(true)
  25. })
  26. })
  27. }
  28. /**
  29. * Convert Object to Array of values
  30. */
  31. export const toArrayWithKey = (obj: object, keyAs: string) =>
  32. _.values(
  33. _.mapValues(obj, (value: any, key: string) => {
  34. value[keyAs] = key
  35. return value
  36. })
  37. )