@bombaysoftwares/tskit
Version:
The @bombaysoftwares/tskit package provides a set of utility functions for working with dates and timestamps in JavaScript. These functions simplify common tasks such as date formatting, timestamp conversion, and JSON validation.
6 lines (5 loc) • 13.8 kB
JavaScript
/*! @bombaysoftwares/tskit ! v - 1.0.15
* license : MIT
Build time: 07-23-2025 06:52:34
*/
;var __awaiter=this&&this.__awaiter||function(thisArg,_arguments,P,generator){function adopt(value){return value instanceof P?value:new P(function(resolve){resolve(value)})}return new(P||(P=Promise))(function(resolve,reject){function fulfilled(value){try{step(generator.next(value))}catch(e){reject(e)}}function rejected(value){try{step(generator["throw"](value))}catch(e){reject(e)}}function step(result){result.done?resolve(result.value):adopt(result.value).then(fulfilled,rejected)}step((generator=generator.apply(thisArg,_arguments||[])).next())})};var __generator=this&&this.__generator||function(thisArg,body){var _={label:0,sent:function(){if(t[0]&1)throw t[1];return t[1]},trys:[],ops:[]},f,y,t,g=Object.create((typeof Iterator==="function"?Iterator:Object).prototype);return g.next=verb(0),g["throw"]=verb(1),g["return"]=verb(2),typeof Symbol==="function"&&(g[Symbol.iterator]=function(){return this}),g;function verb(n){return function(v){return step([n,v])}}function step(op){if(f)throw new TypeError("Generator is already executing.");while(g&&(g=0,op[0]&&(_=0)),_)try{if(f=1,y&&(t=op[0]&2?y["return"]:op[0]?y["throw"]||((t=y["return"])&&t.call(y),0):y.next)&&!(t=t.call(y,op[1])).done)return t;if(y=0,t)op=[op[0]&2,t.value];switch(op[0]){case 0:case 1:t=op;break;case 4:_.label++;return{value:op[1],done:false};case 5:_.label++;y=op[1];op=[0];continue;case 7:op=_.ops.pop();_.trys.pop();continue;default:if(!(t=_.trys,t=t.length>0&&t[t.length-1])&&(op[0]===6||op[0]===2)){_=0;continue}if(op[0]===3&&(!t||op[1]>t[0]&&op[1]<t[3])){_.label=op[1];break}if(op[0]===6&&_.label<t[1]){_.label=t[1];t=op;break}if(t&&_.label<t[2]){_.label=t[2];_.ops.push(op);break}if(t[2])_.ops.pop();_.trys.pop();continue}op=body.call(thisArg,_)}catch(e){op=[6,e];y=0}finally{f=t=0}if(op[0]&5)throw op[1];return{value:op[0]?op[1]:void 0,done:true}}};Object.defineProperty(exports,"__esModule",{value:true});exports.underscoreToCapitalizedText=exports.formatTextToCapitalized=exports.getArray=exports.isSetNumber=exports.strToDate=exports.millisToMinutesAndSeconds=exports.formatDuration=exports.formatTimestampToDateMonthYearString=exports.formatTimestampToDateString=exports.formatTimestamp=exports.dateFormatHHMM=exports.dateFormat=exports.dateAndTimeFormat=exports.getDayFromDate=exports.getUnixConvertedIsoString=exports.getUnixConvertedDateTime=exports.getLocalDateHHMM=exports.getLocalDate=exports.getDateTimeFromTimestamp=exports.getCurrentDate=exports.getCurrentTimestamp=exports.getCurrentDateTime=exports.tweleveHourFormat=exports.camelCaseKeys=exports.getTextFromHtml=exports.handleCopyToClipboard=exports.getRandomColor=exports.isValidJsonData=exports.getTwodigitFormat=exports.evalBooleanValue=exports.isSetObject=exports.isSet=void 0;exports.getUniqueValueFromArray=getUniqueValueFromArray;var constants_1=require("./constants");var isSet=function(obj){if(obj&&obj!=="null"&&obj!==undefined&&obj!==""&&obj!=="[]"&&obj!=="undefined"&&typeof obj!=="undefined"){return true}return false};exports.isSet=isSet;var isSetObject=function(obj){if((0,exports.isSet)(obj)){return Object.keys(obj).length>0?true:false}else{return false}};exports.isSetObject=isSetObject;var evalBooleanValue=function(value){return value==="true"||value===true?true:false};exports.evalBooleanValue=evalBooleanValue;var getTwodigitFormat=function(data){if(data>99){return null}return data>9?data:"0"+data};exports.getTwodigitFormat=getTwodigitFormat;var isValidJsonData=function(data){var json_data;try{json_data=JSON.parse(data)}catch(e){return false}return json_data};exports.isValidJsonData=isValidJsonData;var getRandomColor=function(){var letters="0123456789ABCDEF";var color="#";for(var i=0;i<6;i++){color+=letters[Math.floor(Math.random()*16)]}return color};exports.getRandomColor=getRandomColor;var handleCopyToClipboard=function(text){return __awaiter(void 0,void 0,void 0,function(){return __generator(this,function(_a){switch(_a.label){case 0:if(!((navigator===null||navigator===void 0?void 0:navigator.clipboard)&&(window===null||window===void 0?void 0:window.isSecureContext)))return[3,2];return[4,navigator.clipboard.writeText(text)];case 1:_a.sent();return[2,{success:true}];case 2:if(!(window===null||window===void 0?void 0:window.isSecureContext)){return[2,{success:false,message:"Please host in the secure environment."}]}return[2,{success:false,message:"Something went wrong"}]}})})};exports.handleCopyToClipboard=handleCopyToClipboard;var getTextFromHtml=function(htmlString){if((0,exports.isSet)(htmlString)){return htmlString.replace(/(<([^>]+)>)/gi,"")}else{return""}};exports.getTextFromHtml=getTextFromHtml;var camelCaseKeys=function(obj){var camelCasedObj={};for(var key in obj){if(obj.hasOwnProperty(key)){var camelCasedKey=key.replace(/_([a-z])/g,function(_,match){return match.toUpperCase()});camelCasedObj[camelCasedKey]=obj[key]}}return camelCasedObj};exports.camelCaseKeys=camelCaseKeys;var tweleveHourFormat=function(hours){hours=hours%12;hours=hours?hours:12;return hours.toString().padStart(2,"0")};exports.tweleveHourFormat=tweleveHourFormat;var getCurrentDateTime=function(){var date_ob=new Date;var date=("0"+date_ob.getDate()).slice(-2);var month="0"+(date_ob.getMonth()+1);var year=date_ob.getFullYear();var hours=(0,exports.getTwodigitFormat)(date_ob.getHours());var minutes=(0,exports.getTwodigitFormat)(date_ob.getMinutes());var seconds=(0,exports.getTwodigitFormat)(date_ob.getSeconds());return year+"-"+month+"-"+date+" "+hours+":"+minutes+":"+seconds};exports.getCurrentDateTime=getCurrentDateTime;var getCurrentTimestamp=function(date){if(date===void 0){date=null}if((0,exports.isSet)(date)&&date instanceof Date){return Math.floor(date.getTime()/1e3)}else{return Math.floor(Date.now()/1e3)}};exports.getCurrentTimestamp=getCurrentTimestamp;var getCurrentDate=function(){var date_ob=new Date;var date=("0"+date_ob.getDate()).slice(-2);var month=("0"+(date_ob.getMonth()+1)).slice(-2);var year=date_ob.getFullYear();return year+"-"+month+"-"+date};exports.getCurrentDate=getCurrentDate;var getDateTimeFromTimestamp=function(timestamp,showSeconds){if(showSeconds===void 0){showSeconds=true}var dateObject=new Date(timestamp*1e3);return(0,exports.dateAndTimeFormat)(dateObject,showSeconds)};exports.getDateTimeFromTimestamp=getDateTimeFromTimestamp;var getLocalDate=function(dateAndTime){if((0,exports.isSet)(dateAndTime)){var dateObject=new Date(dateAndTime);if(isNaN(dateObject.getTime())){return null}return(0,exports.dateFormat)(dateObject)}else{return null}};exports.getLocalDate=getLocalDate;var getLocalDateHHMM=function(dateAndTime){if((0,exports.isSet)(dateAndTime)){var dateObject=new Date(dateAndTime);if(isNaN(dateObject.getTime())){return null}return(0,exports.dateFormatHHMM)(dateObject)}else{return null}};exports.getLocalDateHHMM=getLocalDateHHMM;var getUnixConvertedDateTime=function(_a){var _b=_a===void 0?{}:_a,_c=_b.timestamp,timestamp=_c===void 0?null:_c,_d=_b.dateObj,dateObj=_d===void 0?null:_d;var date_ob;if(dateObj instanceof Date){date_ob=dateObj}else if(timestamp){date_ob=new Date(timestamp*1e3)}else{date_ob=new Date}var date=("0"+date_ob.getDate()).slice(-2);var month=("0"+(date_ob.getMonth()+1)).slice(-2);var year=date_ob.getFullYear();var hours=date_ob.getHours();var minutes=date_ob.getMinutes();var ampm=hours>=12?"PM":"AM";var newHour=(0,exports.tweleveHourFormat)(hours);minutes=minutes<10?"0"+minutes:minutes;var seconds=date_ob.getSeconds();seconds=seconds<10?"0"+seconds:seconds;return year+"-"+month+"-"+date+" "+newHour+":"+minutes+":"+seconds+" "+ampm};exports.getUnixConvertedDateTime=getUnixConvertedDateTime;var getUnixConvertedIsoString=function(timestamp){var _timeStamp=typeof timestamp==="number"?timestamp:parseInt(timestamp);return new Date(_timeStamp*1e3).toISOString()};exports.getUnixConvertedIsoString=getUnixConvertedIsoString;var getDayFromDate=function(dateString){var dateFormats=["DD-MMM-YYYY","DD/MM/YYYY","DD/MMM/YYYY"];var date=null;for(var _i=0,dateFormats_1=dateFormats;_i<dateFormats_1.length;_i++){var format=dateFormats_1[_i];date=(0,exports.strToDate)(dateString,format);if(date)break}if(!date){return"Invalid date format"}var days=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];var dayOfWeek=date.getDay();return days[dayOfWeek]};exports.getDayFromDate=getDayFromDate;var dateAndTimeFormat=function(dateObject,showSeconds){if(showSeconds===void 0){showSeconds=true}if((0,exports.isSet)(dateObject)){var date=(0,exports.getTwodigitFormat)(dateObject.getDate());var hour=(0,exports.getTwodigitFormat)(dateObject.getHours());var minutes=(0,exports.getTwodigitFormat)(dateObject.getMinutes());var seconds=(0,exports.getTwodigitFormat)(dateObject.getSeconds());if(showSeconds){return date+"-"+constants_1.MONTH[dateObject.getMonth()]+"-"+dateObject.getFullYear()+" "+hour+":"+minutes+":"+seconds}else{return date+"-"+constants_1.MONTH[dateObject.getMonth()]+"-"+dateObject.getFullYear()+" "+hour+":"+minutes}}else{return null}};exports.dateAndTimeFormat=dateAndTimeFormat;var dateFormat=function(dateObject){if((0,exports.isSet)(dateObject)){var date=(0,exports.getTwodigitFormat)(dateObject.getDate());return date+"-"+constants_1.MONTH[dateObject.getMonth()]+"-"+dateObject.getFullYear()}else{return null}};exports.dateFormat=dateFormat;var dateFormatHHMM=function(dateObject){if((0,exports.isSet)(dateObject)){var date=(0,exports.getTwodigitFormat)(dateObject.getDate());var hour=(0,exports.getTwodigitFormat)(dateObject.getHours());var minutes=(0,exports.getTwodigitFormat)(dateObject.getMinutes());return date+"-"+constants_1.MONTH[dateObject.getMonth()]+"-"+dateObject.getFullYear()+", "+hour+":"+minutes}else{return null}};exports.dateFormatHHMM=dateFormatHHMM;var formatTimestamp=function(timestamp){var Time=new Date(timestamp*1e3);var date=(0,exports.getTwodigitFormat)(Time.getUTCDate());var month=(0,exports.getTwodigitFormat)(Time.getUTCMonth()+1);var year=Time.getFullYear();var hours=(0,exports.getTwodigitFormat)(Time.getUTCHours());var minutes=(0,exports.getTwodigitFormat)(Time.getUTCMinutes());return date+"-"+month+"-"+year+" "+hours+":"+minutes+" "};exports.formatTimestamp=formatTimestamp;var formatTimestampToDateString=function(timestamp){var currentTime=new Date;var currentOffset=currentTime.getTimezoneOffset();var ISTOffset=330;var ISTTime=new Date(timestamp*1e3+(ISTOffset+currentOffset)*6e4);var dateIST=(0,exports.getTwodigitFormat)(ISTTime.getDate());var monthIST=(0,exports.getTwodigitFormat)(ISTTime.getMonth()+1);var yearIST=ISTTime.getFullYear();return dateIST+"/"+monthIST+"/"+yearIST+" "};exports.formatTimestampToDateString=formatTimestampToDateString;var formatTimestampToDateMonthYearString=function(timestamp){var currentTime=new Date;var currentOffset=currentTime.getTimezoneOffset();var ISTOffset=330;var ISTTime=new Date(timestamp*1e3+(ISTOffset+currentOffset)*6e4);var dateIST=ISTTime.getDate();var monthStr=constants_1.MONTH[ISTTime.getMonth()];var yearIST=ISTTime.getFullYear();return dateIST+" "+monthStr+", "+yearIST+" "};exports.formatTimestampToDateMonthYearString=formatTimestampToDateMonthYearString;var formatDuration=function(totalSeconds){if(totalSeconds===null){return""}var hours=Math.floor(totalSeconds/3600);var minutes=Math.floor(totalSeconds%3600/60);var seconds=totalSeconds%60;var formattedTime="";if(hours>0){formattedTime+="".concat(hours,"hr ")}if(minutes>0){formattedTime+="".concat(minutes,"min ")}if(seconds>0){formattedTime+="".concat(seconds,"sec")}return formattedTime.trim()};exports.formatDuration=formatDuration;var millisToMinutesAndSeconds=function(millis){var minutes=Math.floor(millis/6e4);var seconds=(millis%6e4/1e3).toFixed(0);var milliseconds=Math.floor(millis%1e3).toString().padStart(3,"0");return"".concat(minutes,":").concat(parseInt(seconds)<10?"0":"").concat(seconds,".").concat(milliseconds)};exports.millisToMinutesAndSeconds=millisToMinutesAndSeconds;var strToDate=function(dateString,format){var parts=dateString.split(/[\/-]/);var formatParts=format.split(/[-/]/);var dateObj={};for(var i=0;i<formatParts.length;i++){var formatPart=formatParts[i].toUpperCase();var part=parts[i];if(formatPart==="DD"){dateObj.day=parseInt(part,10)}else if(formatPart==="MM"){dateObj.month=parseInt(part,10)-1}else if(formatPart==="MMM"){var monthMap={JAN:0,FEB:1,MAR:2,APR:3,MAY:4,JUN:5,JUL:6,AUG:7,SEP:8,OCT:9,NOV:10,DEC:11};dateObj.month=monthMap[part.toUpperCase()]}else if(formatPart==="YYYY"){dateObj.year=parseInt(part,10)}}var day=dateObj.day,month=dateObj.month,year=dateObj.year;if(isNaN(day)||isNaN(month)||isNaN(year)){return null}return new Date(year,month,day)};exports.strToDate=strToDate;var isSetNumber=function(value){if(value!==null&&value!==undefined){return true}return false};exports.isSetNumber=isSetNumber;var getArray=function(length){return Array.from({length:length},function(_,i){return i+1})};exports.getArray=getArray;function getUniqueValueFromArray(array){if(!Array.isArray(array)){throw new TypeError("Input must be an array")}if(array.length===0){return[]}var firstType=typeof array[0];for(var i=1;i<array.length;i++){if(typeof array[i]!==firstType){throw new TypeError("Array contains mixed data types. Only one data type is allowed.")}}return Array.from(new Set(array))}var formatTextToCapitalized=function(text){if(typeof text!=="string"||!text.length)return"";if(text.length===1)return text.toUpperCase();return text.charAt(0).toUpperCase()+text.slice(1).toLowerCase()};exports.formatTextToCapitalized=formatTextToCapitalized;var underscoreToCapitalizedText=function(text){if(typeof text!=="string"||!text.trim())return"";return text.split("_").map(exports.formatTextToCapitalized).join(" ")};exports.underscoreToCapitalizedText=underscoreToCapitalizedText;