Skip to content

Commit

Permalink
feat: add prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
0529bill committed Jul 8, 2022
1 parent 78d5ced commit 95ea93f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 69 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# dependencies
/node_modules
/.pnp
/dist
.pnp.js

# testing
Expand Down
8 changes: 8 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"bracketSpacing": true,
"printWidth": 100,
"semi": false,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "es5"
}
115 changes: 46 additions & 69 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,130 +1,107 @@
const handleValidation = ({
errorArray = [],
dataSource = [],
defaultErrorMessage = "",
}) => {
let isPass = true;
const errorValidation = {};
const defaultMessage = defaultErrorMessage
? { msg: defaultErrorMessage }
: "";
const handleValidation = ({ errorArray = [], dataSource = [], defaultErrorMessage = '' }) => {
let isPass = true
const errorValidation = {}
const defaultMessage = defaultErrorMessage ? { msg: defaultErrorMessage } : ''

// 接受 condition 為 [ condition1, condition2]
const handleValidateItem = (errorItem) => {
if (errorItem.condition.every((error) => error !== false)) {
return false;
return false
}
return true;
};
return true
}

const setNestedErrorMessage = (
tempErrorObject,
currentIndex,
errorFormat,
errorResult
) => {
const format = errorFormat.shift();
const targetIndex = format === "index" ? currentIndex : format;
const setNestedErrorMessage = (tempErrorObject, currentIndex, errorFormat, errorResult) => {
const format = errorFormat.shift()
const targetIndex = format === 'index' ? currentIndex : format
if (errorFormat.length === 0) {
tempErrorObject[targetIndex] = errorResult;
return;
tempErrorObject[targetIndex] = errorResult
return
}
tempErrorObject[targetIndex] = {};
setNestedErrorMessage(
tempErrorObject[targetIndex],
currentIndex,
errorFormat,
errorResult
);
};
tempErrorObject[targetIndex] = {}
setNestedErrorMessage(tempErrorObject[targetIndex], currentIndex, errorFormat, errorResult)
}

errorArray.forEach((item) => {
if (!Array.isArray(item?.condition) && !item?.customCondition) {
return console.error(
`Invalid type \`${typeof !item?.condition}"\` supplied to parameter "condition", expected \`array\`;
type \`${typeof !item?.customCondition}"\` supplied to parameter "customCondition", expected \`function\
`
);
)
}

if (typeof item?.index !== "string") {
if (typeof item?.index !== 'string') {
// eslint-disable-next-line no-console
return console.error(
`Invalid type \`${typeof item?.index}"\` supplied to parameter "index", expected \`string\`!`
);
)
}

// 有傳customValidate的情況
if (item?.customCondition) {
errorValidation[item.index] = [];
const conditionReturnArray = [];
errorValidation[item.index] = []
const conditionReturnArray = []

// conditionReturnArray ex. [true, true, false, false, true]
item.customCondition(dataSource, conditionReturnArray);
item.customCondition(dataSource, conditionReturnArray)
if (!Array.isArray(conditionReturnArray)) {
// eslint-disable-next-line no-console
return console.error(
`Invalid type \`${typeof conditionReturnArray}"\` return from parameter "customValidation", expected \`array\`!`
);
)
}
if (conditionReturnArray.length) {
const multiple = conditionReturnArray.length > 1;
let returnObject = multiple ? [] : null;
const multiple = conditionReturnArray.length > 1
let returnObject = multiple ? [] : null
// 輸出errorValidation 錯誤的值用error message 正確的值設定為null ex. [{msg: Message}, null, {msg: Message}]
conditionReturnArray.forEach((result, index) => {
let value = true;
let value = true
if (!result) {
value = false;
isPass = false;
value = false
isPass = false
}

const tempErrorObject = {};
let targetTempErrorObject;
const tempErrorObject = {}
let targetTempErrorObject

const errorResult = value
? null
: item?.errorMessage
? { msg: item?.errorMessage }
: defaultMessage;
: defaultMessage

if (item?.errorFormat && errorResult) {
const tempErrorFormat = item?.errorFormat.slice();
setNestedErrorMessage(
tempErrorObject,
index,
tempErrorFormat,
errorResult
);
const tempErrorFormat = item?.errorFormat.slice()
setNestedErrorMessage(tempErrorObject, index, tempErrorFormat, errorResult)
targetTempErrorObject =
Object.keys(tempErrorObject).length === 0
? null
: tempErrorObject;
Object.keys(tempErrorObject).length === 0 ? null : tempErrorObject

if (multiple) {
returnObject.push(targetTempErrorObject);
returnObject.push(targetTempErrorObject)
} else {
returnObject = targetTempErrorObject;
returnObject = targetTempErrorObject
}
} else if (multiple) {
returnObject.push(errorResult);
returnObject.push(errorResult)
} else {
returnObject = errorResult;
returnObject = errorResult
}
});
errorValidation[item.index] = returnObject;
})
errorValidation[item.index] = returnObject
// errorValidation[item.index][index] = errorResult;
}
} else {
const isValidated = handleValidateItem(item);
const isValidated = handleValidateItem(item)
if (!isValidated) {
errorValidation[item.index] = item?.errorMessage
? { msg: item?.errorMessage }
: defaultMessage;
isPass = false;
: defaultMessage
isPass = false
}
}
});
return [isPass, errorValidation];
};
})
return [isPass, errorValidation]
}

export default handleValidation;
export default handleValidation

0 comments on commit 95ea93f

Please sign in to comment.