diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..b58b603 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,5 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..b63b642 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,11 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..461817e --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/sonarlint/issuestore/8/e/8ec9a00bfd09b3190ac6b22251dbb1aa95a0579d b/.idea/sonarlint/issuestore/8/e/8ec9a00bfd09b3190ac6b22251dbb1aa95a0579d new file mode 100644 index 0000000..e69de29 diff --git a/.idea/sonarlint/issuestore/index.pb b/.idea/sonarlint/issuestore/index.pb new file mode 100644 index 0000000..cf934fa --- /dev/null +++ b/.idea/sonarlint/issuestore/index.pb @@ -0,0 +1,3 @@ + +9 + README.md,8\e\8ec9a00bfd09b3190ac6b22251dbb1aa95a0579d \ No newline at end of file diff --git a/.idea/svg-jest.iml b/.idea/svg-jest.iml new file mode 100644 index 0000000..0c8867d --- /dev/null +++ b/.idea/svg-jest.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/index.js b/index.js index 20167b5..4b465ea 100644 --- a/index.js +++ b/index.js @@ -1,49 +1,81 @@ const path = require('path'); -function buildModule(functionName, pathname, filename) -{ - return ` -const React = require('react'); +/** + * This function build module. + * + * P.S.: + * The string "module.exports.default = ${functionName};" + * replaced with (by https://github.com/ChromeQ): + * ``` + * module.exports.__esModule = true; + * module.exports.default = '${pathname}'; + * ``` + * + * Object props is a more extensible solution when we have a lot of props + * @param {Object} props + * @param {string} props.functionName + * @param {string} props.pathname + * @param {string} props.filename + * + * @function {(functionName, pathname, filename) => string} buildModule + * @param {string} props + * @return {string} string module + */ +const buildModule = props => { -const ${functionName} = (props) => -{ - return React.createElement('svg', { - ...props, - 'data-jest-file-name': '${pathname}', - 'data-jest-svg-name': '${filename}', - 'data-testid': '${filename}' - }); -} + const { + filename, + functionName, + pathname, + } = props; -module.exports.default = ${functionName}; -module.exports.ReactComponent = ${functionName}; -`; -} + return ` + const React = require('react'); + const ${functionName} = (props) => + { + return React.createElement('svg', { + ...props, + 'data-jest-file-name': '${pathname}', + 'data-jest-svg-name': '${filename}', + 'data-testid': '${filename}' + }); + } + module.exports.__esModule = true; + module.exports.default = '${pathname}'; + module.exports.ReactComponent = ${functionName}; +`;}; -function createFunctionName(base) -{ +/** + * This function creates a function name + * @function {(base) => string} createFunctionName + * @param {string} base + * @return {string} string module + */ +const createFunctionName = base => { const words = base.split(/\W+/); - return words.reduce( - (identifer, word) => { - return identifer + - word.substr(0, 1).toUpperCase() + - word.substr(1); - }, ''); -} + /* here I refactored the code a bit and replaced "substr" (Deprecated) with "substring" */ + return words.reduce((identifier, word) => identifier + word.substring(0, 1).toUpperCase() + word.substring(1), ''); +}; -function processSvg(contents, filename) -{ +/** + * This function process incoming svg data + * @function {(contents, filename) => string} processSvg + * @param {string} contents - your svg. String like "" + * @param {string} filename - full path of your file + * @return {string} string module + */ +const processSvg = (contents, filename) => { const parts = path.parse(filename); - if (parts.ext.toLowerCase() === '.svg') - { + if (parts.ext.toLowerCase() === '.svg') { const functionName = createFunctionName(parts.name); - return buildModule(functionName, parts.base, parts.name); + return buildModule({ + filename: parts.name, + functionName, + pathname: parts.base, + }); } - + return contents; -} +}; -module.exports = -{ - process: processSvg -} +module.exports = { process: processSvg };