Skip to content

Instantly share code, notes, and snippets.

@asSqr
Created June 21, 2021 13:53
Show Gist options
  • Save asSqr/ca01b9c52e5068e93cd472d72a04614b to your computer and use it in GitHub Desktop.
Save asSqr/ca01b9c52e5068e93cd472d72a04614b to your computer and use it in GitHub Desktop.
export const iso2TimeString = isoTime => {
const tIndex = isoTime.indexOf('T');
const dotIndex = isoTime.indexOf('.');
let date = isoTime.substr(0, tIndex);
const time = isoTime.substr(tIndex+1, dotIndex-tIndex-1);
date = date.replace(/-/g, '/');
return date + ' ' + time;
}
const leadingZero = str => {
return ("0" + str).slice(-2);
}
export const formatDate = date => {
const yearStr = date.getFullYear();
const monthStr = leadingZero(1 + date.getMonth());
const dayStr = leadingZero(date.getDate());
const hourStr = leadingZero(date.getHours());
const minuteStr = leadingZero(date.getMinutes());
const secondStr = leadingZero(date.getSeconds());
let formatStr = 'YYYY/MM/DD hh:mm:ss';
formatStr = formatStr.replace(/YYYY/g, yearStr);
formatStr = formatStr.replace(/MM/g, monthStr);
formatStr = formatStr.replace(/DD/g, dayStr);
formatStr = formatStr.replace(/hh/g, hourStr);
formatStr = formatStr.replace(/mm/g, minuteStr);
formatStr = formatStr.replace(/ss/g, secondStr);
return formatStr;
};
export const unescapeHtml = target => {
if( typeof target !== 'string' )
return target;
const patterns = {
'&lt;' : '<',
'&gt;' : '>',
'&amp;' : '&',
'&quot;' : '"',
'&#x27;' : '\'',
'&#x60;' : '`',
'&#x2F;' : '/',
};
return target.replace(/&(lt|gt|amp|quot|#x27|#x60|#x2F);/g, match => {
return patterns[match];
});
};
export const deleteHtmlTag = htmlStr => {
const pattern = new RegExp("<(\"[^\"]*\"|'[^']*'|[^'\">])*>", "g");
return htmlStr.replace(pattern, "");
};
export const summarizeStr = str => {
const charCount = 80;
if( str && str.length < charCount-2 )
return str;
return str.slice(0, charCount - 2) + "…";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment