@developer.nijat/js-helpers
Advanced tools
Comparing version 1.0.1 to 1.0.2
{ | ||
"name": "@developer.nijat/js-helpers", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Useful JavaScript Methods by Nijat Aliyev", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Useful JavaScript Methods by Nijat Aliyev | ||
## COMMON | ||
`getFileSize | ||
htmlDecode | ||
htmlEncode | ||
getRandomNumber | ||
uniqueNumberFromString | ||
normalizeGraphFilterForExpress | ||
normalizeAzPhoneNumber | ||
normalizeAz9DigitNumber | ||
extractNumberFromTime | ||
calculatePercentageOfNumber | ||
generateRandomColor | ||
normalizeSearchParams` | ||
## DATE | ||
`convertMillisToMinutesAndSeconds | ||
calculateDurationWithStartDate | ||
convertDateToMilliSeconds | ||
convertTime12to24 | ||
convertSecondsToTime` | ||
## VALIDATION | ||
`validateEmail | ||
validateNumber | ||
isJsonString` |
import moment from "moment"; | ||
export const getFileSize = (_size) => { | ||
var fSExt = new Array("Bytes", "KB", "MB", "GB"), | ||
i = 0; | ||
while (_size > 900) { | ||
_size /= 1024; | ||
i++; | ||
try { | ||
var fSExt = new Array("Bytes", "KB", "MB", "GB"), | ||
i = 0; | ||
while (_size > 900) { | ||
_size /= 1024; | ||
i++; | ||
} | ||
var exactSize = Math.round(_size * 100) / 100 + " " + fSExt[i]; | ||
return exactSize; | ||
} catch (error) { | ||
console.log("getFileSize error: ", error); | ||
throw error; | ||
} | ||
var exactSize = Math.round(_size * 100) / 100 + " " + fSExt[i]; | ||
return exactSize; | ||
}; | ||
export const htmlDecode = (input) => { | ||
var doc = new DOMParser().parseFromString(input, "text/html"); | ||
return doc.documentElement.textContent; | ||
try { | ||
var doc = new DOMParser().parseFromString(input, "text/html"); | ||
return doc.documentElement.textContent; | ||
} catch (error) { | ||
console.log("htmlDecode error: ", error); | ||
throw error; | ||
} | ||
}; | ||
export const htmlEncode = (input) => | ||
input.replace(/[\u00A0-\u9999<>\&]/g, function (i) { | ||
return "&#" + i.charCodeAt(0) + ";"; | ||
}); | ||
export const htmlEncode = (input) => { | ||
try { | ||
return input.replace(/[\u00A0-\u9999<>\&]/g, function (i) { | ||
return "&#" + i.charCodeAt(0) + ";"; | ||
}); | ||
} catch (error) { | ||
console.log("htmlEncode error: ", error); | ||
throw error; | ||
} | ||
}; | ||
export const getRandomNumber = (min, max) => { | ||
return Math.floor(Math.random() * (max - min + 1) + min); | ||
try { | ||
return Math.floor(Math.random() * (max - min + 1) + min); | ||
} catch (error) { | ||
console.log("getRandomNumber error: ", error); | ||
throw error; | ||
} | ||
}; | ||
export const convertSecondsToTime = (input, separator) => { | ||
var pad = function (input) { | ||
return input < 10 ? "0" + input : input; | ||
}; | ||
return [ | ||
pad(Math.floor(input / 3600)), | ||
pad(Math.floor((input % 3600) / 60)), | ||
pad(Math.floor(input % 60)), | ||
].join(typeof separator !== "undefined" ? separator : ":"); | ||
}; | ||
export const uniqueNumberFromString = (str, utf16 = false) => { | ||
let results = ""; | ||
const fixedNumber = utf16 ? 10000 : 100; | ||
for (let i = 0; i < str.length; i++) { | ||
results += (fixedNumber + str[i].charCodeAt(0)).toString(); | ||
export const uniqueNumberFromString = (stringValue, utf16 = false) => { | ||
try { | ||
let results = ""; | ||
const fixedNumber = utf16 ? 10000 : 100; | ||
for (let i = 0; i < stringValue.length; i++) { | ||
results += (fixedNumber + stringValue[i].charCodeAt(0)).toString(); | ||
} | ||
return parseInt(results); | ||
} catch (error) { | ||
console.log("uniqueNumberFromString error: ", error); | ||
throw error; | ||
} | ||
return parseInt(results); | ||
}; | ||
export function normalizeGraphFilterForExpress(filter) { | ||
let where = ""; | ||
Object.entries(filter).forEach(([key, val], index) => { | ||
const addition = index > 0 ? "," : ""; | ||
try { | ||
let where = ""; | ||
Object.entries(filter).forEach(([key, val], index) => { | ||
const addition = index > 0 ? "," : ""; | ||
if (val !== undefined && val != null) { | ||
if (typeof val === "string") { | ||
where += `${addition} ${key}: "${val}"`; | ||
} else if ( | ||
typeof val === "object" && | ||
!Array.isArray(val) && | ||
val.getTime() | ||
) { | ||
where += `${addition} ${key}: "${moment(val).format()}"`; | ||
} else if ( | ||
Array.isArray(val) && | ||
(key.includes("_nin") || key.includes("_in")) | ||
) { | ||
where += `${addition} ${key}: [${val.map((x) => { | ||
return `"${x}"`; | ||
})}]`; | ||
} else { | ||
where += `${addition} ${key}: ${val}`; | ||
if (val !== undefined && val != null) { | ||
if (typeof val === "string") { | ||
where += `${addition} ${key}: "${val}"`; | ||
} else if ( | ||
typeof val === "object" && | ||
!Array.isArray(val) && | ||
val.getTime() | ||
) { | ||
where += `${addition} ${key}: "${moment(val).format()}"`; | ||
} else if ( | ||
Array.isArray(val) && | ||
(key.includes("_nin") || key.includes("_in")) | ||
) { | ||
where += `${addition} ${key}: [${val.map((x) => { | ||
return `"${x}"`; | ||
})}]`; | ||
} else { | ||
where += `${addition} ${key}: ${val}`; | ||
} | ||
} | ||
} | ||
}); | ||
return where; | ||
}); | ||
return where; | ||
} catch (error) { | ||
console.log("normalizeGraphFilterForExpress error: ", error); | ||
throw error; | ||
} | ||
} | ||
export const normalizeAzPhoneNumber = (number) => { | ||
let cn = number || ""; | ||
let result = ""; | ||
if (cn.startsWith("+994")) { | ||
result = cn.replace("+994", "0"); | ||
if (result.startsWith("012")) { | ||
result = result.replace("012", ""); | ||
try { | ||
let cn = number || ""; | ||
let result = ""; | ||
if (cn.startsWith("+994")) { | ||
result = cn.replace("+994", "0"); | ||
if (result.startsWith("012")) { | ||
result = result.replace("012", ""); | ||
} | ||
} else { | ||
result = cn.replace("+", ""); | ||
} | ||
} else { | ||
result = cn.replace("+", ""); | ||
return result; | ||
} catch (error) { | ||
console.log("normalizeAzPhoneNumber error: ", error); | ||
throw error; | ||
} | ||
return result; | ||
}; | ||
export const normalizeAz9DigitNumber = (number) => { | ||
let cn = number || ""; | ||
let result = ""; | ||
if ( | ||
(cn.startsWith("+") && !cn.startsWith("+994")) || | ||
(cn.length > 10 && !cn.startsWith("+") && !cn.startsWith("994")) | ||
) { | ||
result = cn.replace("+", ""); | ||
} else if (cn.length > 7) { | ||
result = cn.substr(cn.length - 9, 9); | ||
} else { | ||
result = cn.replace("+", ""); | ||
try { | ||
let cn = number || ""; | ||
let result = ""; | ||
if ( | ||
(cn.startsWith("+") && !cn.startsWith("+994")) || | ||
(cn.length > 10 && !cn.startsWith("+") && !cn.startsWith("994")) | ||
) { | ||
result = cn.replace("+", ""); | ||
} else if (cn.length > 7) { | ||
result = cn.substr(cn.length - 9, 9); | ||
} else { | ||
result = cn.replace("+", ""); | ||
} | ||
return result; | ||
} catch (error) { | ||
console.log("normalizeAz9DigitNumber error: ", error); | ||
throw error; | ||
} | ||
return result; | ||
}; | ||
export const extractNumberFromTime = (value) => { | ||
export const extractNumberFromTime = (timeValue) => { | ||
try { | ||
let result = 0; | ||
if (value.includes(":")) { | ||
let arr = value.split(":"); | ||
if (timeValue.includes(":")) { | ||
let arr = timeValue.split(":"); | ||
let firstItem = arr[0]; | ||
@@ -121,5 +151,5 @@ | ||
return Number(result); | ||
} catch (err) { | ||
console.log("extractNumberFromTime err", err); | ||
return err; | ||
} catch (error) { | ||
console.log("extractNumberFromTime error", error); | ||
return error; | ||
} | ||
@@ -129,14 +159,30 @@ }; | ||
export const calculatePercentageOfNumber = (number, percentage) => { | ||
return (number / 100) * percentage; | ||
try { | ||
return (number / 100) * percentage; | ||
} catch (error) { | ||
console.log("calculatePercentageOfNumber error", error); | ||
return error; | ||
} | ||
}; | ||
export const generateRandomColor = () => | ||
Math.floor(Math.random() * 16777215).toString(16); | ||
export const generateRandomColor = () => { | ||
try { | ||
return `#${Math.floor(Math.random() * 16777215).toString(16)}`; | ||
} catch (error) { | ||
console.log("generateRandomColor error", error); | ||
return error; | ||
} | ||
}; | ||
export function normalizeSearchParams(params) { | ||
const resolvedParams = {}; | ||
Object.entries(params).forEach(([key, val]) => { | ||
if (val) resolvedParams[key] = val; | ||
}); | ||
return resolvedParams; | ||
try { | ||
const resolvedParams = {}; | ||
Object.entries(params).forEach(([key, val]) => { | ||
if (val) resolvedParams[key] = val; | ||
}); | ||
return resolvedParams; | ||
} catch (error) { | ||
console.log("normalizeSearchParams error", error); | ||
return error; | ||
} | ||
} |
118
src/date.js
@@ -1,10 +0,15 @@ | ||
export const convertMillisToMinutesAndSeconds = (millis) => { | ||
if (millis) { | ||
var minutes = Math.floor(millis / 60000); | ||
var seconds = ((millis % 60000) / 1000).toFixed(0); | ||
return seconds == 60 | ||
? minutes + 1 + ":00" | ||
: minutes + ":" + (seconds < 10 ? "0" : "") + seconds; | ||
} else { | ||
return "00:00"; | ||
export const convertMillisToMinutesAndSeconds = (milliSeconds) => { | ||
try { | ||
if (milliSeconds) { | ||
var minutes = Math.floor(milliSeconds / 60000); | ||
var seconds = ((milliSeconds % 60000) / 1000).toFixed(0); | ||
return seconds == 60 | ||
? minutes + 1 + ":00" | ||
: minutes + ":" + (seconds < 10 ? "0" : "") + seconds; | ||
} else { | ||
return "00:00"; | ||
} | ||
} catch (error) { | ||
console.log("convertMillisToMinutesAndSeconds error", error); | ||
return error; | ||
} | ||
@@ -14,17 +19,22 @@ }; | ||
export const calculateDurationWithStartDate = (date) => { | ||
if (!date) { | ||
return "Date required"; | ||
} | ||
var mDateTime = new Date(date).getTime(); | ||
var now = new Date().getTime(); | ||
try { | ||
if (!date) { | ||
return "Date required"; | ||
} | ||
var mDateTime = new Date(date).getTime(); | ||
var now = new Date().getTime(); | ||
if (!isNaN(mDateTime)) { | ||
if (mDateTime < now) { | ||
var msd = now - mDateTime; | ||
if (!isNaN(mDateTime)) { | ||
if (mDateTime < now) { | ||
var msd = now - mDateTime; | ||
} else { | ||
var msd = mDateTime - now; | ||
} | ||
return convertMillisToMinutesAndSeconds(msd); | ||
} else { | ||
var msd = mDateTime - now; | ||
return "00:00"; | ||
} | ||
return convertMillisToMinutesAndSeconds(msd); | ||
} else { | ||
return "00:00"; | ||
} catch (error) { | ||
console.log("calculateDurationWithStartDate error", error); | ||
return error; | ||
} | ||
@@ -34,17 +44,22 @@ }; | ||
export const convertDateToMilliSeconds = (date) => { | ||
if (!date) { | ||
return "Date required"; | ||
} | ||
var mDateTime = new Date(date).getTime(); | ||
var now = new Date().getTime(); | ||
try { | ||
if (!date) { | ||
return "Date required"; | ||
} | ||
var mDateTime = new Date(date).getTime(); | ||
var now = new Date().getTime(); | ||
if (!isNaN(mDateTime)) { | ||
if (mDateTime < now) { | ||
var msd = now - mDateTime; | ||
if (!isNaN(mDateTime)) { | ||
if (mDateTime < now) { | ||
var msd = now - mDateTime; | ||
} else { | ||
var msd = mDateTime - now; | ||
} | ||
return msd; | ||
} else { | ||
var msd = mDateTime - now; | ||
return 0; | ||
} | ||
return msd; | ||
} else { | ||
return 0; | ||
} catch (error) { | ||
console.log("convertDateToMilliSeconds error", error); | ||
return error; | ||
} | ||
@@ -54,15 +69,32 @@ }; | ||
export const convertTime12to24 = (time12h) => { | ||
const [time, modifier] = time12h.split(" "); | ||
let [hours, minutes] = time.split(":"); | ||
if (hours === "12") { | ||
hours = "00"; | ||
try { | ||
const [time, modifier] = time12h.split(" "); | ||
let [hours, minutes] = time.split(":"); | ||
if (hours === "12") { | ||
hours = "00"; | ||
} | ||
if (modifier === "PM") { | ||
hours = parseInt(hours, 10) + 12; | ||
} | ||
return `${hours}:${minutes}`; | ||
} catch (error) { | ||
console.log("convertTime12to24 error", error); | ||
return error; | ||
} | ||
}; | ||
if (modifier === "PM") { | ||
hours = parseInt(hours, 10) + 12; | ||
export const convertSecondsToTime = (input, separator) => { | ||
try { | ||
var pad = function (input) { | ||
return input < 10 ? "0" + input : input; | ||
}; | ||
return [ | ||
pad(Math.floor(input / 3600)), | ||
pad(Math.floor((input % 3600) / 60)), | ||
pad(Math.floor(input % 60)), | ||
].join(typeof separator !== "undefined" ? separator : ":"); | ||
} catch (error) { | ||
console.log("convertSecondsToTime error: ", error); | ||
throw error; | ||
} | ||
return `${hours}:${minutes}`; | ||
}; |
export const validateEmail = (email) => { | ||
let regex = | ||
/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; | ||
let result = regex.exec(email); | ||
return result; | ||
try { | ||
let regex = | ||
/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; | ||
let result = regex.exec(email); | ||
return result; | ||
} catch (error) { | ||
console.log("validateEmail error", error); | ||
return error; | ||
} | ||
}; | ||
export const validateNumber = (number) => { | ||
let regex = /^[+]*[(]{0,1}[0-9]{1,3}[)]{0,1}[-\s\./0-9]*$/g; | ||
let result = regex.test(number); | ||
return result; | ||
try { | ||
let regex = /^[+]*[(]{0,1}[0-9]{1,3}[)]{0,1}[-\s\./0-9]*$/g; | ||
let result = regex.test(number); | ||
return result; | ||
} catch (error) { | ||
console.log("validateNumber error", error); | ||
return error; | ||
} | ||
}; | ||
export function isJsonString(str) { | ||
export function isJsonString(stringValue) { | ||
try { | ||
JSON.parse(str); | ||
JSON.parse(stringValue); | ||
} catch (e) { | ||
@@ -18,0 +28,0 @@ return false; |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
9180
295
31
0