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