helpers.mjs131 lines · main
1import fs from 'fs'
2import path from 'path'
3
4/**
5 * Converts string to CamelCase
6 *
7 * @param {string} string
8 * @returns {string} A camelized string
9 */
10const toCamelCase = (string) =>
11 string.replace(/^([A-Z])|[\s-_]+(\w)/g, (match, p1, p2) =>
12 p2 ? p2.toUpperCase() : p1.toLowerCase()
13 )
14
15/**
16 * Converts string to PascalCase
17 *
18 * @param {string} string
19 * @returns {string} A pascalized string
20 */
21export const toPascalCase = (string) => {
22 const camelCase = toCamelCase(string)
23
24 return camelCase.charAt(0).toUpperCase() + camelCase.slice(1)
25}
26
27/**
28 * Converts string to KebabCase
29 *
30 * @param {string} string
31 * @returns {string} A kebabized string
32 */
33const toKebabCase = (string) => string.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase()
34
35/**
36 * Resets the file contents.
37 *
38 * @param {string} fileName
39 * @param {string} outputDirectory
40 */
41export const resetFile = (fileName, outputDirectory) =>
42 fs.writeFileSync(path.join(outputDirectory, fileName), '', 'utf-8')
43
44/**
45 * Reads the file contents.
46 *
47 * @param {string} path
48 * @returns {string} The contents of a file
49 */
50const readFile = (entry) => fs.readFileSync(path.resolve(__dirname, '../', entry), 'utf-8')
51
52/**
53 * append content to a file
54 *
55 * @param {string} content
56 * @param {string} fileName
57 * @param {string} outputDirectory
58 */
59export const appendFile = (content, fileName, outputDirectory) =>
60 fs.appendFileSync(path.join(outputDirectory, fileName), content, 'utf-8')
61
62/**
63 * writes content to a file
64 *
65 * @param {string} content
66 * @param {string} fileName
67 * @param {string} outputDirectory
68 */
69const writeFile = (content, fileName, outputDirectory) =>
70 fs.writeFileSync(path.join(outputDirectory, fileName), content, 'utf-8')
71
72/**
73 * reads the icon directory
74 *
75 * @param {string} directory
76 * @returns {array} An array of file paths containing svgs
77 */
78export const readSvgDirectory = (directory, fileExtension = '.svg') =>
79 fs.readdirSync(directory).filter((file) => path.extname(file) === fileExtension)
80
81/**
82 * Read svg from directory
83 *
84 * @param {string} fileName
85 * @param {string} directory
86 */
87export const readSvg = (fileName, directory) =>
88 fs.readFileSync(path.join(directory, fileName), 'utf-8')
89
90/**
91 * djb2 hashing function
92 *
93 * @param {string} string
94 * @param {number} seed
95 * @returns {string} A hashed string of 6 characters
96 */
97const hash = (string, seed = 5381) => {
98 let i = string.length
99
100 while (i) {
101 // eslint-disable-next-line no-bitwise, no-plusplus
102 seed = (seed * 33) ^ string.charCodeAt(--i)
103 }
104
105 // eslint-disable-next-line no-bitwise
106 return (seed >>> 0).toString(36).substr(0, 6)
107}
108
109/**
110 * Generate Hashed string based on name and attributes
111 *
112 * @param {object} seed
113 * @param {string} seed.name A name, for example an icon name
114 * @param {object} seed.attributes An object of SVGElement Attrbutes
115 * @returns {string} A hashed string of 6 characters
116 */
117export const generateHashedKey = ({ name, attributes }) => hash(JSON.stringify([name, attributes]))
118
119/**
120 * Checks if array of items contains duplicated items
121 *
122 * @param {array} children an array of items
123 * @returns {Boolean} if items contains duplicated items.
124 */
125export const hasDuplicatedChildren = (children) => {
126 const hashedKeys = children.map(generateHashedKey)
127
128 return !hashedKeys.every(
129 (key, index) => index === hashedKeys.findIndex((childKey) => childKey === key)
130 )
131}