Skip to content

Commit

Permalink
refactoring, docs
Browse files Browse the repository at this point in the history
  • Loading branch information
garvae committed Aug 11, 2022
1 parent 6cfc2d4 commit eeddbbc
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 37 deletions.
5 changes: 5 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.
3 changes: 3 additions & 0 deletions .idea/sonarlint/issuestore/index.pb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/svg-jest.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

103 changes: 66 additions & 37 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,78 @@
const path = require('path');

function buildModule(functionName, pathname, filename)
{
return `
const React = require('react');
/**
* This function build module.
*
* Changes by https://github.com/ChromeQ:
* - module.exports.default = ${functionName};
* + 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 "<svg viewBox="..."><path d="..."/></svg>"
* @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: functionName,
functionName: parts.base,
pathname: parts.name,
});
}

return contents;
}
};

module.exports =
{
process: processSvg
}
module.exports = { process: processSvg };

0 comments on commit eeddbbc

Please sign in to comment.