Comparing version 1.0.17 to 1.0.18
{ | ||
"name": "js-awe", | ||
"version": "1.0.17", | ||
"version": "1.0.18", | ||
"homepage": "https://github.com/josuamanuel/js-awe", | ||
@@ -5,0 +5,0 @@ "author": "josuamanuel@hotmail.com", |
@@ -754,2 +754,10 @@ 'use strict' | ||
function toDate(date) | ||
{ | ||
return date | ||
? new Date(date) | ||
: new Date() | ||
} | ||
function isDate(d) { | ||
@@ -762,7 +770,3 @@ return d instanceof Date && !isNaN(d); | ||
const date = new Date(stringDate) | ||
if (date >= new Date('0000-01-01') && date <= new Date('9999-12-31')) return true | ||
return false | ||
return isDate(new Date(stringDate)) | ||
} | ||
@@ -775,5 +779,7 @@ | ||
function formatDate(format, date) { | ||
let dateToProcess = toDate(date) | ||
let dateToProcess = (date ? new Date(date) : new Date()) | ||
if (isDate(dateToProcess) === false) return undefined | ||
const months = new EnumMap({ | ||
@@ -810,13 +816,13 @@ 'January': '01', | ||
const YYYY = dateIsoString.substr(0, 4) | ||
const YY = dateIsoString.substr(2, 2) | ||
const MM = dateIsoString.substr(5, 2) | ||
const DD = dateIsoString.substr(8, 2) | ||
const YYYY = dateIsoString.substring(0, 4) | ||
const YY = dateIsoString.substring(2, 4) | ||
const MM = dateIsoString.substring(5, 7) | ||
const DD = dateIsoString.substring(8, 10) | ||
const D = parseInt(DD, 10).toString() | ||
const hh = dateIsoString.substr(11, 2) | ||
const hh = dateIsoString.substring(11, 13) | ||
const h = parseInt(hh, 10).toString() | ||
const mm = dateIsoString.substr(14, 2) | ||
const ss = dateIsoString.substr(17, 2) | ||
const mil = dateIsoString.substr(20, 3) | ||
const mi = dateIsoString.substr(20, 2) | ||
const mm = dateIsoString.substring(14, 16) | ||
const ss = dateIsoString.substring(17, 19) | ||
const mil = dateIsoString.substring(20, 23) | ||
const mi = dateIsoString.substring(20, 22) | ||
@@ -846,8 +852,8 @@ const month = indexMonths[MM] | ||
// Input format has 1 char (any could work) between each elements: years, months, days, hours, minutes and seconds | ||
const dateYYYY = parseInt(dateYYYY_MM_DD_hh_mm_ss.substr(0, 4)) | ||
const dateMM = parseInt(dateYYYY_MM_DD_hh_mm_ss.substr(5, 2)) - 1 // Months start with 0 | ||
const dateDD = parseInt(dateYYYY_MM_DD_hh_mm_ss.substr(8, 2)) | ||
const datehh = parseInt(dateYYYY_MM_DD_hh_mm_ss.substr(11, 2)) | ||
const datemm = parseInt(dateYYYY_MM_DD_hh_mm_ss.substr(14, 2)) | ||
const datess = parseInt(dateYYYY_MM_DD_hh_mm_ss.substr(17, 2)) | ||
const dateYYYY = parseInt(dateYYYY_MM_DD_hh_mm_ss.substring(0, 4)) | ||
const dateMM = parseInt(dateYYYY_MM_DD_hh_mm_ss.substring(5, 7)) - 1 // Months start with 0 | ||
const dateDD = parseInt(dateYYYY_MM_DD_hh_mm_ss.substring(8, 10)) | ||
const datehh = parseInt(dateYYYY_MM_DD_hh_mm_ss.substring(11, 13)) | ||
const datemm = parseInt(dateYYYY_MM_DD_hh_mm_ss.substring(14, 16)) | ||
const datess = parseInt(dateYYYY_MM_DD_hh_mm_ss.substring(17, 19)) | ||
@@ -858,13 +864,16 @@ return Date.UTC(dateYYYY, dateMM, dateDD, datehh, datemm, datess) | ||
function dateToObj(date) { | ||
let dateToProcess = new Date(date) ?? new Date() | ||
let dateToProcess = toDate(date) | ||
if (isDate(dateToProcess) === false) return undefined | ||
let ISODate = dateToProcess.toISOString() | ||
return { | ||
YYYY: parseInt(ISODate.substr(0, 4)), | ||
MM: parseInt(ISODate.substr(5, 2)), | ||
DD: parseInt(ISODate.substr(8, 2)), | ||
hh: parseInt(ISODate.substr(11, 2)), | ||
mm: parseInt(ISODate.substr(14, 2)), | ||
ss: parseInt(ISODate.substr(17, 2)), | ||
mil: parseInt(ISODate.substr(20, 3)) | ||
YYYY: parseInt(ISODate.substring(0, 4)), | ||
MM: parseInt(ISODate.substring(5, 7)), | ||
DD: parseInt(ISODate.substring(8, 10)), | ||
hh: parseInt(ISODate.substring(11, 13)), | ||
mm: parseInt(ISODate.substring(14, 16)), | ||
ss: parseInt(ISODate.substring(17, 19)), | ||
mil: parseInt(ISODate.substring(20, 23)) | ||
} | ||
@@ -886,11 +895,8 @@ } | ||
function subtractDays(daysToSubtract, date) { | ||
let dateToReturn = | ||
date | ||
? new Date(date) | ||
: new Date() | ||
let dateToProcess = toDate(date) | ||
if (isDate(dateToReturn) === false) return date | ||
if (isDate(dateToProcess) === false) return dateToProcess | ||
dateToReturn.setDate(dateToReturn.getDate() - daysToSubtract); | ||
return dateToReturn | ||
dateToProcess.setDate(dateToProcess.getDate() - daysToSubtract); | ||
return dateToProcess | ||
} | ||
@@ -901,11 +907,8 @@ //subtractDays(40).toISOString() //? | ||
function addDays(daysToAdd, date) { | ||
let dateToReturn = | ||
date | ||
? new Date(date) | ||
: new Date() | ||
let dateToProcess = toDate(date) | ||
if (isDate(dateToReturn) === false) return date | ||
if (isDate(dateToProcess) === false) return dateToProcess | ||
dateToReturn.setDate(dateToReturn.getDate() + daysToAdd); | ||
return dateToReturn | ||
dateToProcess.setDate(dateToProcess.getDate() + daysToAdd); | ||
return dateToProcess | ||
} | ||
@@ -915,7 +918,7 @@ // addDays(2, "2022-01-01") //? | ||
function previousDayOfWeek(dayOfWeek, date) { | ||
let dateObj = date ?? new Date() | ||
let dateToProcess = toDate(date) | ||
if (isDate(dateObj) === false) return date | ||
if (isDate(dateToProcess) === false) return dateToProcess | ||
let diffInDaysOfWeek = dateObj.getDay() - dayOfWeek | ||
let diffInDaysOfWeek = dateToProcess.getDay() - dayOfWeek | ||
@@ -926,3 +929,3 @@ let toSubtract = diffInDaysOfWeek >= 0 | ||
return subtractDays(toSubtract, dateObj) | ||
return subtractDays(toSubtract, dateToProcess) | ||
} | ||
@@ -933,15 +936,15 @@ //previousDayOfWeek(6,new Date('2021-05-07')) //? | ||
function getSameDateOrPreviousFridayForWeekends(date) { | ||
let dateObj = date ?? new Date() | ||
let dateToProcess = toDate(date) | ||
if (isDate(dateObj) === false) return date | ||
if (isDate(dateToProcess) === false) return dateToProcess | ||
const dayOfWeek = dateObj.getUTCDay() | ||
const dayOfWeek = dateToProcess.getUTCDay() | ||
if (dayOfWeek > 0 && dayOfWeek < 6) return dateObj | ||
if (dayOfWeek > 0 && dayOfWeek < 6) return dateToProcess | ||
//Sunday | ||
if (dayOfWeek === 0) return subtractDays(2, dateObj) | ||
if (dayOfWeek === 0) return subtractDays(2, dateToProcess) | ||
//Saturday (dayOfWeek === 6) | ||
return subtractDays(1, dateObj) | ||
return subtractDays(1, dateToProcess) | ||
} | ||
@@ -955,11 +958,13 @@ // getSameDateOrPreviousFridayForWeekends() //? | ||
function isDateMidnight(date) { | ||
return date?.toISOString?.()?.substr(10, 14) === 'T00:00:00.000Z' | ||
return date?.toISOString?.()?.substring(10, 24) === 'T00:00:00.000Z' | ||
} | ||
function setDateToMidnight(date) { | ||
if (isDate(date) === false) return date | ||
let dateToProcess = toDate(date) | ||
if (isDateMidnight(date) === true) return date | ||
if (isDate(dateToProcess) === false) return dateToProcess | ||
return new Date(date.toISOString().substr(0, 10)) | ||
if (isDateMidnight(dateToProcess) === true) return dateToProcess | ||
return new Date(dateToProcess.toISOString().substring(0, 10)) | ||
} | ||
@@ -966,0 +971,0 @@ |
import { strict as assert } from 'assert' | ||
import { EnumMap, Enum, formatDate, CustomError, findDeepKey, traverse, traverseVertically } from '../src/jsUtils.js' | ||
import { EnumMap, Enum, formatDate, addDays, subtractDays, diffInDaysYYYY_MM_DD, previousDayOfWeek, CustomError, findDeepKey, traverse, traverseVertically } from '../src/jsUtils.js' | ||
import clone from 'just-clone' | ||
@@ -376,2 +376,34 @@ | ||
it('addDays', () => { | ||
assert.strictEqual(addDays(3, new Date('2023-02-27')).toISOString(), new Date('2023-03-02').toISOString()) | ||
}) | ||
it('subtractDays', () => { | ||
assert.strictEqual(subtractDays(3, new Date('2023-03-02')).toISOString(), new Date('2023-02-27').toISOString()) | ||
}) | ||
it('diffInDaysYYYY_MM_DD', () => { | ||
assert.strictEqual(diffInDaysYYYY_MM_DD('2023-02-27', '2023-03-02'), 3) | ||
}) | ||
it('previousDayOfWeek', () => { | ||
assert.strictEqual(previousDayOfWeek(6, new Date('2023-03-19')).toISOString(), new Date('2023-03-18').toISOString()) | ||
}) | ||
it('previousDayOfWeek return the same date when the input date is the day of the week requested', () => { | ||
assert.strictEqual(previousDayOfWeek(0, new Date('2023-03-19')).toISOString(), new Date('2023-03-19').toISOString()) | ||
}) | ||
it('previousDayOfWeek return the same date when the input date is the day of the week requested', () => { | ||
assert.strictEqual(previousDayOfWeek(4, new Date('2023-03-16')).toISOString(), new Date('2023-03-16').toISOString()) | ||
}) | ||
it('previousDayOfWeek', () => { | ||
assert.strictEqual(previousDayOfWeek(4, new Date('2023-03-19')).toISOString(), new Date('2023-03-16').toISOString()) | ||
}) | ||
it('previousDayOfWeek. You can use string date format', () => { | ||
assert.strictEqual(previousDayOfWeek(4, '2023-03-19').toISOString(), new Date('2023-03-16').toISOString()) | ||
}) | ||
const findDeepKeyObjSubject = { | ||
@@ -378,0 +410,0 @@ house: |
@@ -49,2 +49,3 @@ export default jsUtils; | ||
export { diffInDaysYYYY_MM_DD }; | ||
export { addDays }; | ||
export { subtractDays }; | ||
@@ -211,9 +212,15 @@ export { previousDayOfWeek }; | ||
export function fillWith(mapper: any, lenOrWhileTruthFun: any): any[]; | ||
type Month = '01' | '02' | '03' | '04' | '05' | '06' | '07' | '08' | '09' | '10' | '11' | '12'; | ||
type Day = '01' | '02' | '03' | '04' | '05' | '06' | '07' | '08' | '09' | '10' | '11' | '12' | '13' | '14' | '15' | '16' | '17' | '18' | '19' | '20' | '21' | '22' | '23' | '24' | '25' | '26' | '27' | '28' | '29' | '30' | '31'; | ||
type Separator = '/' | '-' | ||
type StringDate = `${number}${Separator}${Month}${Separator}${Day}${string}`; | ||
export function isDate(d: any): boolean; | ||
export function isEmpty(value: any): boolean; | ||
export function isStringADate(stringDate: any): boolean; | ||
export function formatDate(format: any, date: any): any; | ||
export function dateFormatter(format: any): (date: any) => any; | ||
export function YYYY_MM_DD_hh_mm_ss_ToUtcDate(dateYYYY_MM_DD_hh_mm_ss: any): number; | ||
export function dateToObj(date: any): { | ||
export function isStringADate(stringDate: string): boolean; | ||
export function formatDate(format: any, date?: Date | StringDate): string | undefined; | ||
export function dateFormatter(format: string): (date: Date | StringDate) => string | undefined; | ||
export function YYYY_MM_DD_hh_mm_ss_ToUtcDate(dateYYYY_MM_DD_hh_mm_ss: string): number; | ||
export function dateToObj(date: Date | StringDate): { | ||
YYYY: number; | ||
@@ -226,9 +233,10 @@ MM: number; | ||
mil: number; | ||
}; | ||
export function diffInDaysYYYY_MM_DD(iniDate: any, endDate: any): number; | ||
export function subtractDays(daysToSubtract: any, date: any): any; | ||
export function previousDayOfWeek(dayOfWeek: any, date: any): any; | ||
export function getSameDateOrPreviousFridayForWeekends(date: any): any; | ||
export function isDateMidnight(date: Date):boolean; | ||
export function setDateToMidnight(date: Date): Date; | ||
} | undefined; | ||
export function diffInDaysYYYY_MM_DD(iniDate: Date | StringDate, endDate: Date | StringDate): number; | ||
export function addDays(daysToSubtract: number, date: Date | StringDate): Date; | ||
export function subtractDays(daysToSubtract: number, date: Date | StringDate): Date; | ||
export function previousDayOfWeek(dayOfWeek: 0 | 1 | 2 | 3 | 4 | 5 | 6, date: Date | StringDate): Date; | ||
export function getSameDateOrPreviousFridayForWeekends(date: Date | StringDate): Date; | ||
export function isDateMidnight(date: Date):boolean | undefined; | ||
export function setDateToMidnight(date: Date | StringDate): Date; | ||
export function replaceAll(str: any, ...fromTo: any[]): any; | ||
@@ -235,0 +243,0 @@ export function cleanString(str: any): any; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
224080
6730