Last active
June 25, 2020 03:15
-
-
Save max8hine/beced73ea6f82ead4c1d6a0304f6aa0a to your computer and use it in GitHub Desktop.
Helper Methods (Pho, Js)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A errorHandler function | |
* Handle Axios request error | |
* Handle Javascript Error instance | |
* Handle Javascript TypeError | |
* Handle Any error and return default error message | |
* @param {Object || Any} error | |
* @returns {String} | |
*/ | |
export const errorHandler = ( | |
error, | |
message = "An error occurred, please try again later" | |
) => { | |
if (!error) return message; | |
if (error.response) { | |
// The request was made and the server responded with a status code | |
// that falls out of the range of 2xx | |
console.info(error.response.data); | |
console.info(error.response.status); | |
console.info(error.response.headers); | |
if (error.response.data.error) { | |
if (typeof error.response.data.error === "string") { | |
message = error.response.data.error; | |
} | |
} | |
return message; | |
} | |
if (error.request) { | |
// The request was made but no response was received | |
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of | |
// http.ClientRequest in node.js | |
console.info(error.request); | |
return message; | |
} | |
// Something happened in setting up the request that triggered an Error | |
if (error.config) console.info(error.config); | |
if (error.message) { | |
console.info(error.message); | |
if (typeof error.message === "string") { | |
message = error.message; | |
} | |
} | |
return message; | |
}; | |
/** | |
* A Craft Date generator | |
* @returns {Object} | |
*/ | |
export const getCurrentDate = () => { | |
const now = new Date(); | |
let hour = now.getHours(); | |
let minute = now.getMinutes(); | |
let ap = "AM"; | |
if (hour > 11) ap = "PM"; | |
if (hour > 12) hour = hour - 12; | |
if (hour == 0) hour = 12; | |
if (hour < 10) hour = "0" + hour; | |
if (minute < 10) minute = "0" + minute; | |
return { | |
date: new Date().toLocaleDateString('en-GB'), | |
time: `${hour}:${minute} ${ap}`, | |
}; | |
}; | |
export isUrlImage = url => { | |
const validImageSuffix = new RegExp(/\.(jpeg|jpg|gif|png|svg)$/, ""); | |
return url.match(validImageSuffix) !== null | |
} | |
export is/** | |
* A errorHandler function | |
* Handle Axios request error | |
* Handle Javascript Error instance | |
* Handle Javascript TypeError | |
* Handle Any error and return default error message | |
* @param {Object || Any} error | |
* @returns {String} | |
*/ | |
export const errorHandler = ( | |
error, | |
message = "An error occurred, please try again later" | |
) => { | |
if (!error) return message; | |
if (error.response) { | |
// The request was made and the server responded with a status code | |
// that falls out of the range of 2xx | |
console.info(error.response.data); | |
console.info(error.response.status); | |
console.info(error.response.headers); | |
if (error.response.data.error) { | |
if (typeof error.response.data.error === "string") { | |
message = error.response.data.error; | |
} | |
} | |
return message; | |
} | |
if (error.request) { | |
// The request was made but no response was received | |
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of | |
// http.ClientRequest in node.js | |
console.info(error.request); | |
return message; | |
} | |
// Something happened in setting up the request that triggered an Error | |
if (error.config) console.info(error.config); | |
if (error.message) { | |
console.info(error.message); | |
if (typeof error.message === "string") { | |
message = error.message; | |
} | |
} | |
return message; | |
}; | |
/** | |
* A Craft Date generator | |
* @returns {Object} | |
*/ | |
export const getCurrentDate = () => { | |
const now = new Date(); | |
let hour = now.getHours(); | |
let minute = now.getMinutes(); | |
let ap = "AM"; | |
if (hour > 11) ap = "PM"; | |
if (hour > 12) hour = hour - 12; | |
if (hour == 0) hour = 12; | |
if (hour < 10) hour = "0" + hour; | |
if (minute < 10) minute = "0" + minute; | |
return { | |
date: new Date().toLocaleDateString('en-GB'), | |
time: `${hour}:${minute} ${ap}`, | |
}; | |
}; | |
// replaceUrlSuffixTo('.png')('anything.jpg') => 'anything.png' | |
export const replaceUrlSuffixTo = suffixWithDot => filePath => filePath.replace(/\.[\w]+$/, suffixWithDot) | |
export const isUrlImage = url => { | |
const validImageSuffix = new RegExp(/\.(jpeg|jpg|gif|png|svg)$/, ""); | |
return url.match(validImageSuffix) !== null | |
} | |
export const isInputFileImage = file => { | |
const validImageTypes = ['image/gif', 'image/jpeg', 'image/png', 'image/svg+xml']); | |
const fileType = file['type'] | |
return validImageTypes.includes(fileType) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function dumpObject($object) { | |
$class_name = get_class($object); | |
$methods = get_class_methods($class_name); | |
foreach($methods as $method) | |
{ | |
var_dump($method); | |
echo "<br>"; | |
}; | |
} | |
if( $image ){ | |
print_r($image->getUrl()); | |
print_r($image->extension); die(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
use craft\web\twig\Extension;
$view = Craft::$app->getView();
$twigExtension = new Extension($view, $view->getTwig());
$twigExtension->svgFunction($image)