Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactoring, docs #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

106 changes: 69 additions & 37 deletions index.js
Original file line number Diff line number Diff line change
@@ -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 "<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: parts.name,
functionName,
pathname: parts.base,
});
}

return contents;
}
};

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